Skip to content

Commit 3b27815

Browse files
Vexumlugg
authored andcommitted
aro-translate-c: update to upstream changes
1 parent 0ab211f commit 3b27815

File tree

9 files changed

+613
-36
lines changed

9 files changed

+613
-36
lines changed

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,9 +817,12 @@ set(BUILD_ZIG2_ARGS
817817
-OReleaseSmall
818818
--name zig2 -femit-bin="${ZIG2_C_SOURCE}"
819819
--mod "build_options::${ZIG_CONFIG_ZIG_OUT}"
820+
--mod "aro_options::src/stubs/aro_options.zig"
820821
--mod "Builtins/Builtin.def::src/stubs/aro_builtins.zig"
821822
--mod "Attribute/names.def::src/stubs/aro_names.zig"
822-
--mod "aro:Builtins/Builtin.def,Attribute/names.def:deps/aro/lib.zig"
823+
--mod "Diagnostics/messages.def::src/stubs/aro_messages.zig"
824+
--mod "aro_backend:build_options=aro_options:deps/aro/backend.zig"
825+
--mod "aro:Builtins/Builtin.def,Attribute/names.def,Diagnostics/messages.def,build_options=aro_options,backend=aro_backend:deps/aro/aro.zig"
823826
--deps build_options,aro
824827
-target "${ZIG_HOST_TARGET_TRIPLE}"
825828
)

bootstrap.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,12 @@ int main(int argc, char **argv) {
140140
"-ofmt=c", "-lc", "-OReleaseSmall",
141141
"--name", "zig2", "-femit-bin=zig2.c",
142142
"--mod", "build_options::config.zig",
143+
"--mod", "aro_options::src/stubs/aro_options.zig",
143144
"--mod", "Builtins/Builtin.def::src/stubs/aro_builtins.zig",
144145
"--mod", "Attribute/names.def::src/stubs/aro_names.zig",
145-
"--mod", "aro:Builtins/Builtin.def,Attribute/names.def:deps/aro/lib.zig",
146+
"--mod", "Diagnostics/messages.def::src/stubs/aro_messages.zig",
147+
"--mod", "aro_backend:build_options=aro_options:deps/aro/backend.zig",
148+
"--mod", "aro:Builtins/Builtin.def,Attribute/names.def,Diagnostics/messages.def,build_options=aro_options,backend=aro_backend:deps/aro/aro.zig",
146149
"--deps", "build_options,aro",
147150
"-target", host_triple,
148151
NULL,

build.zig

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,33 @@ fn addCompilerStep(
590590
.max_rss = 7_000_000_000,
591591
});
592592
exe.stack_size = stack_size;
593+
594+
const aro_options = b.addOptions();
595+
aro_options.addOption([]const u8, "version_str", "aro-zig");
596+
const aro_options_module = aro_options.createModule();
597+
const aro_backend = b.createModule(.{
598+
.source_file = .{ .path = "deps/aro/backend.zig" },
599+
.dependencies = &.{.{
600+
.name = "build_options",
601+
.module = aro_options_module,
602+
}},
603+
});
593604
const aro_module = b.createModule(.{
594-
.source_file = .{ .path = "deps/aro/lib.zig" },
605+
.source_file = .{ .path = "deps/aro/aro.zig" },
606+
.dependencies = &.{
607+
.{
608+
.name = "build_options",
609+
.module = aro_options_module,
610+
},
611+
.{
612+
.name = "backend",
613+
.module = aro_backend,
614+
},
615+
GenerateDef.create(b, .{ .name = "Builtins/Builtin.def", .src_prefix = "deps/aro/aro" }),
616+
GenerateDef.create(b, .{ .name = "Attribute/names.def", .src_prefix = "deps/aro/aro" }),
617+
GenerateDef.create(b, .{ .name = "Diagnostics/messages.def", .src_prefix = "deps/aro/aro", .kind = .named }),
618+
},
595619
});
596-
GenerateDef.add(b, "deps/aro/Builtins/Builtin.def", "Builtins/Builtin.def", exe, aro_module);
597-
GenerateDef.add(b, "deps/aro/Attribute/names.def", "Attribute/names.def", exe, aro_module);
598620

599621
exe.addModule("aro", aro_module);
600622
return exe;

