Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
7051ef3
translate-c: start creating intermediate AST
Vexu Dec 17, 2020
6ecec4c
translate-c: translate C types to stage2 types
Vexu Jan 31, 2021
d835f5c
translate-c: make Node more like Type
Vexu Jan 31, 2021
4c0c9b0
translate-c: convert transBinaryOperator
Vexu Feb 6, 2021
f5041ca
translate-c: more binaryoperator chagnes, blocks and unary type expre…
Vexu Feb 7, 2021
7514c0a
translate-c: unary operator, integers and misc
Vexu Feb 7, 2021
f36849f
translate-c: convert function translation
Vexu Feb 7, 2021
bb867b0
translate-c: convert vardecl and typedef
Vexu Feb 8, 2021
5dac368
translate-c: convert record and enum decls
Vexu Feb 8, 2021
d8b9fca
translate-c: convert casts and string/array init
Vexu Feb 10, 2021
66dd64e
translate-c: convert most control flow
Vexu Feb 10, 2021
cadd448
translate-c: convert switch
Vexu Feb 10, 2021
450b718
translate-c: convert field/array access, call, pre/postcrement
Vexu Feb 10, 2021
c30c2f7
translate-c: convert assignment and conditional exprs
Vexu Feb 11, 2021
66bbd75
translate-c: convert macro translation
Vexu Feb 11, 2021
2a74a1e
translate-c: bunch of small fixes to get it compiling
Vexu Feb 12, 2021
13a9db2
translate-c: begin implementing ast.render
Vexu Feb 12, 2021
d7460db
translate-c: render a bunch of simple nodes and calls
Vexu Feb 13, 2021
1147ecc
translate-c: render variables and builtin calls
Vexu Feb 13, 2021
685778c
translate-c: render unary ops
Vexu Feb 13, 2021
227f167
translate-c: render binops and break/continue
Vexu Feb 13, 2021
f191251
translate-c: render functions
Vexu Feb 13, 2021
c4dfabf
translate-c: render macro functions, use cast type as return type
Vexu Feb 14, 2021
62162a0
translate-c: render control flow
Vexu Feb 14, 2021
c054096
translate-c: render array stuff
Vexu Feb 14, 2021
77a11e6
translate-c: render records and enums
Vexu Feb 15, 2021
78fba4e
translate-c: get all run-translated-c tests passing
Vexu Feb 15, 2021
74e9d4c
translate-c: get all translate-c tests passing
Vexu Feb 16, 2021
9a826cc
translate-c: elide some unecessary casts of literals
Vexu Feb 16, 2021
e297475
translate-c: demote untranslatable declarations to externs
Vexu Feb 17, 2021
d5fecbd
translate-c: support scoped typedef, enum and record decls
Vexu Feb 17, 2021
3717bed
translate-c: Add test for using correct block label for StmtExpr
ehaas Feb 14, 2021
7ca53bd
translate-c: improve switch translation
Vexu Feb 17, 2021
df5a812
translate-c: small miscellaneous improvements
Vexu Feb 18, 2021
3aba1fa
translate-c: ensure failed macros don't get defined multiple times
Vexu Feb 18, 2021
974a1c5
translate-c: small fixes to avoid generating invalid code for macros
Vexu Feb 19, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ set(ZIG_STAGE2_SOURCES
"${CMAKE_SOURCE_DIR}/src/windows_sdk.zig"
"${CMAKE_SOURCE_DIR}/src/zir.zig"
"${CMAKE_SOURCE_DIR}/src/zir_sema.zig"
"${CMAKE_SOURCE_DIR}/src/translate_c/ast.zig"
)

if(MSVC)
Expand Down
12 changes: 6 additions & 6 deletions lib/std/zig/parser_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3669,12 +3669,12 @@ test "zig fmt: hexadeciaml float literals with underscore separators" {
);
}

