Add LLVM
Signed-off-by: Slendi <slendi@socopon.com>
This commit is contained in:
parent
9d7f79e576
commit
ed1491d6dd
@ -4,42 +4,79 @@ import "core:fmt"
|
|||||||
import "core:os"
|
import "core:os"
|
||||||
|
|
||||||
main :: proc() {
|
main :: proc() {
|
||||||
handle: os.Handle
|
ctx := LLVMContextCreate()
|
||||||
if len(os.args) >= 2 {
|
defer LLVMContextDispose(ctx)
|
||||||
errno: os.Errno
|
module := LLVMModuleCreateWithNameInContext("hello", ctx)
|
||||||
handle, errno = os.open(os.args[1])
|
defer LLVMDisposeModule(module)
|
||||||
if errno != 0 {
|
builder := LLVMCreateBuilderInContext(ctx)
|
||||||
fmt.printf("Error opening file\n", errno)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
handle = os.stdin
|
|
||||||
}
|
|
||||||
defer os.close(handle)
|
|
||||||
|
|
||||||
data, err := os.read_entire_file_from_handle(handle)
|
int_8_type := LLVMInt8TypeInContext(ctx)
|
||||||
if !err {
|
int_8_type_ptr := LLVMPointerType(int_8_type, 0)
|
||||||
fmt.printf("Error reading file\n", err)
|
int_32_type := LLVMInt32TypeInContext(ctx)
|
||||||
return
|
|
||||||
|
puts_function_args_type := []LLVMTypeRef{
|
||||||
|
int_8_type_ptr,
|
||||||
}
|
}
|
||||||
|
|
||||||
u8_arr : [dynamic]u8
|
puts_function_type := LLVMFunctionType(int_32_type, raw_data(puts_function_args_type), 1, LLVMBool(0))
|
||||||
for ch in data {
|
puts_function := LLVMAddFunction(module, "puts", puts_function_type)
|
||||||
append(&u8_arr, u8(ch))
|
|
||||||
|
main_function_type := LLVMFunctionType(int_32_type, nil, 0, LLVMBool(0))
|
||||||
|
main_function := LLVMAddFunction(module, "main", main_function_type)
|
||||||
|
|
||||||
|
entry := LLVMAppendBasicBlockInContext(ctx, main_function, "entry")
|
||||||
|
LLVMPositionBuilderAtEnd(builder, entry)
|
||||||
|
|
||||||
|
puts_function_args := []LLVMValueRef {
|
||||||
|
LLVMBuildPointerCast(
|
||||||
|
builder,
|
||||||
|
LLVMBuildGlobalString(builder, "Hello world!\n", "hello"),
|
||||||
|
int_8_type_ptr,
|
||||||
|
"0",
|
||||||
|
),
|
||||||
}
|
}
|
||||||
|
|
||||||
lexer := lexer_create(&u8_arr)
|
LLVMBuildCall2(builder, puts_function_type, puts_function, raw_data(puts_function_args), len(puts_function_args), "i")
|
||||||
parser := parser_create(lexer)
|
LLVMBuildRet(builder, LLVMConstInt(int_32_type, 0, LLVMBool(0)))
|
||||||
|
|
||||||
ast := parser_parse(&parser)
|
LLVMPrintModuleToFile(module, "hello.ll", nil)
|
||||||
type_check(ast, nil)
|
|
||||||
if len(g_message_list) > 0 {
|
//handle: os.Handle
|
||||||
for msg in g_message_list {
|
//if len(os.args) >= 2 {
|
||||||
fmt.printf("%s\n", msg)
|
// errno: os.Errno
|
||||||
}
|
// handle, errno = os.open(os.args[1])
|
||||||
return
|
// if errno != 0 {
|
||||||
}
|
// fmt.printf("Error opening file\n", errno)
|
||||||
|
// return
|
||||||
node_print(ast)
|
// }
|
||||||
|
//} else {
|
||||||
|
// handle = os.stdin
|
||||||
|
//}
|
||||||
|
//defer os.close(handle)
|
||||||
|
|
||||||
|
//data, err := os.read_entire_file_from_handle(handle)
|
||||||
|
//if !err {
|
||||||
|
// fmt.printf("Error reading file\n", err)
|
||||||
|
// return
|
||||||
|
//}
|
||||||
|
|
||||||
|
//u8_arr : [dynamic]u8
|
||||||
|
//for ch in data {
|
||||||
|
// append(&u8_arr, u8(ch))
|
||||||
|
//}
|
||||||
|
|
||||||
|
//lexer := lexer_create(&u8_arr)
|
||||||
|
//parser := parser_create(lexer)
|
||||||
|
|
||||||
|
//ast := parser_parse(&parser)
|
||||||
|
//type_check(ast, nil)
|
||||||
|
//if len(g_message_list) > 0 {
|
||||||
|
// for msg in g_message_list {
|
||||||
|
// fmt.printf("%s\n", msg)
|
||||||
|
// }
|
||||||
|
// return
|
||||||
|
//}
|
||||||
|
|
||||||
|
//node_print(ast)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user