authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-10 17:42:47-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2015-12-10 17:42:47-07:00
log15ba5bc54e286fb64d67e38857ede4b0dac9c841
tree994b49126657dd38831d4a937e25f5fde4a8cff2
parent0dbee2300ed28c18ecccaf71f10f68eb2da71266

provide std.zig and add it to import paths


6 files changed, 58 insertions(+), 12 deletions(-)

CMakeLists.txt+1
......@@ -117,6 +117,7 @@ set(C_HEADERS
117117
118118set(ZIG_STD_SRC
119119 "${CMAKE_SOURCE_DIR}/std/bootstrap.zig"
120 "${CMAKE_SOURCE_DIR}/std/std.zig"
120121)
121122
122123set(C_HEADERS_DEST "lib/zig/include")
example/hello_world/hello2.zig+2-5
......@@ -1,11 +1,8 @@
11export executable "hello";
22
3#link("c")
4extern {
5 fn printf(__format: *const u8, ...) -> i32;
6}
3use "std.zig";
74
85export fn main(argc : isize, argv : *mut *mut u8, env : *mut *mut u8) -> i32 {
9 printf("argc = %zu\n", argc);
6 print_str("Hello, world!", 13);
107 return 0;
118}
src/analyze.hpp+2
......@@ -108,6 +108,8 @@ struct CodeGen {
108108 LLVMZigDIBuilder *dbuilder;
109109 LLVMZigDICompileUnit *compile_unit;
110110
111 ZigList<Buf *> lib_search_paths;
112
111113 // reminder: hash tables must be initialized before use
112114 HashMap<Buf *, FnTableEntry *, buf_hash, buf_eql_buf> fn_table;
113115 HashMap<Buf *, LLVMValueRef, buf_hash, buf_eql_buf> str_table;
src/codegen.cpp+29-6
......@@ -26,6 +26,7 @@ CodeGen *codegen_create(Buf *root_source_dir) {
2626 g->import_table.init(32);
2727 g->build_type = CodeGenBuildTypeDebug;
2828 g->root_source_dir = root_source_dir;
29
2930 return g;
3031}
3132
......@@ -1066,6 +1067,9 @@ static void define_primitive_types(CodeGen *g) {
10661067
10671068
10681069static void init(CodeGen *g, Buf *source_path) {
1070 g->lib_search_paths.append(g->root_source_dir);
1071 g->lib_search_paths.append(buf_create_from_str(ZIG_STD_DIR));
1072
10691073 LLVMInitializeAllTargets();
10701074 LLVMInitializeAllTargetMCs();
10711075 LLVMInitializeAllAsmPrinters();
......@@ -1188,17 +1192,34 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou
11881192 AstNode *top_level_decl = import_entry->root->data.root.top_level_decls.at(decl_i);
11891193
11901194 if (top_level_decl->type == NodeTypeUse) {
1191 auto entry = g->import_table.maybe_get(&top_level_decl->data.use.path);
1195 Buf *import_target_path = &top_level_decl->data.use.path;
1196 auto entry = g->import_table.maybe_get(import_target_path);
11921197 if (!entry) {
11931198 Buf full_path = BUF_INIT;
1194 os_path_join(g->root_source_dir, &top_level_decl->data.use.path, &full_path);
11951199 Buf *import_code = buf_alloc();
1196 if ((err = os_fetch_file_path(&full_path, import_code))) {
1197 add_node_error(g, top_level_decl,
1198 buf_sprintf("unable to open '%s': %s", buf_ptr(&full_path), err_str(err)));
1200 bool found_it = false;
1201
1202 for (int path_i = 0; path_i < g->lib_search_paths.length; path_i += 1) {
1203 Buf *search_path = g->lib_search_paths.at(path_i);
1204 os_path_join(search_path, import_target_path, &full_path);
1205
1206 if ((err = os_fetch_file_path(&full_path, import_code))) {
1207 if (err == ErrorFileNotFound) {
1208 continue;
1209 } else {
1210 add_node_error(g, top_level_decl,
1211 buf_sprintf("unable to open '%s': %s", buf_ptr(&full_path), err_str(err)));
1212 goto done_looking_at_imports;
1213 }
1214 }
1215 codegen_add_code(g, &top_level_decl->data.use.path, import_code);
1216 found_it = true;
11991217 break;
12001218 }
1201 codegen_add_code(g, &top_level_decl->data.use.path, import_code);
1219 if (!found_it) {
1220 add_node_error(g, top_level_decl,
1221 buf_sprintf("unable to find '%s'", buf_ptr(import_target_path)));
1222 }
12021223 }
12031224 } else if (top_level_decl->type == NodeTypeFnDef) {
12041225 AstNode *proto_node = top_level_decl->data.fn_def.fn_proto;
......@@ -1213,6 +1234,8 @@ static ImportTableEntry *codegen_add_code(CodeGen *g, Buf *source_path, Buf *sou
12131234 }
12141235 }
12151236
1237done_looking_at_imports:
1238
12161239 return import_entry;
12171240}
12181241
std/std.zig created+23
......@@ -0,0 +1,23 @@
1fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
2 let mut result : isize;
3 asm volatile (
4 "mov %[number], %%rax\n"
5 "mov %[arg1], %%rdi\n"
6 "mov %[arg2], %%rsi\n"
7 "mov %[arg3], %%rdx\n"
8 "syscall\n"
9 "mov %%rax, %[ret]\n"
10 : [ret] "=r" (result)
11 : [number] "r" (number), [arg1] "r" (arg1), [arg2] "r" (arg2), [arg3] "r" (arg3)
12 : "rcx", "r11", "rax", "rdi", "rsi", "rdx");
13 return result;
14}
15
16// TODO error handling
17// TODO zig strings instead of C strings
18// TODO handle buffering and flushing
19pub print_str(str : *const u8, len: isize) {
20 let SYS_write = 1;
21 let stdout_fileno = 1;
22 syscall3(SYS_write, stdout_fileno, str as isize, str_len);
23}
test/run_tests.cpp+1-1
......@@ -461,7 +461,7 @@ export executable "test";
461461
462462 add_compile_fail_case("bad import", R"SOURCE(
463463use "bogus-does-not-exist.zig";
464 )SOURCE", 1, ".tmp_source.zig:2:1: error: unable to open './bogus-does-not-exist.zig': file not found");
464 )SOURCE", 1, ".tmp_source.zig:2:1: error: unable to find 'bogus-does-not-exist.zig'");
465465
466466 add_compile_fail_case("undeclared identifier", R"SOURCE(
467467fn a() {