Modulinos: Difference between revisions

Fixed LLVM
(Simplified compilation process)
(Fixed LLVM)
Line 320:
 
=={{header|LLVM}}==
LLVM hascan have scripted main bya la C, using the weak defaultattribute.
 
<lang sh>$ make
<lang llvm>@msg_directory = internal constant [15 x i8] c"Directory: %s\0A\00"
llvm-as scriptedmain.ll
@msg_program = internal constant [13 x i8] c"Program: %s\0A\00"
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 i32 @printf(i8* noalias nocapture, ...)
declare i8* @getcwd(i8*, i32)
 
<lang make>EXECUTABLE_SM=scriptedmain
define i32 @main(i32 %argc, i8** %argv) {
EXECUTABLE_TEST=test
%cwd = alloca [1024 x i8]
 
all: test.ll scriptedmain.s
%cwd_ptr = getelementptr inbounds [1024 x i8]* %cwd, i32 0, i32 0
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:
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([15 x i8]* @msg_directory, i32 0, i32 0), i8* %cwd_ptr)
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
 
<lang llvm>@msg_directorymsg_main = internal constant [1533 x i8] c"DirectoryMain: The meaning of life is %sd\0A\00"
%program = load i8** %program_ptr
 
declare i32 @printf(i8* noalias nocapture, ...)
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
 
%cwd_ptrcall =i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([102433 x i8]* %cwd@msg_main, i32 0, i32 0), i32 %meaning)
for_args:
 
%i_val = load ret i32* %i0
}</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)
 
<lang llvm>@msg_programmsg_test = internal constant [1333 x i8] c"ProgramTest: The meaning of life is %sd\0A\00"
%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()
 
define i32 @main(i32 %argc, i8** %argv) {
%meaning = call i32 @meaning_of_life()
 
call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([1533 x i8]* @msg_directorymsg_test, i32 0, i32 0), i8*i32 %cwd_ptrmeaning)
end_for_args:
 
ret i32 0
Anonymous user