Skip to content

Instantly share code, notes, and snippets.

@waywardmonkeys
Created January 28, 2015 08:43
Show Gist options
  • Save waywardmonkeys/1858db68bf612ebc5e1d to your computer and use it in GitHub Desktop.
Save waywardmonkeys/1858db68bf612ebc5e1d to your computer and use it in GitHub Desktop.
batavia:sources bruce (master) $ ./main
batavia:sources bruce (master) $ echo $?
1
batavia:sources bruce (master) $ ./main 2 3 4
batavia:sources bruce (master) $ echo $?
4
batavia:sources bruce (master) $ ls -alt main
-rwxr-xr-x 1 bruce staff 4312 Jan 28 15:33 main
Module: lb
Synopsis:
Author:
Copyright:
define function llvm-pointer-to (type :: <llvm-type>)
=> (pointer-type :: <llvm-pointer-type>)
let type = llvm-type-forward(type);
make(<llvm-pointer-type>, pointee: type);
end;
define function main (name :: <string>, arguments :: <vector>)
let b = make(<llvm-builder>);
let m = make(<llvm-module>, name: "_main");
b.llvm-builder-module := m;
// Compute types
let argc-type = $llvm-i32-type;
let argv-type = llvm-pointer-to($llvm-i8*-type);
let main-function-type
= make(<llvm-function-type>,
return-type: $llvm-i32-type,
parameter-types: vector(argc-type, argv-type),
varargs?: #f);
let main-function-ptr-type = llvm-pointer-to(main-function-type);
// Generate argument values
let main-argc
= make(<llvm-argument>, type: argc-type, name: "argc", index: 0);
let main-argv
= make(<llvm-argument>, type: argv-type, name: "argv", index: 1);
b.llvm-builder-function
:= make(<llvm-function>,
name: "main",
type: main-function-ptr-type,
arguments: vector(main-argc, main-argv),
linkage: #"external");
ins--local(b, main-argc.llvm-argument-name, main-argc);
ins--local(b, main-argv.llvm-argument-name, main-argv);
ins--block(b, make(<llvm-basic-block>, name: "bb.entry"));
ins--ret(b, main-argc);
llvm-builder-define-global(b, "main", b.llvm-builder-function);
// cleanup
b.llvm-builder-function := #f;
llvm-save-bitcode-file(m, "main.bc");
// done
b.llvm-builder-module := #f;
exit-application(0);
end function main;
main(application-name(), application-arguments());
; ModuleID = 'main.bc'
define i32 @main(i32 %argc, i8** %argv) {
bb.entry:
ret i32 %argc
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment