Skip to content

Commit

Permalink
fix : correctly compile a comple test program and issues mul/div emul…
Browse files Browse the repository at this point in the history
…ation functions from compiler-rt runtime.
  • Loading branch information
Vincenzo-Petrolo committed Oct 31, 2024
1 parent 1906bbc commit d0226ac
Show file tree
Hide file tree
Showing 8 changed files with 125 additions and 2 deletions.
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,40 @@ cmake -G "Ninja" \
-DLLVM_ENABLE_RUNTIMES="compiler-rt" \
-DLLVM_ENABLE_THREADS=Off \
../llvm/
```
```

### Building COMPILER-RT
```sh
cmake ../compiler-rt/ \
-G Ninja \
-DCOMPILER_RT_BUILD_BUILTINS=ON \
-DCOMPILER_RT_BUILD_SANITIZERS=OFF \
-DCOMPILER_RT_BUILD_XRAY=OFF \
-DCOMPILER_RT_BUILD_LIBFUZZER=OFF \
-DCOMPILER_RT_BUILD_PROFILE=OFF \
-DCMAKE_C_COMPILER=clang-10 \
-DCMAKE_CXX_COMPILER=clang-10 \
-DCMAKE_AR=../build/bin/llvm-ar \
-DCMAKE_NM=../build/bin/llvm-nm \
-DCMAKE_RANLIB=../build/bin/llvm-ranlib \
-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=lld -nostdlib -Wl,-m,elf32ldlx" \
-DCMAKE_C_COMPILER_TARGET="dlx32-unknown-elf" \
-DCMAKE_ASM_COMPILER_TARGET="dlx32-unknown-elf" \
-DCOMPILER_RT_DEFAULT_TARGET_ONLY=ON \
-DLLVM_CONFIG_PATH=../build/bin/llvm-config \
-DCMAKE_C_FLAGS="-integrated-as" \
-DCMAKE_CXX_FLAGS="-integrated-as" \
-DCOMPILER_RT_USE_PTHREADS=OFF \
-DCOMPILER_RT_BAREMETAL_BUILD=ON
```

### Compiling a simple program
Just compile the program with the following command:
```sh
../build/bin/clang -target dlx32-unknown-elf -integrated-as -O2 -nostartfiles -nostdlib -c main.c -o main
```

Then link it to the library:
```sh
../build/bin/ld.lld main -L ../build-compiler-rt/lib/linux -lclang_rt.builtins-dlx32
```
1 change: 1 addition & 0 deletions compiler-rt/lib/builtins/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ set(GENERIC_SOURCES
muldc3.c
muldf3.c
muldi3.c
mulsi3.c
mulodi4.c
mulosi4.c
muloti4.c
Expand Down
39 changes: 39 additions & 0 deletions compiler-rt/lib/builtins/mulsi3.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/* Generic 32 bit multiply.
Copyright (C) 2009-2024 Free Software Foundation, Inc.
Contributed by Embecosm on behalf of Adapteva, Inc.
This file is part of GCC.
This file is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 3, or (at your option) any
later version.
This file is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */

unsigned int
__mulsi3 (unsigned int a, unsigned int b)
{
unsigned int r = 0;

while (a)
{
if (a & 1)
r += b;
a >>= 1;
b <<= 1;
}
return r;
}
4 changes: 4 additions & 0 deletions lld/ELF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ static bool isCompatible(InputFile *file) {
if (!file->isElf() && !isa<BitcodeFile>(file))
return true;

outs() << "filename: " << file->getName() << "\n";
outs() << "config->ekind: " << config->ekind << "config->emachine: " << config->emachine << "\n";
outs() << "file->ekind: " << file->ekind << "file->emachine: " << file->emachine << "\n";

if (file->ekind == config->ekind && file->emachine == config->emachine) {
if (config->emachine != EM_MIPS)
return true;
Expand Down
Binary file modified tests/a.out
Binary file not shown.
Binary file added tests/main
Binary file not shown.
45 changes: 44 additions & 1 deletion tests/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,55 @@ int acc(int n)
return tmp;
}

int partition(int *arr, int low, int high)
{
int pivot = arr[high];
int i = low - 1;

for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}

int tmp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = tmp;

return i + 1;
}

int quick_sort(int *arr, int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);

quick_sort(arr, low, pi - 1);
quick_sort(arr, pi + 1, high);
}
}

unsigned div(unsigned a, unsigned b)
{
return a / b;
}

int main(void)
{
int a = 2;
int b = 1;

return acc(a) + acc(b);
int array [5] = {5, 4, 3, 2, 1};

quick_sort(array, 0, 4);

return div((unsigned) (acc(a) + acc(b)), (unsigned)array[0]);
}


Expand Down
Binary file modified tests/main.o
Binary file not shown.

0 comments on commit d0226ac

Please # to comment.