Skip to content

Commit 391b408

Browse files
committed
do some refactorings
1 parent 0ab10c9 commit 391b408

File tree

13 files changed

+252
-177
lines changed

13 files changed

+252
-177
lines changed

codegen/metac_codegen.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ void MetaCCodegen_doGlobal(metac_bytecode_ctx_t* ctx, metac_sema_decl_t* decl, u
309309
sema_decl_variable_t var = decl->sema_decl_variable;
310310
if (METAC_NODE(var.VarInitExpr) != emptyNode)
311311
{
312-
BCValue initVal;
312+
BCValue initVal = {0};
313313
MetaCCodegen_doExpr(ctx, var.VarInitExpr, &initVal, _Rvalue);
314314
}
315315
ctx->GlobalMemoryOffset += sz;
@@ -1173,7 +1173,7 @@ static void MetaCCodegen_doArrowExpr(metac_bytecode_ctx_t* ctx,
11731173
static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
11741174
metac_sema_expr_t* exp,
11751175
BCValue* result,
1176-
metac_value_type_t lValue)
1176+
metac_value_type_t lValueType)
11771177
{
11781178
const BackendInterface gen = *ctx->gen;
11791179
void* c = ctx->c;
@@ -1184,11 +1184,11 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
11841184
metac_expr_kind_t op = exp->Kind;
11851185
BCType expType = MetaCCodegen_GetBCType(ctx, exp->TypeIndex);
11861186

1187-
if (lValue == _Discard && !HasSideEffect(exp))
1187+
if (lValueType == _Discard && !HasSideEffect(exp))
11881188
{
11891189
return ;
11901190
}
1191-
else if (lValue == _Cond && exp->Kind == expr_signed_integer)
1191+
else if (lValueType == _Cond && exp->Kind == expr_signed_integer)
11921192
{
11931193
int32_t truthval = exp->ValueI64 & 0xffffffff
11941194
| exp->ValueI64 << 32;
@@ -1411,7 +1411,7 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
14111411
}
14121412
r = r->E2;
14131413
}
1414-
MetaCCodegen_doExpr(ctx, r, result, lValue);
1414+
MetaCCodegen_doExpr(ctx, r, result, lValueType);
14151415
} break;
14161416

14171417
case expr_addr:
@@ -1462,7 +1462,7 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
14621462

14631463
//metac_identifier_ptr_t idPtr = e->E1->Variable->VarIdentifier;
14641464
//metac_identifier_ptr_t vStorePtr = GetVStoreID(vstore, e->E1);
1465-
if (lValue != _Discard)
1465+
if (lValueType != _Discard)
14661466
{
14671467
gen.Set(c, result, &rhs);
14681468
}
@@ -1487,7 +1487,14 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
14871487
STACK_ARENA_ARRAY(BCValue, bcValues, 32, &ctx->Allocator);
14881488
uint32_t currentOffset = 0;
14891489
BCValue sz;
1490-
1490+
/*
1491+
// TODO don't know if this does mean an ARENA_ARRAY_ADD
1492+
// would lead to append behind the preallocated space
1493+
ARENA_ARRAY_ENSURE_SIZE(types, exp->TupleExprCount);
1494+
ARENA_ARRAY_ENSURE_SIZE(offsets, exp->TupleExprCount);
1495+
ARENA_ARRAY_ENSURE_SIZE(bcTypes, exp->TupleExprCount);
1496+
ARENA_ARRAY_ENSURE_SIZE(bcValues, exp->TupleExprCount);
1497+
*/
14911498

