-
Notifications
You must be signed in to change notification settings - Fork 101
Add LLVM libc sample #538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add LLVM libc sample #538
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
llvmlibc-samples/src/llvmlibc/baremetal-semihosting/Makefile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # | ||
| # Copyright (c) 2020-2024, Arm Limited and affiliates. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
|
|
||
| include ../../../Makefile.conf | ||
|
|
||
| build: hello.elf | ||
|
|
||
| hello.elf: *.c | ||
| ../$(BIN_PATH)/clang --config=llvmlibc.cfg $(MICROBIT_TARGET) -nostartfiles -lsemihost -g -fno-exceptions -fno-rtti -T microbit-llvmlibc.ld -o hello.elf $^ | ||
|
|
||
| %.hex: %.elf | ||
| ../$(BIN_PATH)/llvm-objcopy -O ihex $< $@ | ||
|
|
||
| run: hello.hex | ||
| qemu-system-arm -M microbit -semihosting -nographic -device loader,file=$< | ||
|
|
||
| debug: hello.hex | ||
| qemu-system-arm -M microbit -semihosting -nographic -device loader,file=$< -s -S | ||
|
|
||
| clean: | ||
| rm -f *.elf *.hex | ||
|
|
||
| .PHONY: clean run debug |
41 changes: 41 additions & 0 deletions
41
llvmlibc-samples/src/llvmlibc/baremetal-semihosting/crt0llvmlibc.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| // Copyright (c) 2024, Arm Limited and affiliates. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
|
|
||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <stddef.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <stdint.h> | ||
|
|
||
| extern int main(int argc, char** argv); | ||
|
|
||
| extern void _platform_init(); | ||
|
|
||
| extern char __data_source[]; | ||
| extern char __data_start[]; | ||
| extern char __data_end[]; | ||
| extern char __data_size[]; | ||
| extern char __bss_start[]; | ||
| extern char __bss_end[]; | ||
| extern char __bss_size[]; | ||
| extern char __tls_base[]; | ||
| extern char __tdata_end[]; | ||
| extern char __tls_end[]; | ||
|
|
||
| void _start(void) { | ||
| memcpy(__data_start, __data_source, (size_t) __data_size); | ||
| memset(__bss_start, 0, (size_t) __bss_size); | ||
| _platform_init(); | ||
| _Exit(main(0, NULL)); | ||
| } |
46 changes: 46 additions & 0 deletions
46
llvmlibc-samples/src/llvmlibc/baremetal-semihosting/hello.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright (c) 2024, Arm Limited and affiliates. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
|
|
||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <assert.h> | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include <string.h> | ||
| #include <math.h> | ||
|
|
||
| // Implementation of errno | ||
| int *__llvm_libc_errno() { | ||
| static int internal_err; | ||
| return &internal_err; | ||
| } | ||
|
|
||
| // Example that uses heap, string and math library. | ||
|
|
||
| int main(void) { | ||
| const char *hello_s = "hello "; | ||
| const char *world_s = "world"; | ||
| const size_t hello_s_len = strlen(hello_s); | ||
| const size_t world_s_len = strlen(world_s); | ||
| const size_t out_s_len = hello_s_len + world_s_len + 1; | ||
| char *out_s = (char*) malloc(out_s_len); | ||
| assert(out_s_len >= hello_s_len + 1); | ||
| strncpy(out_s, hello_s, hello_s_len + 1); | ||
| assert(out_s_len >= strlen(out_s) + world_s_len + 1); | ||
| strncat(out_s, world_s, world_s_len + 1); | ||
| // 2024-10-17 Embedded printf implementation does not currently | ||
| // support printing floating point numbers. | ||
| printf("%s %li\n", out_s, lround(400000 * atanf(1.0f))); | ||
| free(out_s); | ||
| return 0; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because you wrote
lroundrather thanlroundf, the float returned fromatanfis being converted into a double, so this code is testing a mix of single and double precision operations.I think that's actually a good thing – it makes it a better test! I point it out because you probably didn't do it on purpose, but I quite like it, and perhaps it's better not to "fix" it :-)