Skip to content

Commit

Permalink
Merge pull request #181 from onyx-lang/fix/eliminate-symres
Browse files Browse the repository at this point in the history
Fix: Eliminate symres phase
  • Loading branch information
brendanfh authored Jan 5, 2025
2 parents 00fd90b + 3b9d0b2 commit 2b5ddd6
Show file tree
Hide file tree
Showing 17 changed files with 1,792 additions and 2,482 deletions.
4 changes: 2 additions & 2 deletions build.bat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@echo off

REM Compile the compiler
set SOURCE_FILES=compiler/src/library_main.c compiler/cli/main.c compiler/src/astnodes.c compiler/src/builtins.c compiler/src/checker.c compiler/src/clone.c compiler/src/doc.c compiler/src/entities.c compiler/src/errors.c compiler/src/lex.c compiler/src/parser.c compiler/src/symres.c compiler/src/types.c compiler/src/utils.c compiler/src/wasm_emit.c compiler/src/wasm_runtime.c compiler/src/extensions.c
set SOURCE_FILES=compiler/src/library_main.c compiler/cli/main.c compiler/src/astnodes.c compiler/src/builtins.c compiler/src/checker.c compiler/src/clone.c compiler/src/doc.c compiler/src/entities.c compiler/src/errors.c compiler/src/lex.c compiler/src/parser.c compiler/src/types.c compiler/src/utils.c compiler/src/wasm_emit.c compiler/src/wasm_runtime.c compiler/src/extensions.c

