This commit is contained in:
David Gonzalez Martin 2025-06-05 04:42:39 -06:00
parent fb54aec7c9
commit f040360f3f
5 changed files with 89 additions and 5 deletions

View File

@ -5451,6 +5451,7 @@ compile_file = fn (arena: &Arena, compile_options: CompileFile) void
>is_compiler = string_equal(relative_file_path, "src/compiler.bbb"); >is_compiler = string_equal(relative_file_path, "src/compiler.bbb");
>outputh_path_dir = arena_join_string(arena, [ >outputh_path_dir = arena_join_string(arena, [
base_cache_dir, base_cache_dir,
#enum_name(#build_mode),
#select(is_compiler, "/compiler/", "/"), #select(is_compiler, "/compiler/", "/"),
#enum_name(compile_options.build_mode), #enum_name(compile_options.build_mode),
"_", "_",

View File

@ -605,7 +605,7 @@ void entry_point(Slice<char* const> arguments, Slice<char* const> envp)
bool has_debug_info = true; bool has_debug_info = true;
String relative_file_path_parts[] = { string_literal("tests/"), name, string_literal(".bbb") }; String relative_file_path_parts[] = { string_literal("tests/"), name, string_literal(".bbb") };
auto relative_file_path = arena_join_string(arena, array_to_slice(relative_file_path_parts)); auto relative_file_path = arena_join_string(arena, array_to_slice(relative_file_path_parts));
char* const arguments[] = char* const compiler_arguments[] =
{ {
(char*)compiler.pointer, (char*)compiler.pointer,
(char*)"compile", (char*)"compile",
@ -614,7 +614,7 @@ void entry_point(Slice<char* const> arguments, Slice<char* const> envp)
(char*)(has_debug_info ? "true" : "false"), (char*)(has_debug_info ? "true" : "false"),
0, 0,
}; };
Slice<char* const> arg_slice = array_to_slice(arguments); Slice<char* const> arg_slice = array_to_slice(compiler_arguments);
arg_slice.length -= 1; arg_slice.length -= 1;
auto execution = os_execute(arena, arg_slice, environment, {}); auto execution = os_execute(arena, arg_slice, environment, {});
auto success = execution.termination_kind == TerminationKind::exit && execution.termination_code == 0; auto success = execution.termination_kind == TerminationKind::exit && execution.termination_code == 0;
@ -625,6 +625,16 @@ void entry_point(Slice<char* const> arguments, Slice<char* const> envp)
print(string_literal("\n")); print(string_literal("\n"));
bb_fail(); bb_fail();
} }
char* const run_arguments[] =
{
(char*)compiler.pointer,
(char*)"compile",
(char*)relative_file_path.pointer,
(char*)build_mode_to_string(build_mode).pointer,
(char*)(has_debug_info ? "true" : "false"),
0,
};
break; break;
} }
} }

View File

@ -336,7 +336,7 @@ enum class TypeId
struct TypeInteger struct TypeInteger
{ {
u32 bit_count; u64 bit_count;
bool is_signed; bool is_signed;
}; };
@ -840,6 +840,7 @@ enum class ValueId
string_to_enum, string_to_enum,
local, local,
argument, argument,
build_mode,
}; };
struct ValueConstantInteger struct ValueConstantInteger
@ -1293,6 +1294,7 @@ struct Module
Type* first_function_type; Type* first_function_type;
Type* va_list_type; Type* va_list_type;
Type* build_mode_enum;
Value* void_value; Value* void_value;
Global* first_global; Global* first_global;
@ -2037,6 +2039,13 @@ fn Type* resolve_alias(Module* module, Type* type)
} }
fn u64 enum_bit_count(u64 highest_value)
{
auto needed_bit_count = 64 - (u64)clz(highest_value);
needed_bit_count = needed_bit_count ? needed_bit_count : 1;
return needed_bit_count;
}
struct ArgBuilder struct ArgBuilder
{ {
char* args[128]; char* args[128];

View File

@ -2609,6 +2609,52 @@ fn void copy_block(Module* module, Scope* parent_scope, BlockCopy copy)
} }
} }
fn Type* get_build_mode_enum(Module* module)
{
auto result = module->build_mode_enum;
if (!result)
{
String enum_names[] = {
string_literal("debug_none"),
string_literal("debug"),
string_literal("soft_optimize"),
string_literal("optimize_for_speed"),
string_literal("optimize_for_size"),
string_literal("aggressively_optimize_for_speed"),
string_literal("aggressively_optimize_for_size"),
};
auto enum_fields = arena_allocate<EnumField>(module->arena, array_length(enum_names));
u64 field_value = 0;
for (String enum_name : enum_names)
{
enum_fields[field_value] = {
.name = enum_name,
.value = field_value,
};
field_value += 1;
}
auto backing_type = integer_type(module, { .bit_count = array_length(enum_names) - 1, .is_signed = false });
result = type_allocate_init(module, {
.enumerator = {
.fields = enum_fields,
.backing_type = backing_type,
},
.id = TypeId::enumerator,
.name = string_literal("BuildMode"),
.scope = &module->scope,
});
}
assert(result);
return result;
}
fn void analyze_type(Module* module, Value* value, Type* expected_type, TypeAnalysis analysis) fn void analyze_type(Module* module, Value* value, Type* expected_type, TypeAnalysis analysis)
{ {
assert(!value->type); assert(!value->type);
@ -4341,6 +4387,17 @@ fn void analyze_type(Module* module, Value* value, Type* expected_type, TypeAnal
module->current_macro_instantiation = current_macro_instantiation; module->current_macro_instantiation = current_macro_instantiation;
module->current_function = current_function; module->current_function = current_function;
} break; } break;
case ValueId::build_mode:
{
value_type = get_build_mode_enum(module);
if (expected_type)
{
// typecheck(module, expected_type);
trap();
}
typecheck(module, expected_type, value_type);
} break;
default: unreachable(); default: unreachable();
} }

View File

@ -3,6 +3,7 @@
enum class ValueIntrinsic enum class ValueIntrinsic
{ {
align_of, align_of,
build_mode,
byte_size, byte_size,
enum_from_int, enum_from_int,
enum_name, enum_name,
@ -1153,6 +1154,7 @@ fn Token tokenize(Module* module)
String value_intrinsics[] = { String value_intrinsics[] = {
string_literal("align_of"), string_literal("align_of"),
string_literal("build_mode"),
string_literal("byte_size"), string_literal("byte_size"),
string_literal("enum_from_int"), string_literal("enum_from_int"),
string_literal("enum_name"), string_literal("enum_name"),
@ -1862,6 +1864,12 @@ fn Value* parse_left(Module* module, Scope* scope, ValueBuilder builder)
.id = ValueId::binary, .id = ValueId::binary,
}; };
} break; } break;
case ValueIntrinsic::build_mode:
{
*result = {
.id = ValueId::build_mode,
};
} break;
case ValueIntrinsic::count: unreachable(); case ValueIntrinsic::count: unreachable();
} }
} break; } break;
@ -3477,8 +3485,7 @@ void parse(Module* module)
}; };
} }
auto needed_bit_count = 64 - (u32)clz(highest_value); auto needed_bit_count = enum_bit_count(highest_value);
needed_bit_count = needed_bit_count ? needed_bit_count : 1;
if (!backing_type) if (!backing_type)
{ {