//test "zig fmt: C var args" {
// try testCanonical(
// \\pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
// \\
// );
//}
test "zig fmt: C var args" {
try testCanonical(
\\pub extern "c" fn printf(format: [*:0]const u8, ...) c_int;
\\
);
}

//test "zig fmt: Only indent multiline string literals in function calls" {
// try testCanonical(
Expand Down
11 changes: 4 additions & 7 deletions lib/std/zig/render.zig
Original file line number Diff line number Diff line change
Expand Up @@ -550,14 +550,11 @@ fn renderExpression(ais: *Ais, tree: ast.Tree, node: ast.Node.Index, space: Spac

.builtin_call_two, .builtin_call_two_comma => {
if (datas[node].lhs == 0) {
const params = [_]ast.Node.Index{};
return renderBuiltinCall(ais, tree, main_tokens[node], &params, space);
return renderBuiltinCall(ais, tree, main_tokens[node], &.{}, space);
} else if (datas[node].rhs == 0) {
const params = [_]ast.Node.Index{datas[node].lhs};
return renderBuiltinCall(ais, tree, main_tokens[node], &params, space);
return renderBuiltinCall(ais, tree, main_tokens[node], &.{datas[node].lhs}, space);
} else {
const params = [_]ast.Node.Index{ datas[node].lhs, datas[node].rhs };
return renderBuiltinCall(ais, tree, main_tokens[node], &params, space);
return renderBuiltinCall(ais, tree, main_tokens[node], &.{ datas[node].lhs, datas[node].rhs }, space);
}
},
.builtin_call, .builtin_call_comma => {
Expand Down Expand Up @@ -1311,7 +1308,7 @@ fn renderFnProto(ais: *Ais, tree: ast.Tree, fn_proto: ast.full.FnProto, space: S
.r_paren => break,
.comma => {
try renderToken(ais, tree, last_param_token, .space); // ,
last_param_token += 1;
continue;
},
else => {}, // Parameter type without a name.
}
Expand Down
2 changes: 1 addition & 1 deletion src/Compilation.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,7 @@ pub fn update(self: *Compilation) !void {
self.c_object_work_queue.writeItemAssumeCapacity(entry.key);
}

const use_stage1 = build_options.is_stage1 and self.bin_file.options.use_llvm;
const use_stage1 = build_options.omit_stage2 or build_options.is_stage1 and self.bin_file.options.use_llvm;
if (!use_stage1) {
if (self.bin_file.options.module) |module| {
module.compile_log_text.shrinkAndFree(module.gpa, 0);
Expand Down
70 changes: 32 additions & 38 deletions src/astgen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2664,8 +2664,11 @@ fn identifier(
return mod.failNode(scope, ident, "TODO implement '_' identifier", .{});
}

if (getSimplePrimitiveValue(ident_name)) |typed_value| {
const result = try addZIRInstConst(mod, scope, src, typed_value);
if (simple_types.get(ident_name)) |val_tag| {
const result = try addZIRInstConst(mod, scope, src, TypedValue{
.ty = Type.initTag(.type),
.val = Value.initTag(val_tag),
});
return rvalue(mod, scope, rl, result);
}

Expand Down Expand Up @@ -3325,42 +3328,33 @@ fn callExpr(
return rvalue(mod, scope, rl, result);
}

fn getSimplePrimitiveValue(name: []const u8) ?TypedValue {
const simple_types = std.ComptimeStringMap(Value.Tag, .{
.{ "u8", .u8_type },
.{ "i8", .i8_type },
.{ "isize", .isize_type },
.{ "usize", .usize_type },
.{ "c_short", .c_short_type },
.{ "c_ushort", .c_ushort_type },
.{ "c_int", .c_int_type },
.{ "c_uint", .c_uint_type },
.{ "c_long", .c_long_type },
.{ "c_ulong", .c_ulong_type },
.{ "c_longlong", .c_longlong_type },
.{ "c_ulonglong", .c_ulonglong_type },
.{ "c_longdouble", .c_longdouble_type },
.{ "f16", .f16_type },
.{ "f32", .f32_type },
.{ "f64", .f64_type },
.{ "f128", .f128_type },
.{ "c_void", .c_void_type },
.{ "bool", .bool_type },
.{ "void", .void_type },
.{ "type", .type_type },
.{ "anyerror", .anyerror_type },
.{ "comptime_int", .comptime_int_type },
.{ "comptime_float", .comptime_float_type },
.{ "noreturn", .noreturn_type },
});
if (simple_types.get(name)) |tag| {
return TypedValue{
.ty = Type.initTag(.type),
.val = Value.initTag(tag),
};
}
return null;
}
pub const simple_types = std.ComptimeStringMap(Value.Tag, .{
.{ "u8", .u8_type },
.{ "i8", .i8_type },
.{ "isize", .isize_type },
.{ "usize", .usize_type },
.{ "c_short", .c_short_type },
.{ "c_ushort", .c_ushort_type },
.{ "c_int", .c_int_type },
.{ "c_uint", .c_uint_type },
.{ "c_long", .c_long_type },
.{ "c_ulong", .c_ulong_type },
.{ "c_longlong", .c_longlong_type },
.{ "c_ulonglong", .c_ulonglong_type },
.{ "c_longdouble", .c_longdouble_type },
.{ "f16", .f16_type },
.{ "f32", .f32_type },
.{ "f64", .f64_type },
.{ "f128", .f128_type },
.{ "c_void", .c_void_type },
.{ "bool", .bool_type },
.{ "void", .void_type },
.{ "type", .type_type },
.{ "anyerror", .anyerror_type },
.{ "comptime_int", .comptime_int_type },
.{ "comptime_float", .comptime_float_type },
.{ "noreturn", .noreturn_type },
});

fn nodeMayNeedMemoryLocation(start_node: *ast.Node, scope: *Scope) bool {
var node = start_node;
Expand Down
11 changes: 7 additions & 4 deletions src/clang.zig
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ pub const APSInt = opaque {

pub const getNumWords = ZigClangAPSInt_getNumWords;
extern fn ZigClangAPSInt_getNumWords(*const APSInt) c_uint;

pub const lessThanEqual = ZigClangAPSInt_lessThanEqual;
extern fn ZigClangAPSInt_lessThanEqual(*const APSInt, rhs: u64) bool;
};

pub const ASTContext = opaque {
Expand Down Expand Up @@ -270,12 +273,12 @@ pub const CompoundAssignOperator = opaque {

pub const CompoundStmt = opaque {
pub const body_begin = ZigClangCompoundStmt_body_begin;
extern fn ZigClangCompoundStmt_body_begin(*const CompoundStmt) const_body_iterator;
extern fn ZigClangCompoundStmt_body_begin(*const CompoundStmt) ConstBodyIterator;

pub const body_end = ZigClangCompoundStmt_body_end;
extern fn ZigClangCompoundStmt_body_end(*const CompoundStmt) const_body_iterator;
extern fn ZigClangCompoundStmt_body_end(*const CompoundStmt) ConstBodyIterator;

pub const const_body_iterator = [*]const *Stmt;
pub const ConstBodyIterator = [*]const *Stmt;
};

pub const ConditionalOperator = opaque {};
Expand Down Expand Up @@ -407,7 +410,7 @@ pub const Expr = opaque {
pub const getBeginLoc = ZigClangExpr_getBeginLoc;
extern fn ZigClangExpr_getBeginLoc(*const Expr) SourceLocation;

pub const EvaluateAsConstantExpr = ZigClangExpr_EvaluateAsConstantExpr;
pub const evaluateAsConstantExpr = ZigClangExpr_EvaluateAsConstantExpr;
extern fn ZigClangExpr_EvaluateAsConstantExpr(*const Expr, *ExprEvalResult, Expr_ConstExprUsage, *const ASTContext) bool;
};

Expand Down
Loading