if "%1" == "1" (
set FLAGS=/Od /MTd /Z7
Expand All @@ -23,7 +23,7 @@ if %ERRORLEVEL% neq 0 (
exit /b %ERRORLEVEL%
)

set SOURCE_FILES=compiler/src/library_main.c compiler/src/astnodes.c compiler/src/builtins.c compiler/src/checker.c compiler/src/clone.c compiler/src/doc.c compiler/src/entities.c compiler/src/errors.c compiler/src/lex.c compiler/src/parser.c compiler/src/symres.c compiler/src/types.c compiler/src/utils.c compiler/src/wasm_emit.c compiler/src/wasm_runtime.c compiler/src/extensions.c
set SOURCE_FILES=compiler/src/library_main.c compiler/src/astnodes.c compiler/src/builtins.c compiler/src/checker.c compiler/src/clone.c compiler/src/doc.c compiler/src/entities.c compiler/src/errors.c compiler/src/lex.c compiler/src/parser.c compiler/src/types.c compiler/src/utils.c compiler/src/wasm_emit.c compiler/src/wasm_runtime.c compiler/src/extensions.c
cl.exe %FLAGS% /Icompiler/include /std:c17 /TC %SOURCE_FILES% /link /DLL /IGNORE:4217 %LINK_OPTIONS% /OUT:onyx.dll

REM Don't continue if we had compilation errors. This prevents CI to succeed.
Expand Down
2 changes: 1 addition & 1 deletion compiler/build.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

C_FILES="library_main astnodes builtins checker clone doc entities errors lex parser symres types utils wasm_emit extensions "
C_FILES="library_main astnodes builtins checker clone doc entities errors lex parser types utils wasm_emit extensions "
LIBS="-lpthread -ldl -lm"
INCLUDES="-I./include -I../shared/include -I../shared/include/dyncall"

Expand Down
12 changes: 9 additions & 3 deletions compiler/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ static char *get_description_for_subcommand(char *path) {
int previous_cursor = cursor;
int name_length = uleb128_to_uint(d, &cursor);

if (strncmp("onyx-command-description", &d[cursor], name_length)) {
if (strncmp("onyx-command-description", (const char *) &d[cursor], name_length)) {
cursor = previous_cursor + section_length;
continue;
}
Expand Down Expand Up @@ -932,13 +932,19 @@ int main(int argc, char *argv[]) {
case ONYX_EVENT_ALL_TYPES_CHECKED:
break;

case ONYX_EVENT_PHASE_START:
break;

case ONYX_EVENT_SYMBOL_DEFINED:
// bh_printf("DEFINED SYMBOL AT %s:%d,%d\n",
// onyx_event_field_str(ctx, i, "filename"),
// onyx_event_field_int(ctx, i, "line"),
// onyx_event_field_int(ctx, i, "column")
// );
break;

case ONYX_EVENT_UNKNOWN:
break;
}
}
}
Expand All @@ -959,8 +965,8 @@ int main(int argc, char *argv[]) {

printf("\nStatistics:\n");
printf(" Time taken: %lf ms\n", (double) duration);
printf(" Processed %llu lines (%f lines/second).\n", lines, lines_per_sec);
printf(" Processed %llu tokens (%f tokens/second).\n", tokens, tokens_per_sec);
printf(" Processed %d lines (%f lines/second).\n", lines, lines_per_sec);
printf(" Processed %d tokens (%f tokens/second).\n", tokens, tokens_per_sec);
printf("\n");
}

Expand Down
16 changes: 10 additions & 6 deletions compiler/include/astnodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,6 @@ typedef enum AstFlags {
Ast_Flag_Address_Taken = BH_BIT(7),

// Type flags
Ast_Flag_Type_Is_Resolved = BH_BIT(8),

Ast_Flag_No_Clone = BH_BIT(9),

Ast_Flag_Cannot_Take_Addr = BH_BIT(10),
Expand Down Expand Up @@ -327,7 +325,7 @@ typedef enum AstFlags {

Ast_Flag_Constraint_Is_Expression = BH_BIT(28),

Ast_Flag_Has_Been_Scheduled_For_Emit = BH_BIT(29)
Ast_Flag_Has_Been_Scheduled_For_Emit = BH_BIT(29),
} AstFlags;

typedef enum UnaryOp {
Expand Down Expand Up @@ -1489,6 +1487,7 @@ struct AstFunction {
b32 is_intrinsic : 1;

b32 named_return_locals_added : 1;
b32 ready_for_body_to_be_checked : 1;
};

struct AstCaptureBlock {
Expand Down Expand Up @@ -1521,7 +1520,6 @@ struct AstPolyQuery {
AstFunction *function_header;

b32 error_on_fail : 1; // Whether or not to report errors on failing to match.
b32 successful_symres : 1; // If something successful happened in symbol resolution
};


Expand Down Expand Up @@ -1844,7 +1842,6 @@ void entity_heap_add_job(EntityHeap *entities, enum TypeMatch (*func)(Context *,
// If target_arr is null, the entities will be placed directly in the heap.
void add_entities_for_node(EntityHeap *entities, bh_arr(Entity *)* target_arr, AstNode* node, Scope* scope, Package* package);

void symres_entity(Context *context, Entity* ent);
void check_entity(Context *context, Entity* ent);
void emit_entity(Context *context, Entity* ent);

Expand Down Expand Up @@ -1915,6 +1912,11 @@ typedef struct OnyxDocInfo {
u32 next_file_id;
} OnyxDocInfo;

typedef enum CheckerMode {
CM_Dont_Resolve_Symbols = BH_BIT(1),
CM_Dont_Check_Case_Bodies = BH_BIT(2),
CM_Allow_Init_Expressions = BH_BIT(3),
} CheckerMode;

typedef struct CheckerData {
b32 expression_types_must_be_known;
Expand All @@ -1928,9 +1930,11 @@ typedef struct CheckerData {
bh_arr(bh_arr(AstLocal *)) named_return_values_stack;

u32 current_checking_level;
CheckerMode mode;

Scope *current_scope;
b32 report_unresolved_symbols;
bh_arr(Scope *) scope_stack;

b32 resolved_a_symbol;
} CheckerData;

Expand Down
12 changes: 4 additions & 8 deletions compiler/src/astnodes.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,9 +588,6 @@ b32 convert_numlit_to_type(Context *context, AstNumLit* num, Type* to_type, b32
return 0;
}

// TODO: This function should be able return a "yield" condition. There
// are a couple cases that need to yield in order to be correct, like
// polymorphic functions with a typeof for the return type.
TypeMatch unify_node_and_type_(Context *context, AstTyped** pnode, Type* type, b32 permanent) {
AstTyped* node = *pnode;
if (type == NULL) return TYPE_MATCH_FAILED;
Expand Down Expand Up @@ -773,14 +770,13 @@ TypeMatch unify_node_and_type_(Context *context, AstTyped** pnode, Type* type, b
// node does not match the given type:
//
// If the nodes type is a function type and that function has an automatic return
// value placeholder, fill in that placeholder with the actual type.
// value placeholder, wait for the return type to be solved by the function first.
// :AutoReturnType
if (node_type && node_type->kind == Type_Kind_Function
&& node_type->Function.return_type == context->types.auto_return
&& type->kind == Type_Kind_Function) {

node_type->Function.return_type = type->Function.return_type;
return TYPE_MATCH_SUCCESS;
return TYPE_MATCH_YIELD;
}

// If the node is an auto cast (~~) node, then check to see if the cast is legal
Expand Down Expand Up @@ -1555,7 +1551,7 @@ TypeMatch implicit_cast_to_bool(Context *context, AstTyped **pnode) {
return TYPE_MATCH_YIELD;
}

static char *sanitize_name(bh_allocator a, const char *name) {
static char *sanitize_name(bh_allocator a, char *name) {
if (!name) return name;

char *sanitized = bh_strdup(a, name);
Expand Down Expand Up @@ -1599,7 +1595,7 @@ char* get_function_assembly_name(Context *context, AstFunction* func) {
if (func->token) {
return bh_aprintf(context->ast_alloc,
"unnamed_at_%s_%d",
sanitize_name(context->scratch_alloc, func->token->pos.filename),
sanitize_name(context->scratch_alloc, (char *) func->token->pos.filename),
func->token->pos.line);
}

Expand Down
Loading

0 comments on commit 2b5ddd6

Please sign in to comment.