deps/aro/LICENSE-UNICODE

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
UNICODE LICENSE V3
2+
3+
COPYRIGHT AND PERMISSION NOTICE
4+
5+
Copyright © 1991-2023 Unicode, Inc.
6+
7+
NOTICE TO USER: Carefully read the following legal agreement. BY
8+
DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR
9+
SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
10+
TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT
11+
DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.
12+
13+
Permission is hereby granted, free of charge, to any person obtaining a
14+
copy of data files and any associated documentation (the "Data Files") or
15+
software and any associated documentation (the "Software") to deal in the
16+
Data Files or Software without restriction, including without limitation
17+
the rights to use, copy, modify, merge, publish, distribute, and/or sell
18+
copies of the Data Files or Software, and to permit persons to whom the
19+
Data Files or Software are furnished to do so, provided that either (a)
20+
this copyright and permission notice appear with all copies of the Data
21+
Files or Software, or (b) this copyright and permission notice appear in
22+
associated Documentation.
23+
24+
THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
25+
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
27+
THIRD PARTY RIGHTS.
28+
29+
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
30+
BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
31+
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
32+
WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
33+
ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA
34+
FILES OR SOFTWARE.
35+
36+
Except as contained in this notice, the name of a copyright holder shall
37+
not be used in advertising or otherwise to promote the sale, use or other
38+
dealings in these Data Files or Software without prior written
39+
authorization of the copyright holder.

src/aro_translate_c.zig

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ const AliasList = common.AliasList;
2121
const ResultUsed = common.ResultUsed;
2222
const Scope = common.ScopeExtra(Context, Type);
2323