14921499
for(uint32_t i = 0; i < exp->TupleExprCount; i++)
14931500
{
@@ -1535,6 +1542,13 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
15351542
{
15361543
gen.Store32(c, &address, bcValues + i);
15371544
}
1545+
else if (TYPE_INDEX_KIND(te.TypeIndex) == type_index_array)
1546+
{
1547+
metac_type_array_t* arrayType = ArrayTypePtr(ctx->Sema, TYPE_INDEX_INDEX(te.TypeIndex));
1548+
uint32_t elemSize =
1549+
MetaCCodegen_GetStorageSize(ctx, MetaCCodegen_GetBCType(ctx, arrayType->ElementType));
1550+
// gen.MemCpy(c, MetaCCodegen_ComputeAddress(ctx, bcValues + i,l ))
1551+
}
15381552
else
15391553
{
15401554
StructMemberInit(c, result, offsets[i], bcValues + i, memberSz);
@@ -1656,7 +1670,7 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
16561670
} break;
16571671
case expr_variable:
16581672
{
1659-
if (MetaCCodegen_AccessVariable(ctx, exp->Variable, result, lValue))
1673+
if (MetaCCodegen_AccessVariable(ctx, exp->Variable, result, lValueType))
16601674
{
16611675
BCType bcType = MetaCCodegen_GetBCType(ctx, exp->Variable->TypeIndex);
16621676
uint32_t sz = MetaCCodegen_GetStorageSize(ctx, bcType);
@@ -1666,7 +1680,7 @@ static void MetaCCodegen_doExpr(metac_bytecode_ctx_t* ctx,
16661680
} break;
16671681
case expr_paren:
16681682
{
1669-
MetaCCodegen_doExpr(ctx, exp->E1, result, lValue);
1683+
MetaCCodegen_doExpr(ctx, exp->E1, result, lValueType);
16701684
} break;
16711685
case expr_compl:
16721686
{
@@ -2084,6 +2098,8 @@ void MetaCCodegen_doStmt(metac_bytecode_ctx_t* ctx,
20842098
}
20852099

20862100
MetaCCodegen_doStmt(ctx, forStmt->ForBody);
2101+
2102+
MetaCCodegen_doExpr(ctx, forStmt->ForPostLoop, 0, _Discard);
20872103
gen.Jmp(c, loopBegin);
20882104

20892105
if (METAC_NODE(forStmt->ForCond) != emptyNode)

libinterpret/printer_backend.c

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ typedef struct Printer
1616
{
1717
char* Buffer;
1818
const char* BufferStart;
19-
uint32_t BufferCapacity;
19+
uint32_t BufferSizeLeft;
2020

21-
uint32_t vIp;
21+
uint32_t VirualInstructionPointer;
2222
uint32_t CurrentIndent;
2323
bool LineIndented;
2424

@@ -42,9 +42,9 @@ typedef struct Printer
4242
void* getTypeInfoCtx;
4343
} Printer;
4444

45-
void static inline Printer_EnsureCapacity(Printer* self, uint32_t capacity)
45+
void static inline Printer_EnsureSizeLeft(Printer* self, uint32_t shouldBeLeft)
4646
{
47-
if (self->BufferCapacity < capacity)
47+
if (self->BufferSizeLeft < shouldBeLeft)
4848
{
4949
assert(0); // we don't support growing yet :->
5050
}
@@ -63,7 +63,7 @@ static inline void Printer_PutIndent(Printer* self)
6363
assert(!self->LineIndented);
6464
assert((self->CurrentIndent * 4) <= 128);
6565

66-
Printer_EnsureCapacity(self, 128);
66+
Printer_EnsureSizeLeft(self, 128);
6767
for(int i = 0; i < self->CurrentIndent; i++)
6868
{
6969
*self->Buffer++ = ' ';
@@ -83,12 +83,12 @@ static inline void Printer_PutNewlineIndent(Printer* self)
8383
static inline void Printer_PutStr(Printer* self, const char* str)
8484
{
8585
uint32_t length = 0;
86-
Printer_EnsureCapacity(self, 128);
86+
Printer_EnsureSizeLeft(self, 128);
8787

8888
if (!self->LineIndented)
8989
{
9090
Printer_PutIndent(self);
91-
Printer_EnsureCapacity(self, 128);
91+
Printer_EnsureSizeLeft(self, 128);
9292
}
9393
char c;
9494
if (str)
@@ -101,14 +101,14 @@ static inline void Printer_PutStr(Printer* self, const char* str)
101101

102102
if (length & 128)
103103
{
104-
self->BufferCapacity -= 128;
104+
self->BufferSizeLeft -= 128;
105105
length -= 128;
106-
Printer_EnsureCapacity(self, 128);
106+
Printer_EnsureSizeLeft(self, 128);
107107
}
108108
}
109109
}
110110

111-
self->BufferCapacity -= length;
111+
self->BufferSizeLeft -= length;
112112
}
113113

114114
static inline void Printer_PutChar(Printer *self, char c)
@@ -118,10 +118,10 @@ static inline void Printer_PutChar(Printer *self, char c)
118118
Printer_PutIndent(self);
119119
}
120120

121-
assert(self->BufferCapacity >= 1);
121+
assert(self->BufferSizeLeft >= 1);
122122

123123
*self->Buffer++ = c;
124-
self->BufferCapacity--;
124+
self->BufferSizeLeft--;
125125
}
126126

127127
static inline void Printer_PutQuotedStr(Printer* self, const char* str)
@@ -183,11 +183,11 @@ static inline void Printer_DecreaseIndent(Printer* self)
183183

184184
static inline void Printer_PutDouble(Printer* self, double d)
185185
{
186-
Printer_EnsureCapacity(self, 24);
186+
Printer_EnsureSizeLeft(self, 24);
187187

188188
uint32_t sz = fpconv_dtoa(d, self->Buffer);
189189
self->Buffer += sz;
190-
self->BufferCapacity -= sz;
190+
self->BufferSizeLeft -= sz;
191191
}
192192

193193
static inline void Printer_PutFloat(Printer* self, float f)
@@ -437,14 +437,14 @@ extern void Printer_StreamToFile(Printer* self, FILE* fd)
437437
assert(sz == bytes_written);
438438

439439
self->Buffer = cast(char*)self->BufferStart;
440-
self->BufferCapacity += sz;
440+
self->BufferSizeLeft += sz;
441441
}
442442

443443
static inline void Printer_Op3(Printer* self,
444444
const BCValue *result, const BCValue *lhs, const BCValue *rhs
445445
, const char* inst)
446446
{
447-
self->vIp += 2;
447+
self->VirualInstructionPointer += 2;
448448
Printer_PutStr(self, inst);
449449
Printer_PutChar(self, '(');
450450

@@ -472,7 +472,7 @@ static inline void Printer_Op2(Printer* self,
472472
const BCValue *lhs, const BCValue *rhs
473473
, const char* inst)
474474
{
475-
self->vIp += 2;
475+
self->VirualInstructionPointer += 2;
476476

477477
Printer_PutStr(self, inst);
478478
Printer_PutChar(self, '(');
@@ -491,7 +491,7 @@ static inline void Printer_Op1(Printer* self,
491491
const BCValue *lhs
492492
, const char* inst)
493493
{
494-
self->vIp += 2;
494+
self->VirualInstructionPointer += 2;
495495

496496
Printer_PutStr(self, inst);
497497

@@ -899,25 +899,25 @@ static inline void Printer_PrintLabel(Printer* self, const BCLabel* label)
899899

900900
static inline BCLabel Printer_GenLabel(Printer* self)
901901
{
902-
BCLabel result = {{self->vIp}};
903-
if (self->LastLabel != self->vIp)
902+
BCLabel result = {{self->VirualInstructionPointer}};
903+
if (self->LastLabel != self->VirualInstructionPointer)
904904
{
905905

906906
Printer_PrintLabel(self, &result);
907907
Printer_PutStr(self, " = GenLabel();");
908908
Printer_PutNewline(self);
909-
self->LastLabel = self->vIp;
909+
self->LastLabel = self->VirualInstructionPointer;
910910
}
911911
return result;
912912
}
913913

914914
static inline BCAddr Printer_BeginJmp(Printer* self)
915915
{
916-
BCAddr result = {self->vIp};
916+
BCAddr result = {self->VirualInstructionPointer};
917917

918918
Printer_PutStr(self, "BCAddr jmp");
919919
Printer_PutU32(self, result.addr);
920-
self->vIp += 2;
920+
self->VirualInstructionPointer += 2;
921921

922922
Printer_PutStr(self, " = BeginJmp();");
923923
Printer_PutNewline(self);
@@ -956,14 +956,14 @@ static inline CndJmpBegin Printer_BeginCndJmp(Printer* self, const BCValue* cond
956956
{
957957
CndJmpBegin result;
958958

959-
BCAddr at = {self->vIp};
959+
BCAddr at = {self->VirualInstructionPointer};
960960
result.at = at;
961961
result.cond = cast(BCValue*)cond;
962962
result.ifTrue = ifTrue;
963963

964964
Printer_PutStr(self, "CndJmpBegin ");
965965
Printer_PrintCndJmp(self, &result);
966-
self->vIp += 2;
966+
self->VirualInstructionPointer += 2;
967967

968968
Printer_PutStr(self, " = BeginCndJmp(");
969969

@@ -1010,7 +1010,7 @@ PR_OP3(Realloc)
10101010

10111011
static inline void Printer_ReadI32(Printer* self, const BCValue* val, const ReadI32_cb_t readCb, void* userCtx)
10121012
{
1013-
self->vIp += 2;
1013+
self->VirualInstructionPointer += 2;
10141014

10151015
Printer_PutStr(self, "ReadI32(");
10161016
Printer_PrintBCValue(self, val);
@@ -1054,7 +1054,7 @@ static inline void Printer_init_instance(Printer* instance)
10541054

10551055
instance->BufferStart = instance->Buffer = cast(char*)
10561056
instance->allocMemory(instance->allocCtx, initialSize, 0);
1057-
instance->BufferCapacity = initialSize;
1057+
instance->BufferSizeLeft = initialSize;
10581058
instance->ErrorInfoCount = 0;
10591059
instance->ErrorInfoCapacity = 1024;
10601060
instance->ErrorInfos = cast (ErrorInfo*)

os/metac_alloc.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,17 @@ void* Allocator_Calloc_(metac_alloc_t* alloc, uint32_t elemSize, uint32_t elemCo
308308
return arena->Memory;
309309
}
310310

311+
bool Arena_ContainsMemoryP(tagged_arena_t* arena, void* memP)
312+
{
313+
intptr_t memPi = (intptr_t) memP;
314+
intptr_t arenaMemPi = (intptr_t) arena->Memory;
315+
if (arenaMemPi <= memPi && (arenaMemPi + arena->Offset) < memPi)
316+
{
317+
return true;
318+
}
319+
return false;
320+
}
321+
311322
void* Allocator_Realloc_(metac_alloc_t* alloc, void* oldMem,
312323
uint32_t elemSize, uint32_t elemCount,
313324
const char* file, uint32_t line)
@@ -323,7 +334,7 @@ void* Allocator_Realloc_(metac_alloc_t* alloc, void* oldMem,
323334
for(i = 0; i < alloc->inuseArenasCount; i++)
324335
{
325336
tagged_arena_t* candidate = &alloc->Arenas[alloc->ArenasCapacity - (i + 1)];
326-
if (candidate->Memory == oldMem)
337+
if (Arena_ContainsMemoryP(candidate->Memory, oldMem))
327338
{
328339
oldArena = candidate;
329340
oldArenaPtr.Index = i;
@@ -355,6 +366,10 @@ void* Allocator_Realloc_(metac_alloc_t* alloc, void* oldMem,
355366
if (spaceLeft >= requestedSize) {
356367
oldArena->Offset = requestedSize;
357368
oldArena->SizeLeft = spaceLeft - requestedSize;
369+
if (requestedSize == 0)
370+
{
371+
return 0;
372+
}
358373
return oldMem;
359374
}
360375

parser/metac_lexer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ metac_token_t* MetaCLexerLexNextToken(metac_lexer_t* self,
12551255
lastNewline = newlinePtr;
12561256
offset = (newlinePtr - text);
12571257
state->Line++;
1258-
newlinePtr = (char*)memchr(lastNewline + 1, '\n', len - (lastNewline - text));
1258+
newlinePtr = (char*)memchr(lastNewline + 1, '\n', len - offset);
12591259
}
12601260
if (!slashPtr) assert(0);
12611261
offset = (slashPtr - text);

printer/metac_printer.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ static inline void PrintStringWithNewline(metac_printer_t* self,
106106
uint32_t pos = 0;
107107

108108
int32_t newline_pos = FindLastNewline(string, length);
109+
ARENA_ARRAY_ADD_N(self->StringMemory, string, length);
110+
109111
if (newline_pos != -1)
110112
{
111113
self->CurrentColumn = length - newline_pos;
@@ -114,14 +116,16 @@ static inline void PrintStringWithNewline(metac_printer_t* self,
114116
{
115117
self->CurrentColumn += length;
116118
}
119+
117120
}
118121

119122
static inline void PrintIdentifier(metac_printer_t* self,
120123
metac_identifier_ptr_t idPtr)
121124
{
125+
const char* ident = IdentifierPtrToCharPtr(self->IdentifierTable, idPtr);
126+
122127
if (idPtr.v == empty_identifier.v)
123128
assert(0); // One is not supposed to print the empty identifier
124-
const char* ident = IdentifierPtrToCharPtr(self->IdentifierTable, idPtr);
125129

126130
PrintString(self, ident, (uint32_t)strlen(ident));
127131
}
@@ -215,7 +219,7 @@ static inline void PrintF23(metac_printer_t* self, float value)
215219

216220
static inline void PrintType(metac_printer_t* self, decl_type_t* type);
217221
static inline void PrintParameterList(metac_printer_t* self,
218-
decl_parameter_t* Parameters);
222+
decl_parameter_t* Parameters);
219223

220224
static inline void PrintFunctionTypeWithIdentifier(metac_printer_t* self,
221225
decl_type_t* type,
@@ -705,13 +709,13 @@ static inline void PrintStmt(metac_printer_t* self, metac_stmt_t* stmt)
705709
} break;
706710
case stmt_break:
707711
{
708-
stmt_break_t* stmt_break = cast(stmt_break_t*) stmt;
712+
// stmt_break_t* stmt_break = cast(stmt_break_t*) stmt;
709713
PrintKeyword(self, tok_kw_break);
710714
PrintToken(self, tok_semicolon);
711715
} break;
712716
case stmt_continue:
713717
{
714-
stmt_continue_t* stmt_continue = cast(stmt_continue_t*) stmt;
718+
// stmt_continue_t* stmt_continue = cast(stmt_continue_t*) stmt;
715719
PrintKeyword(self, tok_kw_continue);
716720
PrintToken(self, tok_semicolon);
717721
} break;

0 commit comments

Comments
 (0)