I wanted a bare-bones example, using only the Win32 API, without a CRT dependency. Here it is:
@.str = private unnamed_addr constant [13 x i8] c"Hello world\0A\00"Compile and link steps:
declare i64 @GetStdHandle(i32) nounwind
declare i32 @WriteFile(i64, i8* nocapture, i32, i32* nocapture, i8*) nounwind
define i32 @Entry() {
%hwStr = getelementptr [13 x i8]* @.str, i64 0, i64 0
%handle = call i64 @GetStdHandle(i32 -11)
%bytes = alloca i32
call i32 @WriteFile(i64 %handle, i8* %hwStr, i32 12, i32* %bytes, i8* null)
ret i32 2
}
llc -filetype=obj hw.llThe resulting executable has 2,560 bytes, prints "Hello world", and returns exit code 2.
link hw.obj kernel32.lib /entry:Entry /subsystem:console
My main observation is that the language is clumsy. It's meant to be read by humans, but in most cases, generated automatically:
- No syntax for hexadecimal integers. If you want to enter a value like 0xC0000005 – good luck! :-)
- The types of all values have to be explicitly qualified each time they're used. This is irritating when writing, but may serve as useful documentation when reading.
- Interestingly, there's no "address-of" operator. Instead,
alloca
creates space for a value on the stack, and gives you the address. Neat!
This post does not yet have any comments.