Skip to content

Commit 1975ea4

Browse files
committed
fixed: slow steady changes to clean up compiler code
1 parent e0c6416 commit 1975ea4

File tree

6 files changed

+70
-70
lines changed

6 files changed

+70
-70
lines changed

compiler/include/astnodes.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,10 @@ typedef enum AstKind {
204204
Ast_Kind_Param,
205205
Ast_Kind_Argument,
206206
Ast_Kind_Call,
207-
Ast_Kind_Intrinsic_Call,
208207
Ast_Kind_Return,
209208
Ast_Kind_Address_Of,
210209
Ast_Kind_Dereference,
211210
Ast_Kind_Subscript,
212-
Ast_Kind_Slice,
213211
Ast_Kind_Field_Access,
214212
Ast_Kind_Unary_Field_Access,
215213
Ast_Kind_Pipe,
@@ -721,15 +719,16 @@ struct AstArgument {
721719
};
722720
struct AstSubscript {
723721
AstTyped_base;
724-
BinaryOp __unused_operation; // This will be set to Binary_Op_Subscript
722+
BinaryOp operation; // This will be set to Binary_Op_Subscript
725723
AstTyped *addr;
726724
AstTyped *expr;
727725

728726
Arguments *overload_args; // This is set of the binary operator is attempted to be overloaded
729727
// but isnt successful yet.
730728
AstBinaryOp *potential_substitute;
731729

732-
u64 elem_size;
730+
u32 elem_size;
731+
b32 is_slice : 1;
733732
};
734733
struct AstFieldAccess {
735734
AstTyped_base;
@@ -800,10 +799,8 @@ struct AstCall {
800799
Arguments args;
801800
i32 placeholder_argument_position;
802801

803-
union {
804-
AstTyped *callee;
805-
OnyxIntrinsic intrinsic;
806-
};
802+
AstTyped *callee;
803+
OnyxIntrinsic intrinsic;
807804

808805
VarArgKind va_kind;
809806
i32 ignored_return_value_count;

compiler/src/astnodes.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,10 @@ static const char* ast_node_names[] = {
6464
"PARAM",
6565
"ARGUMENT",
6666
"CALL",
67-
"INTRINSIC CALL",
6867
"RETURN",
6968
"ADDRESS OF",
7069
"DEREFERENCE",
7170
"ARRAY ACCESS",
72-
"SLICE",
7371
"FIELD ACCESS",
7472
"UNARY FIELD ACCESS",
7573
"PIPE",

compiler/src/checker.c

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ CHECK_FUNC(argument, AstArgument** parg) {
910910
}
911911

912912
CHECK_FUNC(resolve_callee, AstCall* call, AstTyped** effective_callee) {
913-
if (call->kind == Ast_Kind_Intrinsic_Call) return Check_Success;
913+
if (call->intrinsic) return Check_Success;
914914

915915
AstTyped* callee = (AstTyped *) strip_aliases((AstNode *) call->callee);
916916
AstTyped* original_callee = callee;
@@ -1102,13 +1102,10 @@ CHECK_FUNC(call, AstCall** pcall) {
11021102
// NOTE: If we are calling an intrinsic function, translate the
11031103
// call into an intrinsic call node.
11041104
if (callee->kind == Ast_Kind_Function && callee->is_intrinsic) {
1105-
call->kind = Ast_Kind_Intrinsic_Call;
1106-
call->callee = NULL;
1107-
11081105
token_toggle_end(callee->intrinsic_name);
11091106
char* intr_name = callee->intrinsic_name->text;
11101107

1111-
OnyxIntrinsic intrinsic = 0xffffffff;
1108+
OnyxIntrinsic intrinsic = ONYX_INTRINSIC_UNDEFINED;
11121109
const IntrinsicMap *im = &builtin_intrinsics[0];
11131110
while (im->name) {
11141111
if (!strcmp(im->name, intr_name)) {
@@ -1118,7 +1115,7 @@ CHECK_FUNC(call, AstCall** pcall) {
11181115
im++;
11191116
}
11201117

1121-
if (intrinsic == 0xffffffff) {
1118+
if (intrinsic == ONYX_INTRINSIC_UNDEFINED) {
11221119
ONYX_ERROR(callee->token->pos, Error_Critical, "Intrinsic not supported, '%s'.", intr_name);
11231120
token_toggle_end(callee->intrinsic_name);
11241121
return Check_Error;
@@ -2511,9 +2508,9 @@ CHECK_FUNC(subscript, AstSubscript** psub) {
25112508
ERROR(sub->token->pos, "Invalid type for left of slice creation.");
25122509
}
25132510

2514-
sub->kind = Ast_Kind_Slice;
25152511
sub->type = type_make_slice(context, of);
25162512
sub->elem_size = type_size_of(of);
2513+
sub->is_slice = 1;
25172514

25182515
return Check_Success;
25192516
}
@@ -3012,7 +3009,6 @@ CHECK_FUNC(expression, AstTyped** pexpr) {
30123009
case Ast_Kind_Unary_Op: retval = check_unaryop(context, (AstUnaryOp **) pexpr); break;
30133010
case Ast_Kind_Pipe: retval = check_pipe(context, (AstBinaryOp **) pexpr); break;
30143011

3015-
case Ast_Kind_Intrinsic_Call:
30163012
case Ast_Kind_Call: retval = check_call(context, (AstCall **) pexpr); break;
30173013
case Ast_Kind_Argument: retval = check_argument(context, (AstArgument **) pexpr); break;
30183014
case Ast_Kind_Block: retval = check_block(context, (AstBlock *) expr); break;
@@ -3035,7 +3031,6 @@ CHECK_FUNC(expression, AstTyped** pexpr) {
30353031

30363032
case Ast_Kind_Address_Of: retval = check_address_of(context, (AstAddressOf **) pexpr); break;
30373033
case Ast_Kind_Dereference: retval = check_dereference(context, (AstDereference *) expr); break;
3038-
case Ast_Kind_Slice:
30393034
case Ast_Kind_Subscript: retval = check_subscript(context, (AstSubscript **) pexpr); break;
30403035
case Ast_Kind_Field_Access: retval = check_field_access(context, (AstFieldAccess **) pexpr); break;
30413036
case Ast_Kind_Method_Call: retval = check_method_call(context, (AstBinaryOp **) pexpr); break;
@@ -3435,33 +3430,37 @@ CHECK_FUNC(directive_solidify, AstDirectiveSolidify** psolid) {
34353430

34363431
CHECK(expression, (AstTyped **) &solid->poly_proc);
34373432

3438-
if (solid->poly_proc && solid->poly_proc->kind == Ast_Kind_Directive_Solidify) {
3439-
AstFunction* potentially_resolved_proc = (AstFunction *) ((AstDirectiveSolidify *) solid->poly_proc)->resolved_proc;
3440-
if (!potentially_resolved_proc) return Check_Yield;
3441-
3442-
solid->poly_proc = potentially_resolved_proc;
3433+
if (!solid->poly_proc) {
3434+
ERROR(solid->token->pos, "Internal compiler error. The given procedure did not resolve correctly.");
34433435
}
34443436

3445-
if (!solid->poly_proc || solid->poly_proc->kind != Ast_Kind_Polymorphic_Proc) {
3446-
ERROR(solid->token->pos, "Expected polymorphic procedure in #solidify directive.");
3447-
}
3437+
switch (solid->poly_proc->kind) {
3438+
case Ast_Kind_Polymorphic_Proc:
3439+
bh_arr_each(AstPolySolution, sln, solid->known_polyvars) {
3440+
// HACK: This assumes that 'ast_type' and 'value' are at the same offset.
3441+
CHECK(expression, &sln->value);
34483442

3449-
bh_arr_each(AstPolySolution, sln, solid->known_polyvars) {
3450-
// HACK: This assumes that 'ast_type' and 'value' are at the same offset.
3451-
CHECK(expression, &sln->value);
3443+
if (node_is_type((AstNode *) sln->value)) {
3444+
sln->type = type_build_from_ast(context, sln->ast_type);
3445+
sln->kind = PSK_Type;
3446+
} else {
3447+
sln->kind = PSK_Value;
3448+
}
3449+
}
34523450

3453-
if (node_is_type((AstNode *) sln->value)) {
3454-
sln->type = type_build_from_ast(context, sln->ast_type);
3455-
sln->kind = PSK_Type;
3456-
} else {
3457-
sln->kind = PSK_Value;
3458-
}
3451+
solid->resolved_proc = polymorphic_proc_try_solidify(context, solid->poly_proc, solid->known_polyvars, solid->token);
3452+
break;
3453+
3454+
//case Ast_Kind_Poly_Union_Type:
3455+
//break;
3456+
3457+
default:
3458+
ERROR(solid->token->pos, "Unable to '#solidify' this. Expected a 'polymorphic procedure'.");
34593459
}
34603460

3461-
solid->resolved_proc = polymorphic_proc_try_solidify(context, solid->poly_proc, solid->known_polyvars, solid->token);
34623461
if (solid->resolved_proc == (AstNode *) &context->node_that_signals_a_yield) {
34633462
solid->resolved_proc = NULL;
3464-
YIELD(solid->token->pos, "Waiting for partially solidified procedure.");
3463+
YIELD(solid->token->pos, "Waiting for solidification to finish.");
34653464
}
34663465

34673466
// NOTE: Not a DirectiveSolidify.
@@ -4845,6 +4844,12 @@ CHECK_FUNC(type, AstType** ptype) {
48454844
break;
48464845
}
48474846

4847+
case Ast_Kind_Directive_Solidify: {
4848+
AstDirectiveSolidify **solidify = (AstDirectiveSolidify **) ptype;
4849+
CHECK(directive_solidify, solidify);
4850+
break;
4851+
}
4852+
48484853
default: break;
48494854
}
48504855

compiler/src/clone.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,10 @@ static inline i32 ast_kind_to_size(AstNode* node) {
7676
case Ast_Kind_Param: return sizeof(AstLocal);
7777
case Ast_Kind_Argument: return sizeof(AstArgument);
7878
case Ast_Kind_Call: return sizeof(AstCall);
79-
case Ast_Kind_Intrinsic_Call: return sizeof(AstCall);
8079
case Ast_Kind_Return: return sizeof(AstReturn);
8180
case Ast_Kind_Address_Of: return sizeof(AstAddressOf);
8281
case Ast_Kind_Dereference: return sizeof(AstDereference);
8382
case Ast_Kind_Subscript: return sizeof(AstSubscript);
84-
case Ast_Kind_Slice: return sizeof(AstSubscript);
8583
case Ast_Kind_Field_Access: return sizeof(AstFieldAccess);
8684
case Ast_Kind_Unary_Field_Access: return sizeof(AstUnaryFieldAccess);
8785
case Ast_Kind_Pipe: return sizeof(AstBinaryOp);
@@ -215,7 +213,6 @@ AstNode* ast_clone(Context *context, void* n) {
215213
C(AstDereference, expr);
216214
break;
217215

218-
case Ast_Kind_Slice:
219216
case Ast_Kind_Subscript:
220217
C(AstSubscript, addr);
221218
C(AstSubscript, expr);

compiler/src/parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,7 @@ static AstTyped* parse_factor(OnyxParser* parser) {
11761176
sub_node->token = open_bracket;
11771177
sub_node->addr = retval;
11781178
sub_node->expr = expr;
1179-
sub_node->__unused_operation = Binary_Op_Subscript;
1179+
sub_node->operation = Binary_Op_Subscript;
11801180

11811181
retval = (AstTyped *) sub_node;
11821182
expect_token(parser, ']');

compiler/src/wasm_emit.c

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,6 +1838,11 @@ EMIT_FUNC(unaryop, AstUnaryOp* unop) {
18381838
// this because many times for interoperability, it is nicer to get two primitive values for the pointer and
18391839
// count of a slice, instead of a pointer.
18401840
EMIT_FUNC(call, AstCall* call) {
1841+
if (call->intrinsic) {
1842+
emit_intrinsic_call(mod, pcode, call);
1843+
return;
1844+
}
1845+
18411846
bh_arr(WasmInstruction) code = *pcode;
18421847

18431848
u64 stack_top_idx = bh_imap_get(&mod->index_map, (u64) &mod->context->builtins.stack_top);
@@ -3338,7 +3343,6 @@ EMIT_FUNC(expression, AstTyped* expr) {
33383343
case Ast_Kind_Do_Block: emit_do_block(mod, &code, (AstDoBlock *) expr); break;
33393344
case Ast_Kind_Call: emit_call(mod, &code, (AstCall *) expr); break;
33403345
case Ast_Kind_Argument: emit_expression(mod, &code, ((AstArgument *) expr)->value); break;
3341-
case Ast_Kind_Intrinsic_Call: emit_intrinsic_call(mod, &code, (AstCall *) expr); break;
33423346
case Ast_Kind_Binary_Op: emit_binop(mod, &code, (AstBinaryOp *) expr); break;
33433347
case Ast_Kind_Unary_Op: emit_unaryop(mod, &code, (AstUnaryOp *) expr); break;
33443348
case Ast_Kind_Alias: emit_expression(mod, &code, ((AstAlias *) expr)->alias); break;
@@ -3379,6 +3383,32 @@ EMIT_FUNC(expression, AstTyped* expr) {
33793383

33803384
case Ast_Kind_Subscript: {
33813385
AstSubscript* sub = (AstSubscript *) expr;
3386+
3387+
if (sub->is_slice) {
3388+
emit_expression(mod, &code, sub->expr);
3389+
emit_struct_as_separate_values(mod, &code, sub->expr->type, 0); // nocheckin This should be optimized for range literals
3390+
3391+
u64 lo_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
3392+
u64 hi_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
3393+
3394+
WI(NULL, WI_DROP);
3395+
WIL(NULL, WI_LOCAL_SET, hi_local);
3396+
WIL(NULL, WI_LOCAL_TEE, lo_local);
3397+
if (sub->elem_size != 1) {
3398+
WID(NULL, WI_I32_CONST, sub->elem_size);
3399+
WI(NULL, WI_I32_MUL);
3400+
}
3401+
emit_expression(mod, &code, sub->addr);
3402+
WI(NULL, WI_I32_ADD);
3403+
WIL(NULL, WI_LOCAL_GET, hi_local);
3404+
WIL(NULL, WI_LOCAL_GET, lo_local);
3405+
WI(NULL, WI_I32_SUB);
3406+
3407+
local_raw_free(mod->local_alloc, lo_local);
3408+
local_raw_free(mod->local_alloc, hi_local);
3409+
break;
3410+
}
3411+
33823412
u64 offset = 0;
33833413
emit_subscript_location(mod, &code, sub, &offset);
33843414
emit_load_instruction(mod, &code, sub->type, offset);
@@ -3497,33 +3527,6 @@ EMIT_FUNC(expression, AstTyped* expr) {
34973527
break;
34983528
}
34993529

3500-
case Ast_Kind_Slice: {
3501-
AstSubscript* sl = (AstSubscript *) expr;
3502-
3503-
emit_expression(mod, &code, sl->expr);
3504-
emit_struct_as_separate_values(mod, &code, sl->expr->type, 0); // nocheckin This should be optimized for range literals
3505-
3506-
u64 lo_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
3507-
u64 hi_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
3508-
3509-
WI(NULL, WI_DROP);
3510-
WIL(NULL, WI_LOCAL_SET, hi_local);
3511-
WIL(NULL, WI_LOCAL_TEE, lo_local);
3512-
if (sl->elem_size != 1) {
3513-
WID(NULL, WI_I32_CONST, sl->elem_size);
3514-
WI(NULL, WI_I32_MUL);
3515-
}
3516-
emit_expression(mod, &code, sl->addr);
3517-
WI(NULL, WI_I32_ADD);
3518-
WIL(NULL, WI_LOCAL_GET, hi_local);
3519-
WIL(NULL, WI_LOCAL_GET, lo_local);
3520-
WI(NULL, WI_I32_SUB);
3521-
3522-
local_raw_free(mod->local_alloc, lo_local);
3523-
local_raw_free(mod->local_alloc, hi_local);
3524-
break;
3525-
}
3526-
35273530
case Ast_Kind_Size_Of: {
35283531
AstSizeOf* so = (AstSizeOf *) expr;
35293532
WID(NULL, WI_I32_CONST, so->size);

0 commit comments

Comments
 (0)