aro-translate-c: update to upstream changes

This commit is contained in:
Veikka Tuominen 2023-11-15 11:30:26 +02:00
parent 145ddb8104
commit acebf05be9
8 changed files with 575 additions and 37 deletions

View File

@ -817,9 +817,12 @@ set(BUILD_ZIG2_ARGS
-OReleaseSmall
--name zig2 -femit-bin="${ZIG2_C_SOURCE}"
--mod "build_options::${ZIG_CONFIG_ZIG_OUT}"
--mod "aro_options::src/stubs/aro_options.zig"
--mod "Builtins/Builtin.def::src/stubs/aro_builtins.zig"
--mod "Attribute/names.def::src/stubs/aro_names.zig"
--mod "aro:Builtins/Builtin.def,Attribute/names.def:deps/aro/lib.zig"
--mod "Diagnostics/messages.def::src/stubs/aro_messages.zig"
--mod "aro_backend:build_options=aro_options:deps/aro/backend.zig"
--mod "aro:Builtins/Builtin.def,Attribute/names.def,Diagnostics/messages.def,build_options=aro_options,backend=aro_backend:deps/aro/aro.zig"
--deps build_options,aro
-target "${ZIG_HOST_TARGET_TRIPLE}"
)

View File

@ -140,9 +140,12 @@ int main(int argc, char **argv) {
"-ofmt=c", "-lc", "-OReleaseSmall",
"--name", "zig2", "-femit-bin=zig2.c",
"--mod", "build_options::config.zig",
"--mod", "aro_options::src/stubs/aro_options.zig",
"--mod", "Builtins/Builtin.def::src/stubs/aro_builtins.zig",
"--mod", "Attribute/names.def::src/stubs/aro_names.zig",
"--mod", "aro:Builtins/Builtin.def,Attribute/names.def:deps/aro/lib.zig",
"--mod", "Diagnostics/messages.def::src/stubs/aro_messages.zig",
"--mod", "aro_backend:build_options=aro_options:deps/aro/backend.zig",
"--mod", "aro:Builtins/Builtin.def,Attribute/names.def,Diagnostics/messages.def,build_options=aro_options,backend=aro_backend:deps/aro/aro.zig",
"--deps", "build_options,aro",
"-target", host_triple,
NULL,

View File

@ -590,11 +590,33 @@ fn addCompilerStep(
.max_rss = 7_000_000_000,
});
exe.stack_size = stack_size;
const aro_module = b.createModule(.{
.source_file = .{ .path = "deps/aro/lib.zig" },
const aro_options = b.addOptions();
aro_options.addOption([]const u8, "version_str", "aro-zig");
const aro_options_module = aro_options.createModule();
const aro_backend = b.createModule(.{
.source_file = .{ .path = "deps/aro/backend.zig" },
.dependencies = &.{.{
.name = "build_options",
.module = aro_options_module,
}},
});
const aro_module = b.createModule(.{
.source_file = .{ .path = "deps/aro/aro.zig" },
.dependencies = &.{
.{
.name = "build_options",
.module = aro_options_module,
},
.{
.name = "backend",
.module = aro_backend,
},
GenerateDef.create(b, .{ .name = "Builtins/Builtin.def", .src_prefix = "deps/aro/aro" }),
GenerateDef.create(b, .{ .name = "Attribute/names.def", .src_prefix = "deps/aro/aro" }),
GenerateDef.create(b, .{ .name = "Diagnostics/messages.def", .src_prefix = "deps/aro/aro", .kind = .named }),
},
});
GenerateDef.add(b, "deps/aro/Builtins/Builtin.def", "Builtins/Builtin.def", exe, aro_module);
GenerateDef.add(b, "deps/aro/Attribute/names.def", "Attribute/names.def", exe, aro_module);
exe.addModule("aro", aro_module);
return exe;

View File

@ -21,8 +21,6 @@ const AliasList = common.AliasList;
const ResultUsed = common.ResultUsed;
const Scope = common.ScopeExtra(Context, Type);
pub const Compilation = aro.Compilation;
const Context = struct {
gpa: mem.Allocator,
arena: mem.Allocator,
@ -54,7 +52,7 @@ const Context = struct {
pattern_list: translate_c.PatternList,
tree: Tree,
comp: *Compilation,
comp: *aro.Compilation,
mapper: aro.TypeMapper,
fn getMangle(c: *Context) u32 {
@ -108,7 +106,7 @@ fn warn(c: *Context, scope: *Scope, loc: TokenIndex, comptime format: []const u8
pub fn translate(
gpa: mem.Allocator,
comp: *Compilation,
comp: *aro.Compilation,
args: []const []const u8,
) !std.zig.Ast {
try comp.addDefaultPragmaHandlers();
@ -124,23 +122,18 @@ pub fn translate(
assert(driver.inputs.items.len == 1);
const source = driver.inputs.items[0];
const builtin = try comp.generateBuiltinMacros();
const builtin_macros = try comp.generateBuiltinMacros(.include_system_defines);
const user_macros = try comp.addSourceFromBuffer("<command line>", macro_buf.items);
var pp = aro.Preprocessor.init(comp);
var pp = try aro.Preprocessor.initDefault(comp);
defer pp.deinit();
try pp.addBuiltinMacros();
try pp.preprocessSources(&.{ source, builtin_macros, user_macros });
_ = try pp.preprocess(builtin);
_ = try pp.preprocess(user_macros);
const eof = try pp.preprocess(source);
try pp.tokens.append(pp.comp.gpa, eof);
var tree = try aro.Parser.parse(&pp);
var tree = try pp.parse();
defer tree.deinit();
if (driver.comp.diag.errors != 0) {
if (driver.comp.diagnostics.errors != 0) {
return error.SemanticAnalyzeFail;
}
@ -441,14 +434,11 @@ fn transEnumDecl(c: *Context, scope: *Scope, enum_decl: NodeIndex, field_nodes:
};
const val = c.tree.value_map.get(field_node).?;
const str = try std.fmt.allocPrint(c.arena, "{d}", .{val.data.int});
const int = try ZigTag.integer_literal.create(c.arena, str);
const enum_const_def = try ZigTag.enum_constant.create(c.arena, .{
.name = enum_val_name,
.is_public = toplevel,
.type = enum_const_type_node,
.value = int,
.value = try transCreateNodeAPInt(c, val),
});
if (toplevel)
try addTopLevelDecl(c, enum_val_name, enum_const_def)
@ -565,7 +555,7 @@ fn transFnType(
const linksection_string = blk: {
if (raw_ty.getAttribute(.section)) |section| {
break :blk section.name.slice(c.tree.strings);
break :blk c.comp.interner.get(section.name.ref()).bytes;
}
break :blk null;
};
@ -659,8 +649,7 @@ fn transExpr(c: *Context, node: NodeIndex, result_used: ResultUsed) TransError!Z
const ty = c.tree.nodes.items(.ty)[@intFromEnum(node)];
if (c.tree.value_map.get(node)) |val| {
// TODO handle other values
const str = try std.fmt.allocPrint(c.arena, "{d}", .{val.data.int});
const int = try ZigTag.integer_literal.create(c.arena, str);
const int = try transCreateNodeAPInt(c, val);
const as_node = try ZigTag.as.create(c.arena, .{
.lhs = try transType(c, undefined, ty, undefined),
.rhs = int,
@ -673,3 +662,17 @@ fn transExpr(c: *Context, node: NodeIndex, result_used: ResultUsed) TransError!Z
}
return .none;
}
fn transCreateNodeAPInt(c: *Context, int: aro.Value) !ZigNode {
var space: aro.Interner.Tag.Int.BigIntSpace = undefined;
var big = int.toBigInt(&space, c.comp);
const is_negative = !big.positive;
big.positive = true;
const str = big.toStringAlloc(c.arena, 10, .lower) catch |err| switch (err) {
error.OutOfMemory => return error.OutOfMemory,
};
const res = try ZigTag.integer_literal.create(c.arena, str);
if (is_negative) return ZigTag.negate.create(c.arena, res);
return res;
}

View File

@ -4335,14 +4335,15 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, fancy_output: ?*Compilati
var tree = switch (comp.c_frontend) {
.aro => tree: {
const aro = @import("aro");
const translate_c = @import("aro_translate_c.zig");
var aro_comp = translate_c.Compilation.init(comp.gpa);
var aro_comp = aro.Compilation.init(comp.gpa);
defer aro_comp.deinit();
break :tree translate_c.translate(comp.gpa, &aro_comp, argv.items) catch |err| switch (err) {
error.SemanticAnalyzeFail, error.FatalError => {
// TODO convert these to zig errors
aro_comp.renderErrors();
aro.Diagnostics.render(&aro_comp, std.io.tty.detectConfig(std.io.getStdErr()));
process.exit(1);
},
error.OutOfMemory => return error.OutOfMemory,

View File

@ -357,9 +357,9 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
nosuspend stderr.print("output path: {s}\n", .{def_final_path}) catch break :print;
}
try aro_comp.include_dirs.append(include_dir);
try aro_comp.include_dirs.append(comp.gpa, include_dir);
const builtin_macros = try aro_comp.generateBuiltinMacros();
const builtin_macros = try aro_comp.generateBuiltinMacros(.include_system_defines);
const user_macros = try aro_comp.addSourceFromBuffer("<command line>", target_defines);
const def_file_source = try aro_comp.addSourceFromPath(def_file_path);
@ -368,14 +368,11 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
pp.linemarkers = .none;
pp.preserve_whitespace = true;
_ = try pp.preprocess(builtin_macros);
_ = try pp.preprocess(user_macros);
const eof = try pp.preprocess(def_file_source);
try pp.tokens.append(pp.comp.gpa, eof);
try pp.preprocessSources(&.{ def_file_source, builtin_macros, user_macros });
for (aro_comp.diag.list.items) |diagnostic| {
for (aro_comp.diagnostics.list.items) |diagnostic| {
if (diagnostic.kind == .@"fatal error" or diagnostic.kind == .@"error") {
aro_comp.renderErrors();
aro.Diagnostics.render(&aro_comp, std.io.tty.detectConfig(std.io.getStdErr()));
return error.AroPreprocessorFailed;
}
}

508
src/stubs/aro_messages.zig Normal file
View File

@ -0,0 +1,508 @@
//! Stub implementation only used when bootstrapping stage2
//! Keep in sync with deps/aro/build/GenerateDef.zig
pub fn with(comptime Properties: type) type {
return struct {
pub const Tag = enum {
todo,
error_directive,
warning_directive,
elif_without_if,
elif_after_else,
elifdef_without_if,
elifdef_after_else,
elifndef_without_if,
elifndef_after_else,
else_without_if,
else_after_else,
endif_without_if,
unknown_pragma,
line_simple_digit,
line_invalid_filename,
unterminated_conditional_directive,
invalid_preprocessing_directive,
macro_name_missing,
extra_tokens_directive_end,
expected_value_in_expr,
closing_paren,
to_match_paren,
to_match_brace,
to_match_bracket,
header_str_closing,
header_str_match,
string_literal_in_pp_expr,
float_literal_in_pp_expr,
defined_as_macro_name,
macro_name_must_be_identifier,
whitespace_after_macro_name,
hash_hash_at_start,
hash_hash_at_end,
pasting_formed_invalid,
missing_paren_param_list,
unterminated_macro_param_list,
invalid_token_param_list,
expected_comma_param_list,
hash_not_followed_param,
expected_filename,
empty_filename,
expected_invalid,
expected_eof,
expected_token,
expected_expr,
expected_integer_constant_expr,
missing_type_specifier,
missing_type_specifier_c23,
multiple_storage_class,
static_assert_failure,
static_assert_failure_message,
expected_type,
cannot_combine_spec,
duplicate_decl_spec,
restrict_non_pointer,
expected_external_decl,
expected_ident_or_l_paren,
missing_declaration,
func_not_in_root,
illegal_initializer,
extern_initializer,
spec_from_typedef,
param_before_var_args,
void_only_param,
void_param_qualified,
void_must_be_first_param,
invalid_storage_on_param,
threadlocal_non_var,
func_spec_non_func,
illegal_storage_on_func,
illegal_storage_on_global,
expected_stmt,
func_cannot_return_func,
func_cannot_return_array,
undeclared_identifier,
not_callable,
unsupported_str_cat,
static_func_not_global,
implicit_func_decl,
unknown_builtin,
implicit_builtin,
implicit_builtin_header_note,
expected_param_decl,
invalid_old_style_params,
expected_fn_body,
invalid_void_param,
unused_value,
continue_not_in_loop,
break_not_in_loop_or_switch,
unreachable_code,
duplicate_label,
previous_label,
undeclared_label,
case_not_in_switch,
duplicate_switch_case,
multiple_default,
previous_case,
expected_arguments,
expected_arguments_old,
expected_at_least_arguments,
invalid_static_star,
static_non_param,
array_qualifiers,
star_non_param,
variable_len_array_file_scope,
useless_static,
negative_array_size,
array_incomplete_elem,
array_func_elem,
static_non_outermost_array,
qualifier_non_outermost_array,
unterminated_macro_arg_list,
unknown_warning,
overflow,
int_literal_too_big,
indirection_ptr,
addr_of_rvalue,
addr_of_bitfield,
not_assignable,
ident_or_l_brace,
empty_enum,
redefinition,
previous_definition,
expected_identifier,
expected_str_literal,
expected_str_literal_in,
parameter_missing,
empty_record,
empty_record_size,
wrong_tag,
expected_parens_around_typename,
alignof_expr,
invalid_alignof,
invalid_sizeof,
macro_redefined,
generic_qual_type,
generic_array_type,
generic_func_type,
generic_duplicate,
generic_duplicate_here,
generic_duplicate_default,
generic_no_match,
escape_sequence_overflow,
invalid_universal_character,
incomplete_universal_character,
multichar_literal_warning,
invalid_multichar_literal,
wide_multichar_literal,
char_lit_too_wide,
char_too_large,
must_use_struct,
must_use_union,
must_use_enum,
redefinition_different_sym,
redefinition_incompatible,
redefinition_of_parameter,
invalid_bin_types,
comparison_ptr_int,
comparison_distinct_ptr,
incompatible_pointers,
invalid_argument_un,
incompatible_assign,
implicit_ptr_to_int,
invalid_cast_to_float,
invalid_cast_to_pointer,
invalid_cast_type,
qual_cast,
invalid_index,
invalid_subscript,
array_after,
array_before,
statement_int,
statement_scalar,
func_should_return,
incompatible_return,
incompatible_return_sign,
implicit_int_to_ptr,
func_does_not_return,
void_func_returns_value,
incompatible_arg,
incompatible_ptr_arg,
incompatible_ptr_arg_sign,
parameter_here,
atomic_array,
atomic_func,
atomic_incomplete,
addr_of_register,
variable_incomplete_ty,
parameter_incomplete_ty,
tentative_array,
deref_incomplete_ty_ptr,
alignas_on_func,
alignas_on_param,
minimum_alignment,
maximum_alignment,
negative_alignment,
align_ignored,
zero_align_ignored,
non_pow2_align,
pointer_mismatch,
static_assert_not_constant,
static_assert_missing_message,
pre_c23_compat,
unbound_vla,
array_too_large,
incompatible_ptr_init,
incompatible_ptr_init_sign,
incompatible_ptr_assign,
incompatible_ptr_assign_sign,
vla_init,
func_init,
incompatible_init,
empty_scalar_init,
excess_scalar_init,
excess_str_init,
excess_struct_init,
excess_array_init,
str_init_too_long,
arr_init_too_long,
invalid_typeof,
division_by_zero,
division_by_zero_macro,
builtin_choose_cond,
alignas_unavailable,
case_val_unavailable,
enum_val_unavailable,
incompatible_array_init,
array_init_str,
initializer_overrides,
previous_initializer,
invalid_array_designator,
negative_array_designator,
oob_array_designator,
invalid_field_designator,
no_such_field_designator,
empty_aggregate_init_braces,
ptr_init_discards_quals,
ptr_assign_discards_quals,
ptr_ret_discards_quals,
ptr_arg_discards_quals,
unknown_attribute,
ignored_attribute,
invalid_fallthrough,
cannot_apply_attribute_to_statement,
builtin_macro_redefined,
feature_check_requires_identifier,
missing_tok_builtin,
gnu_label_as_value,
expected_record_ty,
member_expr_not_ptr,
member_expr_ptr,
no_such_member,
malformed_warning_check,
invalid_computed_goto,
pragma_warning_message,
pragma_error_message,
pragma_message,
pragma_requires_string_literal,
poisoned_identifier,
pragma_poison_identifier,
pragma_poison_macro,
newline_eof,
empty_translation_unit,
omitting_parameter_name,
non_int_bitfield,
negative_bitwidth,
zero_width_named_field,
bitfield_too_big,
invalid_utf8,
implicitly_unsigned_literal,
invalid_preproc_operator,
invalid_preproc_expr_start,
c99_compat,
unexpected_character,
invalid_identifier_start_char,
unicode_zero_width,
unicode_homoglyph,
meaningless_asm_qual,
duplicate_asm_qual,
invalid_asm_str,
dollar_in_identifier_extension,
dollars_in_identifiers,
expanded_from_here,
skipping_macro_backtrace,
pragma_operator_string_literal,
unknown_gcc_pragma,
unknown_gcc_pragma_directive,
predefined_top_level,
incompatible_va_arg,
too_many_scalar_init_braces,
uninitialized_in_own_init,
gnu_statement_expression,
stmt_expr_not_allowed_file_scope,
gnu_imaginary_constant,
plain_complex,
complex_int,
qual_on_ret_type,
cli_invalid_standard,
cli_invalid_target,
cli_invalid_emulate,
cli_unknown_arg,
cli_error,
cli_unused_link_object,
cli_unknown_linker,
extra_semi,
func_field,
vla_field,
field_incomplete_ty,
flexible_in_union,
flexible_non_final,
flexible_in_empty,
duplicate_member,
binary_integer_literal,
gnu_va_macro,
builtin_must_be_called,
va_start_not_in_func,
va_start_fixed_args,
va_start_not_last_param,
attribute_not_enough_args,
attribute_too_many_args,
attribute_arg_invalid,
unknown_attr_enum,
attribute_requires_identifier,
declspec_not_enabled,
declspec_attr_not_supported,
deprecated_declarations,
deprecated_note,
unavailable,
unavailable_note,
warning_attribute,
error_attribute,
ignored_record_attr,
backslash_newline_escape,
array_size_non_int,
cast_to_smaller_int,
gnu_switch_range,
empty_case_range,
non_standard_escape_char,
invalid_pp_stringify_escape,
vla,
float_overflow_conversion,
float_out_of_range,
float_zero_conversion,
float_value_changed,
float_to_int,
const_decl_folded,
const_decl_folded_vla,
redefinition_of_typedef,
undefined_macro,
fn_macro_undefined,
preprocessing_directive_only,
missing_lparen_after_builtin,
offsetof_ty,
offsetof_incomplete,
offsetof_array,
pragma_pack_lparen,
pragma_pack_rparen,
pragma_pack_unknown_action,
pragma_pack_show,
pragma_pack_int,
pragma_pack_int_ident,
pragma_pack_undefined_pop,
pragma_pack_empty_stack,
cond_expr_type,
too_many_includes,
enumerator_too_small,
enumerator_too_large,
include_next,
include_next_outside_header,
enumerator_overflow,
enum_not_representable,
enum_too_large,
enum_fixed,
enum_prev_nonfixed,
enum_prev_fixed,
enum_different_explicit_ty,
enum_not_representable_fixed,
transparent_union_wrong_type,
transparent_union_one_field,
transparent_union_size,
transparent_union_size_note,
designated_init_invalid,
designated_init_needed,
ignore_common,
ignore_nocommon,
non_string_ignored,
local_variable_attribute,
ignore_cold,
ignore_hot,
ignore_noinline,
ignore_always_inline,
invalid_noreturn,
nodiscard_unused,
warn_unused_result,
invalid_vec_elem_ty,
vec_size_not_multiple,
invalid_imag,
invalid_real,
zero_length_array,
old_style_flexible_struct,
comma_deletion_va_args,
main_return_type,
expansion_to_defined,
invalid_int_suffix,
invalid_float_suffix,
invalid_octal_digit,
invalid_binary_digit,
exponent_has_no_digits,
hex_floating_constant_requires_exponent,
sizeof_returns_zero,
declspec_not_allowed_after_declarator,
declarator_name_tok,
type_not_supported_on_target,
bit_int,
unsigned_bit_int_too_small,
signed_bit_int_too_small,
bit_int_too_big,
keyword_macro,
ptr_arithmetic_incomplete,
callconv_not_supported,
pointer_arith_void,
sizeof_array_arg,
array_address_to_bool,
string_literal_to_bool,
constant_expression_conversion_not_allowed,
invalid_object_cast,
cli_invalid_fp_eval_method,
suggest_pointer_for_invalid_fp16,
bitint_suffix,
auto_type_extension,
auto_type_not_allowed,
auto_type_requires_initializer,
auto_type_requires_single_declarator,
auto_type_requires_plain_declarator,
invalid_cast_to_auto_type,
auto_type_from_bitfield,
array_of_auto_type,
auto_type_with_init_list,
missing_semicolon,
tentative_definition_incomplete,
forward_declaration_here,
gnu_union_cast,
invalid_union_cast,
cast_to_incomplete_type,
invalid_source_epoch,
fuse_ld_path,
invalid_rtlib,
unsupported_rtlib_gcc,
invalid_unwindlib,
incompatible_unwindlib,
gnu_asm_disabled,
extension_token_used,
complex_component_init,
complex_prefix_postfix_op,
not_floating_type,
argument_types_differ,
ms_search_rule,
ctrl_z_eof,
illegal_char_encoding_warning,
illegal_char_encoding_error,
ucn_basic_char_error,
ucn_basic_char_warning,
ucn_control_char_error,
ucn_control_char_warning,
c89_ucn_in_literal,
four_char_char_literal,
multi_char_char_literal,
missing_hex_escape,
unknown_escape_sequence,
attribute_requires_string,
unterminated_string_literal_warning,
unterminated_string_literal_error,
empty_char_literal_warning,
empty_char_literal_error,
unterminated_char_literal_warning,
unterminated_char_literal_error,
unterminated_comment,
def_no_proto_deprecated,
passing_args_to_kr,
unknown_type_name,
label_compound_end,
u8_char_lit,
malformed_embed_param,
malformed_embed_limit,
duplicate_embed_param,
unsupported_embed_param,
invalid_compound_literal_storage_class,
va_opt_lparen,
va_opt_rparen,
attribute_int_out_of_range,
identifier_not_normalized,
c23_auto_plain_declarator,
c23_auto_single_declarator,
c32_auto_requires_initializer,
c23_auto_scalar_init,
pub fn property(_: Tag) Properties {
return undefined;
}
};
};
}

View File

@ -0,0 +1 @@
pub const version_str: []const u8 = "bootstrap-stub";