authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 19:07:33-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-11-24 19:07:33-07:00
logca836191e199603056fc2c5f854f8e134f98af4d
tree2ecd475ca1d2fca0822246433886636f364551df
parentbaf5167171fa0159adb8bd55c6a111b006f8c038

debug information for functions


9 files changed, 258 insertions(+), 96 deletions(-)

src/buffer.hpp-17
...@@ -134,23 +134,6 @@ static inline void buf_splice_buf(Buf *buf, int start, int end, Buf *other) {...@@ -134,23 +134,6 @@ static inline void buf_splice_buf(Buf *buf, int start, int end, Buf *other) {
134 memcpy(buf_ptr(buf) + start, buf_ptr(other), buf_len(other));134 memcpy(buf_ptr(buf) + start, buf_ptr(other), buf_len(other));
135}135}
136136
137// TODO this method needs work
138static inline Buf *buf_dirname(Buf *buf) {
139 if (buf_len(buf) <= 2)
140 zig_panic("TODO buf_dirname small");
141 int last_index = buf_len(buf) - 1;
142 if (buf_ptr(buf)[buf_len(buf) - 1] == '/') {
143 last_index = buf_len(buf) - 2;
144 }
145 for (int i = last_index; i >= 0; i -= 1) {
146 uint8_t c = buf_ptr(buf)[i];
147 if (c == '/') {
148 return buf_slice(buf, 0, i);
149 }
150 }
151 return buf_create_from_mem("", 0);
152}
153
154static inline uint32_t buf_hash(Buf *buf) {137static inline uint32_t buf_hash(Buf *buf) {
155 // FNV 32-bit hash138 // FNV 32-bit hash
156 uint32_t h = 2166136261;139 uint32_t h = 2166136261;
src/codegen.cpp+227-67
...@@ -9,27 +9,63 @@...@@ -9,27 +9,63 @@
9#include "hash_map.hpp"9#include "hash_map.hpp"
10#include "zig_llvm.hpp"10#include "zig_llvm.hpp"
11#include "os.hpp"11#include "os.hpp"
12#include "config.h"
1213
13#include <stdio.h>14#include <stdio.h>
1415
16#include <llvm/IR/DIBuilder.h>
17#include <llvm/IR/DiagnosticInfo.h>
18#include <llvm/IR/DiagnosticPrinter.h>
19
15struct FnTableEntry {20struct FnTableEntry {
16 LLVMValueRef fn_value;21 LLVMValueRef fn_value;
17 AstNode *proto_node;22 AstNode *proto_node;
18};23};
1924
25enum TypeId {
26 TypeIdUserDefined,
27 TypeIdPointer,
28 TypeIdU8,
29 TypeIdI32,
30 TypeIdVoid,
31 TypeIdUnreachable,
32};
33
34struct TypeTableEntry {
35 TypeId id;
36 LLVMTypeRef type_ref;
37 llvm::DIType *di_type;
38
39 TypeTableEntry *pointer_child;
40 bool pointer_is_const;
41 int user_defined_id;
42 Buf name;
43 TypeTableEntry *pointer_const_parent;
44 TypeTableEntry *pointer_mut_parent;
45};
46
20struct CodeGen {47struct CodeGen {
21 LLVMModuleRef mod;48 LLVMModuleRef mod;
22 AstNode *root;49 AstNode *root;
23 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> fn_defs;50 HashMap<Buf *, AstNode *, buf_hash, buf_eql_buf> fn_defs;
24 ZigList<ErrorMsg> errors;51 ZigList<ErrorMsg> errors;
25 LLVMBuilderRef builder;52 LLVMBuilderRef builder;
53 llvm::DIBuilder *dbuilder;
54 llvm::DICompileUnit *compile_unit;
26 HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;55 HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;
27 HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table;56 HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table;
57 HashMap<Buf *, TypeTableEntry *, buf_hash, buf_eql_buf> type_table;
58 TypeTableEntry *invalid_type_entry;
59 LLVMTargetDataRef target_data_ref;
60 unsigned pointer_size_bytes;
61 bool is_static;
62 LLVMTargetMachineRef target_machine;
63 Buf in_file;
64 Buf in_dir;
28};65};
2966
30struct TypeNode {67struct TypeNode {
31 LLVMTypeRef type_ref;68 TypeTableEntry *entry;
32 bool is_unreachable;
33};69};
3470
35struct CodeGenNode {71struct CodeGenNode {
...@@ -38,12 +74,16 @@ struct CodeGenNode {...@@ -38,12 +74,16 @@ struct CodeGenNode {
38 } data;74 } data;
39};75};
4076
41CodeGen *create_codegen(AstNode *root) {77CodeGen *create_codegen(AstNode *root, bool is_static, Buf *in_full_path) {
42 CodeGen *g = allocate<CodeGen>(1);78 CodeGen *g = allocate<CodeGen>(1);
43 g->root = root;79 g->root = root;
44 g->fn_defs.init(32);80 g->fn_defs.init(32);
45 g->fn_table.init(32);81 g->fn_table.init(32);
46 g->str_table.init(32);82 g->str_table.init(32);
83 g->type_table.init(32);
84 g->is_static = is_static;
85
86 os_path_split(in_full_path, &g->in_dir, &g->in_file);
47 return g;87 return g;
48}88}
4989
...@@ -60,9 +100,17 @@ static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) {...@@ -60,9 +100,17 @@ static void add_node_error(CodeGen *g, AstNode *node, Buf *msg) {
60static LLVMTypeRef to_llvm_type(AstNode *type_node) {100static LLVMTypeRef to_llvm_type(AstNode *type_node) {
61 assert(type_node->type == NodeTypeType);101 assert(type_node->type == NodeTypeType);
62 assert(type_node->codegen_node);102 assert(type_node->codegen_node);
63 assert(type_node->codegen_node->data.type_node.type_ref);103 assert(type_node->codegen_node->data.type_node.entry);
104
105 return type_node->codegen_node->data.type_node.entry->type_ref;
106}
107
108static llvm::DIType *to_llvm_debug_type(AstNode *type_node) {
109 assert(type_node->type == NodeTypeType);
110 assert(type_node->codegen_node);
111 assert(type_node->codegen_node->data.type_node.entry);
64112
65 return type_node->codegen_node->data.type_node.type_ref;113 return type_node->codegen_node->data.type_node.entry->di_type;
66}114}
67115
68116
...@@ -72,6 +120,56 @@ static bool type_is_unreachable(AstNode *type_node) {...@@ -72,6 +120,56 @@ static bool type_is_unreachable(AstNode *type_node) {
72 buf_eql_str(&type_node->data.type.primitive_name, "unreachable");120 buf_eql_str(&type_node->data.type.primitive_name, "unreachable");
73}121}
74122
123static void analyze_node(CodeGen *g, AstNode *node);
124
125static void resolve_type_and_recurse(CodeGen *g, AstNode *node) {
126 assert(!node->codegen_node);
127 node->codegen_node = allocate<CodeGenNode>(1);
128 TypeNode *type_node = &node->codegen_node->data.type_node;
129 switch (node->data.type.type) {
130 case AstNodeTypeTypePrimitive:
131 {
132 Buf *name = &node->data.type.primitive_name;
133 auto table_entry = g->type_table.maybe_get(name);
134 if (table_entry) {
135 type_node->entry = table_entry->value;
136 } else {
137 add_node_error(g, node,
138 buf_sprintf("invalid type name: '%s'", buf_ptr(name)));
139 type_node->entry = g->invalid_type_entry;
140 }
141 break;
142 }
143 case AstNodeTypeTypePointer:
144 {
145 analyze_node(g, node->data.type.child_type);
146 TypeNode *child_type_node = &node->data.type.child_type->codegen_node->data.type_node;
147 if (child_type_node->entry->id == TypeIdUnreachable) {
148 add_node_error(g, node,
149 buf_create_from_str("pointer to unreachable not allowed"));
150 }
151 TypeTableEntry **parent_pointer = node->data.type.is_const ?
152 &child_type_node->entry->pointer_const_parent :
153 &child_type_node->entry->pointer_mut_parent;
154 const char *const_or_mut_str = node->data.type.is_const ? "const" : "mut";
155 if (*parent_pointer) {
156 type_node->entry = *parent_pointer;
157 } else {
158 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
159 entry->id = TypeIdPointer;
160 entry->type_ref = LLVMPointerType(child_type_node->entry->type_ref, 0);
161 buf_appendf(&entry->name, "*%s %s", const_or_mut_str, buf_ptr(&child_type_node->entry->name));
162 entry->di_type = g->dbuilder->createPointerType(child_type_node->entry->di_type,
163 g->pointer_size_bytes * 8, g->pointer_size_bytes * 8, buf_ptr(&entry->name));
164 g->type_table.put(&entry->name, entry);
165 type_node->entry = entry;
166 *parent_pointer = entry;
167 }
168 break;
169 }
170 }
171}
172
75static void analyze_node(CodeGen *g, AstNode *node) {173static void analyze_node(CodeGen *g, AstNode *node) {
76 switch (node->type) {174 switch (node->type) {
77 case NodeTypeRoot:175 case NodeTypeRoot:
...@@ -148,42 +246,10 @@ static void analyze_node(CodeGen *g, AstNode *node) {...@@ -148,42 +246,10 @@ static void analyze_node(CodeGen *g, AstNode *node) {
148 case NodeTypeParamDecl:246 case NodeTypeParamDecl:
149 analyze_node(g, node->data.param_decl.type);247 analyze_node(g, node->data.param_decl.type);
150 break;248 break;
249
151 case NodeTypeType:250 case NodeTypeType:
152 {251 {
153 node->codegen_node = allocate<CodeGenNode>(1);252 resolve_type_and_recurse(g, node);
154 TypeNode *type_node = &node->codegen_node->data.type_node;
155 switch (node->data.type.type) {
156 case AstNodeTypeTypePrimitive:
157 {
158 Buf *name = &node->data.type.primitive_name;
159 if (buf_eql_str(name, "u8")) {
160 type_node->type_ref = LLVMInt8Type();
161 } else if (buf_eql_str(name, "i32")) {
162 type_node->type_ref = LLVMInt32Type();
163 } else if (buf_eql_str(name, "void")) {
164 type_node->type_ref = LLVMVoidType();
165 } else if (buf_eql_str(name, "unreachable")) {
166 type_node->type_ref = LLVMVoidType();
167 type_node->is_unreachable = true;
168 } else {
169 add_node_error(g, node,
170 buf_sprintf("invalid type name: '%s'", buf_ptr(name)));
171 type_node->type_ref = LLVMVoidType();
172 }
173 break;
174 }
175 case AstNodeTypeTypePointer:
176 {
177 analyze_node(g, node->data.type.child_type);
178 TypeNode *child_type_node = &node->data.type.child_type->codegen_node->data.type_node;
179 if (child_type_node->is_unreachable) {
180 add_node_error(g, node,
181 buf_create_from_str("pointer to unreachable not allowed"));
182 }
183 type_node->type_ref = LLVMPointerType(child_type_node->type_ref, 0);
184 break;
185 }
186 }
187 break;253 break;
188 }254 }
189 case NodeTypeBlock:255 case NodeTypeBlock:
...@@ -224,10 +290,85 @@ static void analyze_node(CodeGen *g, AstNode *node) {...@@ -224,10 +290,85 @@ static void analyze_node(CodeGen *g, AstNode *node) {
224 }290 }
225}291}
226292
293static void add_types(CodeGen *g) {
294 {
295 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
296 entry->id = TypeIdU8;
297 entry->type_ref = LLVMInt8Type();
298 buf_init_from_str(&entry->name, "u8");
299 entry->di_type = g->dbuilder->createBasicType(buf_ptr(&entry->name), 8, 8, llvm::dwarf::DW_ATE_unsigned);
300 g->type_table.put(&entry->name, entry);
301 }
302 {
303 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
304 entry->id = TypeIdI32;
305 entry->type_ref = LLVMInt32Type();
306 buf_init_from_str(&entry->name, "i32");
307 entry->di_type = g->dbuilder->createBasicType(buf_ptr(&entry->name), 32, 32,
308 llvm::dwarf::DW_ATE_signed);
309 g->type_table.put(&entry->name, entry);
310 }
311 {
312 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
313 entry->id = TypeIdVoid;
314 entry->type_ref = LLVMVoidType();
315 buf_init_from_str(&entry->name, "void");
316 entry->di_type = g->dbuilder->createBasicType(buf_ptr(&entry->name), 0, 0,
317 llvm::dwarf::DW_ATE_unsigned);
318 g->type_table.put(&entry->name, entry);
319
320 // invalid types are void
321 g->invalid_type_entry = entry;
322 }
323 {
324 TypeTableEntry *entry = allocate<TypeTableEntry>(1);
325 entry->id = TypeIdUnreachable;
326 entry->type_ref = LLVMVoidType();
327 buf_init_from_str(&entry->name, "unreachable");
328 entry->di_type = g->invalid_type_entry->di_type;
329 g->type_table.put(&entry->name, entry);
330 }
331}
332
227333
228void semantic_analyze(CodeGen *g) {334void semantic_analyze(CodeGen *g) {
335 LLVMInitializeAllTargets();
336 LLVMInitializeAllTargetMCs();
337 LLVMInitializeAllAsmPrinters();
338 LLVMInitializeAllAsmParsers();
339 LLVMInitializeNativeTarget();
340
341 char *native_triple = LLVMGetDefaultTargetTriple();
342
343 LLVMTargetRef target_ref;
344 char *err_msg = nullptr;
345 if (LLVMGetTargetFromTriple(native_triple, &target_ref, &err_msg)) {
346 zig_panic("unable to get target from triple: %s", err_msg);
347 }
348
349 char *native_cpu = LLVMZigGetHostCPUName();
350 char *native_features = LLVMZigGetNativeFeatures();
351
352 LLVMCodeGenOptLevel opt_level = LLVMCodeGenLevelNone;
353
354 LLVMRelocMode reloc_mode = g->is_static ? LLVMRelocStatic : LLVMRelocPIC;
355
356 g->target_machine = LLVMCreateTargetMachine(target_ref, native_triple,
357 native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault);
358
359 g->target_data_ref = LLVMGetTargetMachineData(g->target_machine);
360
361
229 g->mod = LLVMModuleCreateWithName("ZigModule");362 g->mod = LLVMModuleCreateWithName("ZigModule");
230363
364 g->pointer_size_bytes = LLVMPointerSize(g->target_data_ref);
365
366 g->builder = LLVMCreateBuilder();
367 g->dbuilder = new llvm::DIBuilder(*llvm::unwrap(g->mod), true);
368
369
370 add_types(g);
371
231 // Pass 1.372 // Pass 1.
232 analyze_node(g, g->root);373 analyze_node(g, g->root);
233}374}
...@@ -344,8 +485,29 @@ static void gen_block(CodeGen *g, AstNode *block_node) {...@@ -344,8 +485,29 @@ static void gen_block(CodeGen *g, AstNode *block_node) {
344 }485 }
345}486}
346487
488static llvm::DISubroutineType *create_di_function_type(CodeGen *g, AstNodeFnProto *fn_proto, llvm::DIFile *unit) {
489 llvm::SmallVector<llvm::Metadata *, 8> types;
490
491 llvm::DIType *return_type = to_llvm_debug_type(fn_proto->return_type);
492 types.push_back(return_type);
493
494 for (int i = 0; i < fn_proto->params.length; i += 1) {
495 AstNode *param_node = fn_proto->params.at(i);
496 llvm::DIType *param_type = to_llvm_debug_type(param_node);
497 types.push_back(param_type);
498 }
499
500 return g->dbuilder->createSubroutineType(unit, g->dbuilder->getOrCreateTypeArray(types));
501}
502
347void code_gen(CodeGen *g) {503void code_gen(CodeGen *g) {
348 g->builder = LLVMCreateBuilder();504 Buf *producer = buf_sprintf("zig %s", ZIG_VERSION_STRING);
505 bool is_optimized = false;
506 const char *flags = "";
507 unsigned runtime_version = 0;
508 g->compile_unit = g->dbuilder->createCompileUnit(llvm::dwarf::DW_LANG_C99,
509 buf_ptr(&g->in_file), buf_ptr(&g->in_dir),
510 buf_ptr(producer), is_optimized, flags, runtime_version);
349511
350 auto it = g->fn_defs.entry_iterator();512 auto it = g->fn_defs.entry_iterator();
351 for (;;) {513 for (;;) {
...@@ -369,9 +531,29 @@ void code_gen(CodeGen *g) {...@@ -369,9 +531,29 @@ void code_gen(CodeGen *g) {
369 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, fn_proto->params.length, 0);531 LLVMTypeRef function_type = LLVMFunctionType(ret_type, param_types, fn_proto->params.length, 0);
370 LLVMValueRef fn = LLVMAddFunction(g->mod, buf_ptr(&fn_proto->name), function_type);532 LLVMValueRef fn = LLVMAddFunction(g->mod, buf_ptr(&fn_proto->name), function_type);
371533
534 bool internal_linkage = false;
535 LLVMSetLinkage(fn, internal_linkage ? LLVMPrivateLinkage : LLVMExternalLinkage);
536
372 if (type_is_unreachable(fn_proto->return_type)) {537 if (type_is_unreachable(fn_proto->return_type)) {
373 LLVMAddFunctionAttr(fn, LLVMNoReturnAttribute);538 LLVMAddFunctionAttr(fn, LLVMNoReturnAttribute);
374 }539 }
540 LLVMAddFunctionAttr(fn, LLVMNoUnwindAttribute);
541
542 // Add debug info.
543 llvm::DIFile *unit = g->dbuilder->createFile(g->compile_unit->getFilename(),
544 g->compile_unit->getDirectory());
545 llvm::DIScope *fn_scope = unit;
546 unsigned line_number = fn_def_node->line + 1;
547 unsigned scope_line = line_number;
548 bool is_definition = true;
549 unsigned flags = 0;
550 llvm::Function *unwrapped_function = reinterpret_cast<llvm::Function*>(llvm::unwrap(fn));
551 g->dbuilder->createFunction(
552 fn_scope, buf_ptr(&fn_proto->name), "", unit, line_number,
553 create_di_function_type(g, fn_proto, unit), internal_linkage,
554 is_definition, scope_line, flags, is_optimized, unwrapped_function);
555
556
375557
376 LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn, "entry");558 LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn, "entry");
377 LLVMPositionBuilderAtEnd(g->builder, entry_block);559 LLVMPositionBuilderAtEnd(g->builder, entry_block);
...@@ -379,6 +561,8 @@ void code_gen(CodeGen *g) {...@@ -379,6 +561,8 @@ void code_gen(CodeGen *g) {
379 gen_block(g, fn_def->body);561 gen_block(g, fn_def->body);
380 }562 }
381563
564 g->dbuilder->finalize();
565
382 LLVMDumpModule(g->mod);566 LLVMDumpModule(g->mod);
383567
384 char *error = nullptr;568 char *error = nullptr;
...@@ -390,14 +574,7 @@ ZigList<ErrorMsg> *codegen_error_messages(CodeGen *g) {...@@ -390,14 +574,7 @@ ZigList<ErrorMsg> *codegen_error_messages(CodeGen *g) {
390}574}
391575
392576
393void code_gen_link(CodeGen *g, bool is_static, const char *out_file) {577void code_gen_link(CodeGen *g, const char *out_file) {
394 LLVMInitializeAllTargets();
395 LLVMInitializeAllTargetMCs();
396 LLVMInitializeAllAsmPrinters();
397 LLVMInitializeAllAsmParsers();
398 LLVMInitializeNativeTarget();
399
400
401 LLVMPassRegistryRef registry = LLVMGetGlobalPassRegistry();578 LLVMPassRegistryRef registry = LLVMGetGlobalPassRegistry();
402 LLVMInitializeCore(registry);579 LLVMInitializeCore(registry);
403 LLVMInitializeCodeGen(registry);580 LLVMInitializeCodeGen(registry);
...@@ -405,29 +582,12 @@ void code_gen_link(CodeGen *g, bool is_static, const char *out_file) {...@@ -405,29 +582,12 @@ void code_gen_link(CodeGen *g, bool is_static, const char *out_file) {
405 LLVMZigInitializeLowerIntrinsicsPass(registry);582 LLVMZigInitializeLowerIntrinsicsPass(registry);
406 LLVMZigInitializeUnreachableBlockElimPass(registry);583 LLVMZigInitializeUnreachableBlockElimPass(registry);
407584
408 char *native_triple = LLVMGetDefaultTargetTriple();
409
410 LLVMTargetRef target_ref;
411 char *err_msg = nullptr;
412 if (LLVMGetTargetFromTriple(native_triple, &target_ref, &err_msg)) {
413 zig_panic("unable to get target from triple: %s", err_msg);
414 }
415
416 char *native_cpu = LLVMZigGetHostCPUName();
417 char *native_features = LLVMZigGetNativeFeatures();
418
419 LLVMCodeGenOptLevel opt_level = LLVMCodeGenLevelNone;
420
421 LLVMRelocMode reloc_mode = is_static ? LLVMRelocStatic : LLVMRelocPIC;
422
423 LLVMTargetMachineRef target_machine = LLVMCreateTargetMachine(target_ref, native_triple,
424 native_cpu, native_features, opt_level, reloc_mode, LLVMCodeModelDefault);
425
426 Buf out_file_o = BUF_INIT;585 Buf out_file_o = BUF_INIT;
427 buf_init_from_str(&out_file_o, out_file);586 buf_init_from_str(&out_file_o, out_file);
428 buf_append_str(&out_file_o, ".o");587 buf_append_str(&out_file_o, ".o");
429588
430 if (LLVMTargetMachineEmitToFile(target_machine, g->mod, buf_ptr(&out_file_o), LLVMObjectFile, &err_msg)) {589 char *err_msg = nullptr;
590 if (LLVMTargetMachineEmitToFile(g->target_machine, g->mod, buf_ptr(&out_file_o), LLVMObjectFile, &err_msg)) {
431 zig_panic("unable to write object file: %s", err_msg);591 zig_panic("unable to write object file: %s", err_msg);
432 }592 }
433593
src/codegen.hpp+2-2
...@@ -21,13 +21,13 @@ struct ErrorMsg {...@@ -21,13 +21,13 @@ struct ErrorMsg {
21};21};
2222
2323
24CodeGen *create_codegen(AstNode *root);24CodeGen *create_codegen(AstNode *root, bool is_static, Buf *in_file);
2525
26void semantic_analyze(CodeGen *g);26void semantic_analyze(CodeGen *g);
2727
28void code_gen(CodeGen *g);28void code_gen(CodeGen *g);
2929
30void code_gen_link(CodeGen *g, bool is_static, const char *out_file);30void code_gen_link(CodeGen *g, const char *out_file);
3131
32ZigList<ErrorMsg> *codegen_error_messages(CodeGen *g);32ZigList<ErrorMsg> *codegen_error_messages(CodeGen *g);
3333
src/main.cpp+3-6
...@@ -62,18 +62,15 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi...@@ -62,18 +62,15 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi
62 return usage(arg0);62 return usage(arg0);
6363
64 FILE *in_f;64 FILE *in_f;
65 Buf *cur_dir_path;
66 if (strcmp(in_file, "-") == 0) {65 if (strcmp(in_file, "-") == 0) {
67 in_f = stdin;66 in_f = stdin;
68 char *result = getcwd(cur_dir, sizeof(cur_dir));67 char *result = getcwd(cur_dir, sizeof(cur_dir));
69 if (!result)68 if (!result)
70 zig_panic("unable to get current working directory: %s", strerror(errno));69 zig_panic("unable to get current working directory: %s", strerror(errno));
71 cur_dir_path = buf_create_from_str(result);
72 } else {70 } else {
73 in_f = fopen(in_file, "rb");71 in_f = fopen(in_file, "rb");
74 if (!in_f)72 if (!in_f)
75 zig_panic("unable to open %s for reading: %s\n", in_file, strerror(errno));73 zig_panic("unable to open %s for reading: %s\n", in_file, strerror(errno));
76 cur_dir_path = buf_dirname(buf_create_from_str(in_file));
77 }74 }
7875
79 fprintf(stderr, "Original source:\n");76 fprintf(stderr, "Original source:\n");
...@@ -83,7 +80,7 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi...@@ -83,7 +80,7 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi
8380
84 fprintf(stderr, "\nTokens:\n");81 fprintf(stderr, "\nTokens:\n");
85 fprintf(stderr, "---------\n");82 fprintf(stderr, "---------\n");
86 ZigList<Token> *tokens = tokenize(in_data, cur_dir_path);83 ZigList<Token> *tokens = tokenize(in_data);
87 print_tokens(in_data, tokens);84 print_tokens(in_data, tokens);
8885
89 fprintf(stderr, "\nAST:\n");86 fprintf(stderr, "\nAST:\n");
...@@ -94,7 +91,7 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi...@@ -94,7 +91,7 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi
9491
95 fprintf(stderr, "\nSemantic Analysis:\n");92 fprintf(stderr, "\nSemantic Analysis:\n");
96 fprintf(stderr, "--------------------\n");93 fprintf(stderr, "--------------------\n");
97 CodeGen *codegen = create_codegen(root);94 CodeGen *codegen = create_codegen(root, false, buf_create_from_str(in_file));
98 semantic_analyze(codegen);95 semantic_analyze(codegen);
99 ZigList<ErrorMsg> *errors = codegen_error_messages(codegen);96 ZigList<ErrorMsg> *errors = codegen_error_messages(codegen);
100 if (errors->length == 0) {97 if (errors->length == 0) {
...@@ -115,7 +112,7 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi...@@ -115,7 +112,7 @@ static int build(const char *arg0, const char *in_file, const char *out_file, Zi
115112
116 fprintf(stderr, "\nLink:\n");113 fprintf(stderr, "\nLink:\n");
117 fprintf(stderr, "------------------\n");114 fprintf(stderr, "------------------\n");
118 code_gen_link(codegen, false, out_file);115 code_gen_link(codegen, out_file);
119 fprintf(stderr, "OK\n");116 fprintf(stderr, "OK\n");
120117
121 return 0;118 return 0;
src/os.cpp+20
...@@ -31,3 +31,23 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detache...@@ -31,3 +31,23 @@ void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detache
31 execvp(exe, const_cast<char * const *>(argv));31 execvp(exe, const_cast<char * const *>(argv));
32 zig_panic("execvp failed: %s", strerror(errno));32 zig_panic("execvp failed: %s", strerror(errno));
33}33}
34
35void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename) {
36 if (buf_len(full_path) <= 2)
37 zig_panic("TODO full path small");
38 int last_index = buf_len(full_path) - 1;
39 if (buf_ptr(full_path)[buf_len(full_path) - 1] == '/') {
40 last_index = buf_len(full_path) - 2;
41 }
42 for (int i = last_index; i >= 0; i -= 1) {
43 uint8_t c = buf_ptr(full_path)[i];
44 if (c == '/') {
45 buf_init_from_mem(out_dirname, buf_ptr(full_path), i);
46 buf_init_from_mem(out_basename, buf_ptr(full_path) + i + 1, buf_len(full_path) - (i + 1));
47 return;
48 }
49 }
50 buf_init_from_mem(out_dirname, ".", 1);
51 buf_init_from_buf(out_basename, full_path);
52}
53
src/os.hpp+3
...@@ -13,4 +13,7 @@...@@ -13,4 +13,7 @@
1313
14void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detached);14void os_spawn_process(const char *exe, ZigList<const char *> &args, bool detached);
1515
16void os_path_split(Buf *full_path, Buf *out_dirname, Buf *out_basename);
17
18
16#endif19#endif
src/tokenizer.cpp+1-3
...@@ -100,7 +100,6 @@ struct Tokenize {...@@ -100,7 +100,6 @@ struct Tokenize {
100 int line;100 int line;
101 int column;101 int column;
102 Token *cur_tok;102 Token *cur_tok;
103 Buf *cur_dir_path;
104};103};
105104
106__attribute__ ((format (printf, 2, 3)))105__attribute__ ((format (printf, 2, 3)))
...@@ -159,11 +158,10 @@ static void end_token(Tokenize *t) {...@@ -159,11 +158,10 @@ static void end_token(Tokenize *t) {
159 t->cur_tok = nullptr;158 t->cur_tok = nullptr;
160}159}
161160
162ZigList<Token> *tokenize(Buf *buf, Buf *cur_dir_path) {161ZigList<Token> *tokenize(Buf *buf) {
163 Tokenize t = {0};162 Tokenize t = {0};
164 t.tokens = allocate<ZigList<Token>>(1);163 t.tokens = allocate<ZigList<Token>>(1);
165 t.buf = buf;164 t.buf = buf;
166 t.cur_dir_path = cur_dir_path;
167 for (t.pos = 0; t.pos < buf_len(t.buf); t.pos += 1) {165 for (t.pos = 0; t.pos < buf_len(t.buf); t.pos += 1) {
168 uint8_t c = buf_ptr(t.buf)[t.pos];166 uint8_t c = buf_ptr(t.buf)[t.pos];
169 switch (t.state) {167 switch (t.state) {
src/tokenizer.hpp+1-1
...@@ -50,7 +50,7 @@ enum TokenizeState {...@@ -50,7 +50,7 @@ enum TokenizeState {
50 TokenizeStateSawDash,50 TokenizeStateSawDash,
51};51};
5252
53ZigList<Token> *tokenize(Buf *buf, Buf *cur_dir_path);53ZigList<Token> *tokenize(Buf *buf);
5454
55void print_tokens(Buf *buf, ZigList<Token> *tokens);55void print_tokens(Buf *buf, ZigList<Token> *tokens);
5656
src/util.hpp+1
...@@ -12,6 +12,7 @@...@@ -12,6 +12,7 @@
12#include <string.h>12#include <string.h>
13#include <assert.h>13#include <assert.h>
1414
15#include <new>
1516
16#define BREAKPOINT __asm("int $0x03")17#define BREAKPOINT __asm("int $0x03")
1718