Skip to content

Commit 93a72bd

Browse files
committed
translate-c: a little closer to self-hosted implementation
1 parent bb25f21 commit 93a72bd

22 files changed

+1467
-366
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ set(ZIG_STD_FILES
512512
"heap.zig"
513513
"io.zig"
514514
"io/seekable_stream.zig"
515+
"io/c_out_stream.zig"
515516
"json.zig"
516517
"lazy_init.zig"
517518
"linked_list.zig"

src-self-hosted/clang.zig

Lines changed: 944 additions & 0 deletions
Large diffs are not rendered by default.

src-self-hosted/stage1.zig

Lines changed: 92 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
// This is Zig code that is used by both stage1 and stage2.
22
// The prototypes in src/userland.h must match these definitions.
3-
comptime {
4-
_ = @import("translate_c.zig");
5-
}
3+
4+
const std = @import("std");
65

76
pub const info_zen =
87
\\
@@ -29,3 +28,93 @@ export fn stage2_zen(ptr: *[*]const u8, len: *usize) void {
2928
export fn stage2_panic(ptr: [*]const u8, len: usize) void {
3029
@panic(ptr[0..len]);
3130
}
31+
32+
const TranslateMode = extern enum {
33+
import,
34+
translate,
35+
};
36+
37+
const Error = extern enum {
38+
None,
39+
OutOfMemory,
40+
InvalidFormat,
41+
SemanticAnalyzeFail,
42+
AccessDenied,
43+
Interrupted,
44+
SystemResources,
45+
FileNotFound,
46+
FileSystem,
47+
FileTooBig,
48+
DivByZero,
49+
Overflow,
50+
PathAlreadyExists,
51+
Unexpected,
52+
ExactDivRemainder,
53+
NegativeDenominator,
54+
ShiftedOutOneBits,
55+
CCompileErrors,
56+
EndOfFile,
57+
IsDir,
58+
NotDir,
59+
UnsupportedOperatingSystem,
60+
SharingViolation,
61+
PipeBusy,
62+
PrimitiveTypeNotFound,
63+
CacheUnavailable,
64+
PathTooLong,
65+
CCompilerCannotFindFile,
66+
ReadingDepFile,
67+
InvalidDepFile,
68+
MissingArchitecture,
69+
MissingOperatingSystem,
70+
UnknownArchitecture,
71+
UnknownOperatingSystem,
72+
UnknownABI,
73+
InvalidFilename,
74+
DiskQuota,
75+
DiskSpace,
76+
UnexpectedWriteFailure,
77+
UnexpectedSeekFailure,
78+
UnexpectedFileTruncationFailure,
79+
Unimplemented,
80+
OperationAborted,
81+
BrokenPipe,
82+
NoSpaceLeft,
83+
};
84+
85+
const FILE = std.c.FILE;
86+
const ast = std.zig.ast;
87+
88+
/// Args should have a null terminating last arg.
89+
export fn stage2_translate_c(
90+
out_ast: **ast.Tree,
91+
args_begin: [*]?[*]const u8,
92+
args_end: [*]?[*]const u8,
93+
mode: TranslateMode,
94+
) Error {
95+
const translate_c = @import("translate_c.zig");
96+
out_ast.* = translate_c.translate(args_begin, args_end, switch (mode) {
97+
.import => translate_c.Mode.import,
98+
.translate => translate_c.Mode.translate,
99+
}) catch |err| switch (err) {
100+
error.Unimplemented => return Error.Unimplemented,
101+
};
102+
return Error.None;
103+
}
104+
105+
export fn stage2_render_ast(tree: *ast.Tree, output_file: *FILE) Error {
106+
const c_out_stream = &std.io.COutStream.init(output_file).stream;
107+
_ = std.zig.render(std.heap.c_allocator, c_out_stream, tree) catch |e| switch (e) {
108+
error.SystemResources => return Error.SystemResources,
109+
error.OperationAborted => return Error.OperationAborted,
110+
error.BrokenPipe => return Error.BrokenPipe,
111+
error.DiskQuota => return Error.DiskQuota,
112+
error.FileTooBig => return Error.FileTooBig,
113+
error.NoSpaceLeft => return Error.NoSpaceLeft,
114+
error.AccessDenied => return Error.AccessDenied,
115+
error.OutOfMemory => return Error.OutOfMemory,
116+
error.Unexpected => return Error.Unexpected,
117+
error.InputOutput => return Error.FileSystem,
118+
};
119+
return Error.None;
120+
}

src-self-hosted/translate_c.zig

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@
22
// and stage2. Currently it's not used by anything, as it's not feature complete.
33

44
const std = @import("std");
5+
const ast = std.zig.ast;
6+
use @import("clang.zig");
57

6-
export fn stage2_translate_c() void {
7-
std.debug.panic("unimplemented");
8+
pub const Mode = enum {
9+
import,
10+
translate,
11+
};
12+
13+
pub fn translate(args_begin: [*]?[*]const u8, args_end: [*]?[*]const u8, mode: Mode) !*ast.Tree {
14+
return error.Unimplemented;
815
}

src/analyze.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3725,7 +3725,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {
37253725
}
37263726
if (g->verbose_ir) {
37273727
fprintf(stderr, "\n");
3728-
ast_render(g, stderr, fn_table_entry->body_node, 4);
3728+
ast_render(stderr, fn_table_entry->body_node, 4);
37293729
fprintf(stderr, "\n{ // (IR)\n");
37303730
ir_print(g, stderr, &fn_table_entry->ir_executable, 4);
37313731
fprintf(stderr, "}\n");

src/analyze.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,4 +247,6 @@ Error create_c_object_cache(CodeGen *g, CacheHash **out_cache_hash, bool verbose
247247
LLVMTypeRef get_llvm_type(CodeGen *g, ZigType *type);
248248
ZigLLVMDIType *get_llvm_di_type(CodeGen *g, ZigType *type);
249249

250+
void add_cc_args(CodeGen *g, ZigList<const char *> &args, const char *out_dep_path, bool translate_c);
251+
250252
#endif

src/ast_render.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ void ast_print(FILE *f, AstNode *node, int indent) {
296296

297297

298298
struct AstRender {
299-
CodeGen *codegen;
300299
int indent;
301300
int indent_size;
302301
FILE *f;
@@ -1170,9 +1169,8 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
11701169
}
11711170

11721171

1173-
void ast_render(CodeGen *codegen, FILE *f, AstNode *node, int indent_size) {
1172+
void ast_render(FILE *f, AstNode *node, int indent_size) {
11741173
AstRender ar = {0};
1175-
ar.codegen = codegen;
11761174
ar.f = f;
11771175
ar.indent_size = indent_size;
11781176
ar.indent = 0;

src/ast_render.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515

1616
void ast_print(FILE *f, AstNode *node, int indent);
1717

18-
void ast_render(CodeGen *codegen, FILE *f, AstNode *node, int indent_size);
18+
void ast_render(FILE *f, AstNode *node, int indent_size);
1919

2020
#endif

0 commit comments

Comments
 (0)