Standard library - ylab-nsu/cdm16-llvm-neo GitHub Wiki
The toolchain includes a set of standard libraries linked with each executable by default:
-
libc- C standard library functions, freestanding subset; -
crt0- default interrupt vector table (IVT), startup code; -
cdm-builtins- software implementations of missing hardware instructions.
These can be fully or partially disabled using Clang linker options:
-
-nostdlib- disable ALL implicitly linked libraries; -
-nodefaultlibs- disablelibcandcdm-builtins, but keepcrt0; -
-nostartfiles- disablecrt0; -
-lc- explicitly linklibc; -
-lclang_rt.cdm-builtins- explicitly linkcdm-builtins; - explicitly link
crt0(different for each LLVM target and memory model:-
-l:crt0.o-cdm-cocastarget; -
-l:crt0-vonNeumann.o-cdmtarget, von Neumann version; -
-l:crt0-harvard.o-cdmtarget, Harvard version.
-
The included implementation of the C standard library is a conforming freestanding implementation, as defined in ISO/IEC 9899:2024 (C23), section 4.7.
It supports the following headers:
- All declarations:
<float.h>,<iso646.h>,<limits.h>,<stdalign.h>,<stdarg.h><stdbit.h>,<stdbool.h><stddef.h>,<stdint.h>,<stdnoreturn.h>; - All declarations with exceptions:
<string.h>;- Unsupported functions:
strcoll,strdup,strerror,strndup,strtok,strxfrm;
- Unsupported functions:
- Select declarations:
<stdlib.h>;- Supported functions:
memalignment.
- Supported functions:
Note
- Despite
<float.h>being in this list, floating point arithmetic is not supported yet. - Heap allocation (
mallocfrom<stdlib.h>) is not supported.
Warning
By default, the compiler generates calls to memcpy, memmove and memset. If you disable libc, you will have to implement these functions yourself. Similarly, if you disable cdm-builtins, you will have to provide your own implementations of the software arithmetic functions, e.g. __mulhi3, __modhi3, __divhi3.
This library provides an interrupt vector table with a default reset vector and exception handlers. Unlike other libraries, it is a plain object file, because it needs to be linked unconditionally.
The startup vector initializes the stack pointer (SP) and the frame pointer (FP) registers. When using -mmem-model=harvard, it also copies .data from program memory into RAM and zeroes out .bss (see Memory models). It then passes control over to main, which has to be defined for the program to be linked successfully.
The default exception handler functions put a code into r0 and halt the processor. This behavior can be overridden when compiling for the cdm target. For more info, see the API reference.
You can add application-specific interrupt handlers using the INTERRUPT_VECTORS macro when using the cdm target.
Note
Due to limitations of Cocas, it not possible to partially overwrite the IVT (i.e. add custom interrupt handlers or replace the default exception handlers) when targeting cdm-cocas.
If you want to use interrupts in a Cocas-based project, you must define a custom IVT in assembly with asect 0 and disable crt0 with -nostartfiles.
The builtins library provides functions that implement various missing hardware instructions. Calls to these functions are emitted by the compiler (e.g., when you use % operator in C). You can disable cdm-builtins to provide your own implementations (e.g., with hardware acceleration).
Currently, cdm-builtins provides:
-
__mulhi3,__mulsi3,__muldi3- multiplication (16, 32, 64-bit); -
__mulosi4- 32-bit multiplication with overflow check; -
__divhi3,__divsi3,__divdi3- signed division (16, 32, 64-bit); -
__udivhi3,__udivsi3,__udivdi3- unsigned division (16, 32, 64-bit); -
__modhi3,__modsi3,__moddi3- signed remainder (16, 32, 64-bit); -
__umodhi3,__umodsi3,__umoddi3- unsigned remainder (16, 32, 64-bit); -
__adddi3,__subdi3,__negdi2- 64-bit addition, subtraction and arithmetic negation.
These functions have the same semantics and signatures as in GCC's libgcc and LLVM's compiler-rt. See the GCC docs for more information.