24-
pub const Compilation = aro.Compilation;
25-
2624
const Context = struct {
2725
gpa: mem.Allocator,
2826
arena: mem.Allocator,
@@ -54,7 +52,7 @@ const Context = struct {
5452

5553
pattern_list: translate_c.PatternList,
5654
tree: Tree,
57-
comp: *Compilation,
55+
comp: *aro.Compilation,
5856
mapper: aro.TypeMapper,
5957

6058
fn getMangle(c: *Context) u32 {
@@ -108,7 +106,7 @@ fn warn(c: *Context, scope: *Scope, loc: TokenIndex, comptime format: []const u8
108106

109107
pub fn translate(
110108
gpa: mem.Allocator,
111-
comp: *Compilation,
109+
comp: *aro.Compilation,
112110
args: []const []const u8,
113111
) !std.zig.Ast {
114112
try comp.addDefaultPragmaHandlers();
@@ -124,23 +122,18 @@ pub fn translate(
124122
assert(driver.inputs.items.len == 1);
125123
const source = driver.inputs.items[0];
126124

127-
const builtin = try comp.generateBuiltinMacros();
125+
const builtin_macros = try comp.generateBuiltinMacros(.include_system_defines);
128126
const user_macros = try comp.addSourceFromBuffer("<command line>", macro_buf.items);
129127

130-
var pp = aro.Preprocessor.init(comp);
128+
var pp = try aro.Preprocessor.initDefault(comp);
131129
defer pp.deinit();
132130

133-
try pp.addBuiltinMacros();
134-
135-
_ = try pp.preprocess(builtin);
136-
_ = try pp.preprocess(user_macros);
137-
const eof = try pp.preprocess(source);
138-
try pp.tokens.append(pp.comp.gpa, eof);
131+
try pp.preprocessSources(&.{ source, builtin_macros, user_macros });
139132

140-
var tree = try aro.Parser.parse(&pp);
133+
var tree = try pp.parse();
141134
defer tree.deinit();
142135

143-
if (driver.comp.diag.errors != 0) {
136+
if (driver.comp.diagnostics.errors != 0) {
144137
return error.SemanticAnalyzeFail;
145138
}
146139

@@ -441,14 +434,11 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: NodeIndex, field_nodes:
441434
};
442435

443436
const val = c.tree.value_map.get(field_node).?;
444-
const str = try std.fmt.allocPrint(c.arena, "{d}", .{val.data.int});
445-
const int = try ZigTag.integer_literal.create(c.arena, str);
446-
447437
const enum_const_def = try ZigTag.enum_constant.create(c.arena, .{
448438
.name = enum_val_name,
449439
.is_public = toplevel,
450440
.type = enum_const_type_node,
451-
.value = int,
441+
.value = try transCreateNodeAPInt(c, val),
452442
});
453443
if (toplevel)
454444
try addTopLevelDecl(c, enum_val_name, enum_const_def)
@@ -565,7 +555,7 @@ fn transFnType(
565555

566556
const linksection_string = blk: {
567557
if (raw_ty.getAttribute(.section)) |section| {
568-
break :blk section.name.slice(c.tree.strings);
558+
break :blk c.comp.interner.get(section.name.ref()).bytes;
569559
}
570560
break :blk null;
571561
};
@@ -659,8 +649,7 @@ fn transExpr(c: *Context, node: NodeIndex, result_used: ResultUsed) TransError!Z
659649
const ty = c.tree.nodes.items(.ty)[@intFromEnum(node)];
660650
if (c.tree.value_map.get(node)) |val| {
661651
// TODO handle other values
662-
const str = try std.fmt.allocPrint(c.arena, "{d}", .{val.data.int});
663-
const int = try ZigTag.integer_literal.create(c.arena, str);
652+
const int = try transCreateNodeAPInt(c, val);
664653
const as_node = try ZigTag.as.create(c.arena, .{
665654
.lhs = try transType(c, undefined, ty, undefined),
666655
.rhs = int,
@@ -673,3 +662,17 @@ fn transExpr(c: *Context, node: NodeIndex, result_used: ResultUsed) TransError!Z
673662
}
674663
return .none;
675664
}
665+
666+
fn transCreateNodeAPInt(c: *Context, int: aro.Value) !ZigNode {
667+
var space: aro.Interner.Tag.Int.BigIntSpace = undefined;
668+
var big = int.toBigInt(&space, c.comp);
669+
const is_negative = !big.positive;
670+
big.positive = true;
671+
672+
const str = big.toStringAlloc(c.arena, 10, .lower) catch |err| switch (err) {
673+
error.OutOfMemory => return error.OutOfMemory,
674+
};
675+
const res = try ZigTag.integer_literal.create(c.arena, str);
676+
if (is_negative) return ZigTag.negate.create(c.arena, res);
677+
return res;
678+
}

src/main.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4335,14 +4335,15 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, fancy_output: ?*Compilati
43354335

43364336
var tree = switch (comp.c_frontend) {
43374337
.aro => tree: {
4338+
const aro = @import("aro");
43384339
const translate_c = @import("aro_translate_c.zig");
4339-
var aro_comp = translate_c.Compilation.init(comp.gpa);
4340+
var aro_comp = aro.Compilation.init(comp.gpa);
43404341
defer aro_comp.deinit();
43414342

43424343
break :tree translate_c.translate(comp.gpa, &aro_comp, argv.items) catch |err| switch (err) {
43434344
error.SemanticAnalyzeFail, error.FatalError => {
43444345
// TODO convert these to zig errors
4345-
aro_comp.renderErrors();
4346+
aro.Diagnostics.render(&aro_comp, std.io.tty.detectConfig(std.io.getStdErr()));
43464347
process.exit(1);
43474348
},
43484349
error.OutOfMemory => return error.OutOfMemory,

src/mingw.zig

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,9 +357,9 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
357357
nosuspend stderr.print("output path: {s}\n", .{def_final_path}) catch break :print;
358358
}
359359

360-
try aro_comp.include_dirs.append(include_dir);
360+
try aro_comp.include_dirs.append(comp.gpa, include_dir);
361361

362-
const builtin_macros = try aro_comp.generateBuiltinMacros();
362+
const builtin_macros = try aro_comp.generateBuiltinMacros(.include_system_defines);
363363
const user_macros = try aro_comp.addSourceFromBuffer("<command line>", target_defines);
364364
const def_file_source = try aro_comp.addSourceFromPath(def_file_path);
365365

@@ -368,14 +368,11 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
368368
pp.linemarkers = .none;
369369
pp.preserve_whitespace = true;
370370

371-
_ = try pp.preprocess(builtin_macros);
372-
_ = try pp.preprocess(user_macros);
373-
const eof = try pp.preprocess(def_file_source);
374-
try pp.tokens.append(pp.comp.gpa, eof);
371+
try pp.preprocessSources(&.{ def_file_source, builtin_macros, user_macros });
375372

376-
for (aro_comp.diag.list.items) |diagnostic| {
373+
for (aro_comp.diagnostics.list.items) |diagnostic| {
377374
if (diagnostic.kind == .@"fatal error" or diagnostic.kind == .@"error") {
378-
aro_comp.renderErrors();
375+
aro.Diagnostics.render(&aro_comp, std.io.tty.detectConfig(std.io.getStdErr()));
379376
return error.AroPreprocessorFailed;
380377
}
381378
}

0 commit comments

Comments
 (0)