Modulinos: Difference between revisions
Content added Content deleted
(Simplified compilation process) |
(Fixed LLVM) |
||
Line 320: | Line 320: | ||
=={{header|LLVM}}== |
=={{header|LLVM}}== |
||
LLVM |
LLVM can have scripted main a la C, using the weak attribute. |
||
<lang sh>$ make |
|||
⚫ | |||
llvm-as scriptedmain.ll |
|||
⚫ | |||
llc scriptedmain.bc |
|||
@msg_argc = internal constant [20 x i8] c"Number of Args: %d\0A\00" |
|||
gcc -o scriptedmain scriptedmain.s |
|||
@msg_arg = internal constant [10 x i8] c"Arg = %s\0A\00" |
|||
./scriptedmain |
|||
Main: The meaning of life is 42 |
|||
llvm-as test.ll |
|||
llc test.bc |
|||
gcc -o test test.s scriptedmain.s |
|||
./test |
|||
Test: The meaning of life is 42</lang> |
|||
Makefile |
|||
⚫ | |||
declare i8* @getcwd(i8*, i32) |
|||
<lang make>EXECUTABLE_SM=scriptedmain |
|||
⚫ | |||
EXECUTABLE_TEST=test |
|||
%cwd = alloca [1024 x i8] |
|||
all: test.ll scriptedmain.s |
|||
⚫ | |||
llvm-as test.ll |
|||
llc test.bc |
|||
gcc -o $(EXECUTABLE_TEST) test.s scriptedmain.s |
|||
./$(EXECUTABLE_TEST) |
|||
scriptedmain.s: scriptedmain.ll |
|||
call i8* @getcwd(i8* %cwd_ptr, i32 1024) |
|||
llvm-as scriptedmain.ll |
|||
llc scriptedmain.bc |
|||
gcc -o $(EXECUTABLE_SM) scriptedmain.s |
|||
./$(EXECUTABLE_SM) |
|||
clean: |
|||
⚫ | |||
rm $(EXECUTABLE_TEST) |
|||
rm $(EXECUTABLE_SM) |
|||
rm test.s |
|||
rm test.bc |
|||
rm scriptedmain.s |
|||
rm scriptedmain.bc</lang> |
|||
scriptedmain.ll |
|||
%program_ptr = getelementptr inbounds i8** %argv, i32 0 |
|||
⚫ | |||
%program = load i8** %program_ptr |
|||
⚫ | |||
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([13 x i8]* @msg_program, i32 0, i32 0), i8* %program) |
|||
define i32 @meaning_of_life() { |
|||
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([20 x i8]* @msg_argc, i32 0, i32 0), i32 %argc) |
|||
ret i32 42 |
|||
} |
|||
define weak i32 @main(i32 %argc, i8** %argv) { |
|||
%i = alloca i32 |
|||
%meaning = call i32 @meaning_of_life() |
|||
store i32 0, i32* %i |
|||
br label %for_args |
|||
⚫ | |||
for_args: |
|||
ret i32 0 |
|||
}</lang> |
|||
test.ll |
|||
%arg_ptr = getelementptr inbounds i8** %argv, i32 %i_val |
|||
%arg = load i8** %arg_ptr |
|||
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([10 x i8]* @msg_arg, i32 0, i32 0), i8* %arg) |
|||
⚫ | |||
%new_i_val = add i32 %i_val, 1 |
|||
store i32 %new_i_val, i32* %i |
|||
declare i32 @printf(i8* noalias nocapture, ...) |
|||
%more_args = icmp slt i32 %new_i_val, %argc |
|||
br i1 %more_args, label %for_args, label %end_for_args |
|||
declare i32 @meaning_of_life() |
|||
⚫ | |||
%meaning = call i32 @meaning_of_life() |
|||
⚫ | |||
end_for_args: |
|||
ret i32 0 |
ret i32 0 |