Skip to content

Commit bf86d7a

Browse files
committed
Breaking changes:
* Array<M, V, E> is now Array<M, E>, all arrays are now varying. * Removed IFunction1 from builtins, its no longer needed by the compiler. * Removed RSA initialization like [vary *], now just use Array<...> * Thinking of using sugar for array lists, not arrays. Non-breaking changes: * Added push/pop support for arrays. * This means our array list no longer needs optionals! * Added .capacity() for arrays. * Removed RawArray, it doesn't fit anymore since all RSAs are varying. * Fixed kldc test * Added an error for the rune type solver * We can now destroy an RSA with `() = arr;` to mirror structs. * Moved drop_into into builtins
1 parent 70173bf commit bf86d7a

File tree

130 files changed

+3259
-918
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+3259
-918
lines changed

Driver/src/build.vale

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ fn build_stuff(compiler_dir &Path, all_args &Array<imm, final, str>) {
101101
"Whether to reuse existing VAST",
102102
"false",
103103
"Whether to reuse the VAST created by (presumably) the last run."),
104+
Flag(
105+
"--override_known_live_true",
106+
FLAG_BOOL(),
107+
"Whether to always gen-check, even if known live is true.",
108+
"false",
109+
"Whether to always gen-check, even if known live is true."),
104110
Flag(
105111
"-o",
106112
FLAG_STR(),

Midas/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ add_executable(midas
6060
src/c-compiler/function/expressions/constantstr.cpp
6161
src/c-compiler/function/expressions/localload.cpp
6262
src/c-compiler/function/expressions/while.cpp
63-
src/c-compiler/function/expressions/constructunknownsizearray.cpp
63+
src/c-compiler/function/expressions/newimmruntimesizearray.cpp
64+
src/c-compiler/function/expressions/newmutruntimesizearray.cpp
6465
src/c-compiler/function/expressions/staticarrayfromcallable.cpp
6566
src/c-compiler/function/expressions/newarrayfromvalues.cpp
6667

Midas/src/c-compiler/function/expression.cpp

Lines changed: 146 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -265,15 +265,6 @@ Ref translateExpressionInner(
265265
std::vector<Ref> argExprRefs = { consumerRef, elementRef };
266266

267267
buildCall(globalState, functionState, bodyBuilder, consumerMethod, argExprRefs);
268-
//
269-
// auto consumerInterfaceMT = dynamic_cast<InterfaceKind*>(consumerType->kind);
270-
// assert(consumerInterfaceMT);
271-
// int indexInEdge = globalState->getInterfaceMethodIndex(consumerInterfaceMT, consumerMethod);
272-
// auto methodFunctionPtrLE =
273-
// globalState->getRegion(consumerType)
274-
// ->getInterfaceMethodFunctionPtr(functionState, bodyBuilder, consumerType, consumerRef, indexInEdge);
275-
// buildInterfaceCall(
276-
// globalState, functionState, bodyBuilder, consumerMethod, methodFunctionPtrLE, argExprRefs, 0);
277268
});
278269

279270
if (arrayType->ownership == Ownership::OWN) {
@@ -289,20 +280,150 @@ Ref translateExpressionInner(
289280
assert(false);
290281
}
291282

292-
293283
globalState->getRegion(consumerType)
294284
->dealias(
295285
AFL("DestroySSAIntoF"), functionState, builder, consumerType, consumerRef);
296286

297287
return makeEmptyTupleRef(globalState);
298-
} else if (auto destroyRuntimeSizedArrayIntoFunction = dynamic_cast<DestroyRuntimeSizedArray*>(expr)) {
299-
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
300-
auto consumerType = destroyRuntimeSizedArrayIntoFunction->consumerType;
301-
auto arrayKind = destroyRuntimeSizedArrayIntoFunction->arrayKind;
302-
auto arrayExpr = destroyRuntimeSizedArrayIntoFunction->arrayExpr;
303-
auto consumerExpr = destroyRuntimeSizedArrayIntoFunction->consumerExpr;
304-
auto consumerMethod = destroyRuntimeSizedArrayIntoFunction->consumerMethod;
305-
auto arrayType = destroyRuntimeSizedArrayIntoFunction->arrayType;
288+
} else if (auto pushRuntimeSizedArray = dynamic_cast<PushRuntimeSizedArray*>(expr)) {
289+
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
290+
auto arrayExpr = pushRuntimeSizedArray->arrayExpr;
291+
auto arrayType = pushRuntimeSizedArray->arrayType;
292+
auto arrayMT = dynamic_cast<RuntimeSizedArrayT*>(arrayType->kind);
293+
assert(arrayMT);
294+
bool arrayKnownLive = true;
295+
auto newcomerExpr = pushRuntimeSizedArray->newcomerExpr;
296+
auto newcomerType = pushRuntimeSizedArray->newcomerType;
297+
bool newcomerKnownLive = true;
298+
299+
auto arrayRef = translateExpression(globalState, functionState, blockState, builder, arrayExpr);
300+
globalState->getRegion(arrayType)
301+
->checkValidReference(FL(), functionState, builder, arrayType, arrayRef);
302+
303+
auto arrayLenRef =
304+
globalState->getRegion(arrayType)
305+
->getRuntimeSizedArrayLength(
306+
functionState, builder, arrayType, arrayRef, arrayKnownLive);
307+
auto arrayLenLE =
308+
globalState->getRegion(globalState->metalCache->i32Ref)
309+
->checkValidReference(FL(),
310+
functionState, builder, globalState->metalCache->i32Ref, arrayLenRef);
311+
312+
auto arrayCapacityRef =
313+
globalState->getRegion(arrayType)
314+
->getRuntimeSizedArrayCapacity(
315+
functionState, builder, arrayType, arrayRef, arrayKnownLive);
316+
auto arrayCapacityLE =
317+
globalState->getRegion(globalState->metalCache->i32Ref)
318+
->checkValidReference(FL(),
319+
functionState, builder, globalState->metalCache->i32Ref, arrayCapacityRef);
320+
321+
auto hasSpaceLE = LLVMBuildICmp(builder, LLVMIntULT, arrayLenLE, arrayCapacityLE, "hasSpace");
322+
buildIf(globalState, functionState, builder, hasSpaceLE, [globalState](LLVMBuilderRef bodyBuilder) {
323+
buildPrint(globalState, bodyBuilder, "Error: Runtime-sized array has no room for new element!");
324+
});
325+
326+
auto newcomerRef = translateExpression(globalState, functionState, blockState, builder, newcomerExpr);
327+
globalState->getRegion(newcomerType)
328+
->checkValidReference(FL(), functionState, builder, newcomerType, newcomerRef);
329+
330+
globalState->getRegion(arrayType)
331+
->pushRuntimeSizedArrayNoBoundsCheck(functionState, builder, arrayType, arrayMT, arrayRef, arrayKnownLive, arrayLenRef, newcomerRef);
332+
333+
globalState->getRegion(arrayType)
334+
->dealias(
335+
AFL("pushRuntimeSizedArrayNoBoundsCheck"), functionState, builder, arrayType, arrayRef);
336+
337+
return makeEmptyTupleRef(globalState);
338+
} else if (auto popRuntimeSizedArray = dynamic_cast<PopRuntimeSizedArray*>(expr)) {
339+
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
340+
auto arrayExpr = popRuntimeSizedArray->arrayExpr;
341+
auto arrayType = popRuntimeSizedArray->arrayType;
342+
auto arrayMT = dynamic_cast<RuntimeSizedArrayT*>(arrayType->kind);
343+
assert(arrayMT);
344+
bool arrayKnownLive = true;
345+
346+
auto arrayRef = translateExpression(globalState, functionState, blockState, builder, arrayExpr);
347+
globalState->getRegion(arrayType)
348+
->checkValidReference(FL(), functionState, builder, arrayType, arrayRef);
349+
350+
auto arrayLenRef =
351+
globalState->getRegion(arrayType)
352+
->getRuntimeSizedArrayLength(
353+
functionState, builder, arrayType, arrayRef, arrayKnownLive);
354+
auto arrayLenLE =
355+
globalState->getRegion(globalState->metalCache->i32Ref)
356+
->checkValidReference(FL(),
357+
functionState, builder, globalState->metalCache->i32Ref, arrayLenRef);
358+
globalState->getRegion(globalState->metalCache->i32Ref)
359+
->checkValidReference(FL(),
360+
functionState, builder, globalState->metalCache->i32Ref, arrayLenRef);
361+
362+
auto indexLE = LLVMBuildSub(builder, arrayLenLE, constI32LE(globalState, 1), "index");
363+
auto indexRef =
364+
wrap(globalState->getRegion(globalState->metalCache->i32Ref), globalState->metalCache->i32Ref, indexLE);
365+
366+
auto hasElementsLE = LLVMBuildICmp(builder, LLVMIntNE, arrayLenLE, constI32LE(globalState, 0), "hasElements");
367+
buildIf(globalState, functionState, builder, hasElementsLE, [globalState](LLVMBuilderRef bodyBuilder) {
368+
buildPrint(globalState, bodyBuilder, "Error: Cannot pop element from empty runtime-sized array!");
369+
});
370+
371+
auto resultRef =
372+
globalState->getRegion(arrayType)
373+
->popRuntimeSizedArrayNoBoundsCheck(functionState, builder, arrayType, arrayMT, arrayRef, arrayKnownLive, indexRef);
374+
375+
globalState->getRegion(arrayType)
376+
->dealias(
377+
AFL("popRuntimeSizedArrayNoBoundsCheck"), functionState, builder, arrayType, arrayRef);
378+
379+
return resultRef;
380+
} else if (auto dmrsa = dynamic_cast<DestroyMutRuntimeSizedArray*>(expr)) {
381+
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
382+
auto arrayKind = dmrsa->arrayKind;
383+
auto arrayExpr = dmrsa->arrayExpr;
384+
auto arrayType = dmrsa->arrayType;
385+
bool arrayKnownLive = true;
386+
387+
auto arrayRef = translateExpression(globalState, functionState, blockState, builder, arrayExpr);
388+
globalState->getRegion(arrayType)
389+
->checkValidReference(FL(), functionState, builder, arrayType, arrayRef);
390+
auto arrayLenRef =
391+
globalState->getRegion(arrayType)
392+
->getRuntimeSizedArrayLength(
393+
functionState, builder, arrayType, arrayRef, arrayKnownLive);
394+
auto arrayLenLE =
395+
globalState->getRegion(globalState->metalCache->i32Ref)
396+
->checkValidReference(FL(),
397+
functionState, builder, globalState->metalCache->i32Ref, arrayLenRef);
398+
399+
auto hasElementsLE = LLVMBuildICmp(builder, LLVMIntNE, arrayLenLE, constI32LE(globalState, 0), "hasElements");
400+
buildIf(globalState, functionState, builder, hasElementsLE, [globalState](LLVMBuilderRef bodyBuilder) {
401+
buildPrint(globalState, bodyBuilder, "Error: Destroying non-empty array!");
402+
});
403+
404+
if (arrayType->ownership == Ownership::OWN) {
405+
globalState->getRegion(arrayType)
406+
->discardOwningRef(FL(), functionState, blockState, builder, arrayType, arrayRef);
407+
} else if (arrayType->ownership == Ownership::SHARE) {
408+
// We dont decrement anything here, we're only here because we already hit zero.
409+
410+
// Free it!
411+
globalState->getRegion(arrayType)
412+
->deallocate(
413+
AFL("DestroyRSAIntoF"), functionState, builder, arrayType, arrayRef);
414+
} else {
415+
assert(false);
416+
}
417+
418+
return makeEmptyTupleRef(globalState);
419+
} else if (auto dirsa = dynamic_cast<DestroyImmRuntimeSizedArray*>(expr)) {
420+
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
421+
auto consumerType = dirsa->consumerType;
422+
auto arrayKind = dirsa->arrayKind;
423+
auto arrayExpr = dirsa->arrayExpr;
424+
auto consumerExpr = dirsa->consumerExpr;
425+
auto consumerMethod = dirsa->consumerMethod;
426+
auto arrayType = dirsa->arrayType;
306427
bool arrayKnownLive = true;
307428

308429
auto arrayRef = translateExpression(globalState, functionState, blockState, builder, arrayExpr);
@@ -331,7 +452,7 @@ Ref translateExpressionInner(
331452

332453
auto elementRef =
333454
globalState->getRegion(arrayType)
334-
->deinitializeElementFromRSA(
455+
->popRuntimeSizedArrayNoBoundsCheck(
335456
functionState, bodyBuilder, arrayType, arrayKind, arrayRef, arrayKnownLive, indexRef);
336457
std::vector<Ref> argExprRefs = { consumerRef, elementRef };
337458

@@ -457,7 +578,7 @@ Ref translateExpressionInner(
457578
auto arrayKind = runtimeSizedArrayStore->arrayKind;
458579
bool arrayKnownLive = runtimeSizedArrayStore->arrayKnownLive || globalState->opt->overrideKnownLiveTrue;
459580

460-
auto elementType = globalState->program->getRuntimeSizedArray(arrayKind)->rawArray->elementType;
581+
auto elementType = globalState->program->getRuntimeSizedArray(arrayKind)->elementType;
461582

462583
auto arrayRefLE = translateExpression(globalState, functionState, blockState, builder, arrayExpr);
463584
globalState->getRegion(arrayType)
@@ -533,9 +654,12 @@ Ref translateExpressionInner(
533654
} else if (auto newArrayFromValues = dynamic_cast<NewArrayFromValues*>(expr)) {
534655
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
535656
return translateNewArrayFromValues(globalState, functionState, blockState, builder, newArrayFromValues);
536-
} else if (auto constructRuntimeSizedArray = dynamic_cast<ConstructRuntimeSizedArray*>(expr)) {
657+
} else if (auto nirsa = dynamic_cast<NewImmRuntimeSizedArray*>(expr)) {
658+
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
659+
return translateNewImmRuntimeSizedArray(globalState, functionState, blockState, builder, nirsa);
660+
} else if (auto nmrsa = dynamic_cast<NewMutRuntimeSizedArray*>(expr)) {
537661
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
538-
return translateConstructRuntimeSizedArray(globalState, functionState, blockState, builder, constructRuntimeSizedArray);
662+
return translateNewMutRuntimeSizedArray(globalState, functionState, blockState, builder, nmrsa);
539663
} else if (auto staticArrayFromCallable = dynamic_cast<StaticArrayFromCallable*>(expr)) {
540664
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
541665
return translateStaticArrayFromCallable(globalState, functionState, blockState, builder, staticArrayFromCallable);

Midas/src/c-compiler/function/expressions/expressions.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,19 @@ Ref translateStaticArrayFromCallable(
9898
LLVMBuilderRef builder,
9999
StaticArrayFromCallable* staticArrayFromCallable);
100100

101-
Ref translateConstructRuntimeSizedArray(
101+
Ref translateNewImmRuntimeSizedArray(
102102
GlobalState* globalState,
103103
FunctionState* functionState,
104104
BlockState* blockState,
105105
LLVMBuilderRef builder,
106-
ConstructRuntimeSizedArray* constructRuntimeSizedArray);
106+
NewImmRuntimeSizedArray* constructRuntimeSizedArray);
107+
108+
Ref translateNewMutRuntimeSizedArray(
109+
GlobalState* globalState,
110+
FunctionState* functionState,
111+
BlockState* blockState,
112+
LLVMBuilderRef builder,
113+
NewMutRuntimeSizedArray* constructRuntimeSizedArray);
107114

108115
Ref translateConstantStr(
109116
AreaAndFileAndLine from,

Midas/src/c-compiler/function/expressions/newarrayfromvalues.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ Ref translateNewArrayFromValues(
2222
globalState, functionState, blockState, builder, newArrayFromValues->sourceExprs);
2323
auto ssaDefM = globalState->program->getStaticSizedArray(newArrayFromValues->arrayKind);
2424
for (auto elementLE : elementsLE) {
25-
globalState->getRegion(ssaDefM->rawArray->elementType)
25+
globalState->getRegion(ssaDefM->elementType)
2626
->checkValidReference(
27-
FL(), functionState, builder, ssaDefM->rawArray->elementType, elementLE);
27+
FL(), functionState, builder, ssaDefM->elementType, elementLE);
2828
}
2929

3030
auto staticSizedArrayMT = dynamic_cast<StaticSizedArrayT*>(newArrayFromValues->arrayRefType->kind);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <iostream>
2+
#include <region/common/common.h>
3+
#include "region/common/controlblock.h"
4+
#include "function/expressions/shared/elements.h"
5+
6+
#include "translatetype.h"
7+
8+
#include "function/expressions/shared/members.h"
9+
#include "function/expression.h"
10+
#include "function/expressions/shared/shared.h"
11+
#include "region/common/heap.h"
12+
13+
Ref translateNewImmRuntimeSizedArray(
14+
GlobalState* globalState,
15+
FunctionState* functionState,
16+
BlockState* blockState,
17+
LLVMBuilderRef builder,
18+
NewImmRuntimeSizedArray* constructRuntimeSizedArray) {
19+
20+
auto generatorType = constructRuntimeSizedArray->generatorType;
21+
auto generatorExpr = constructRuntimeSizedArray->generatorExpr;
22+
auto sizeKind = constructRuntimeSizedArray->sizeKind;
23+
auto sizeExpr = constructRuntimeSizedArray->sizeExpr;
24+
auto sizeType = constructRuntimeSizedArray->sizeType;
25+
auto elementType = constructRuntimeSizedArray->elementType;
26+
auto arrayRefType = constructRuntimeSizedArray->arrayRefType;
27+
28+
auto runtimeSizedArrayMT = dynamic_cast<RuntimeSizedArrayT*>(constructRuntimeSizedArray->arrayRefType->kind);
29+
30+
auto capacityRef = translateExpression(globalState, functionState, blockState, builder, sizeExpr);
31+
32+
auto generatorRef = translateExpression(globalState, functionState, blockState, builder, generatorExpr);
33+
globalState->getRegion(generatorType)->checkValidReference(FL(), functionState, builder,
34+
generatorType, generatorRef);
35+
36+
// If we get here, arrayLT is a pointer to our counted struct.
37+
auto rsaRef =
38+
globalState->getRegion(arrayRefType)->constructRuntimeSizedArray(
39+
makeEmptyTupleRef(globalState),
40+
functionState,
41+
builder,
42+
arrayRefType,
43+
runtimeSizedArrayMT,
44+
capacityRef,
45+
runtimeSizedArrayMT->name->name);
46+
buildFlare(FL(), globalState, functionState, builder);
47+
globalState->getRegion(arrayRefType)->checkValidReference(FL(), functionState, builder,
48+
arrayRefType, rsaRef);
49+
50+
buildFlare(FL(), globalState, functionState, builder);
51+
fillRuntimeSizedArray(
52+
globalState,
53+
functionState,
54+
builder,
55+
arrayRefType,
56+
runtimeSizedArrayMT,
57+
elementType,
58+
generatorType,
59+
constructRuntimeSizedArray->generatorMethod,
60+
generatorRef,
61+
capacityRef,
62+
rsaRef);//getRuntimeSizedArrayContentsPtr(builder, rsaWrapperPtrLE));
63+
buildFlare(FL(), globalState, functionState, builder);
64+
65+
globalState->getRegion(sizeType)->dealias(AFL("ConstructRSA"), functionState, builder, sizeType, capacityRef);
66+
globalState->getRegion(generatorType)->dealias(AFL("ConstructRSA"), functionState, builder, generatorType, generatorRef);
67+
68+
return rsaRef;
69+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <iostream>
2+
#include <region/common/common.h>
3+
#include "region/common/controlblock.h"
4+
#include "function/expressions/shared/elements.h"
5+
6+
#include "translatetype.h"
7+
8+
#include "function/expressions/shared/members.h"
9+
#include "function/expression.h"
10+
#include "function/expressions/shared/shared.h"
11+
#include "region/common/heap.h"
12+
13+
Ref translateNewMutRuntimeSizedArray(
14+
GlobalState* globalState,
15+
FunctionState* functionState,
16+
BlockState* blockState,
17+
LLVMBuilderRef builder,
18+
NewMutRuntimeSizedArray* constructRuntimeSizedArray) {
19+
20+
auto sizeKind = constructRuntimeSizedArray->sizeKind;
21+
auto sizeExpr = constructRuntimeSizedArray->sizeExpr;
22+
auto sizeType = constructRuntimeSizedArray->sizeType;
23+
auto elementType = constructRuntimeSizedArray->elementType;
24+
auto arrayRefType = constructRuntimeSizedArray->arrayRefType;
25+
26+
auto runtimeSizedArrayMT = dynamic_cast<RuntimeSizedArrayT*>(constructRuntimeSizedArray->arrayRefType->kind);
27+
28+
auto capacityRef = translateExpression(globalState, functionState, blockState, builder, sizeExpr);
29+
30+
// If we get here, arrayLT is a pointer to our counted struct.
31+
auto rsaRef =
32+
globalState->getRegion(arrayRefType)->constructRuntimeSizedArray(
33+
makeEmptyTupleRef(globalState),
34+
functionState,
35+
builder,
36+
arrayRefType,
37+
runtimeSizedArrayMT,
38+
capacityRef,
39+
runtimeSizedArrayMT->name->name);
40+
buildFlare(FL(), globalState, functionState, builder);
41+
globalState->getRegion(arrayRefType)->checkValidReference(FL(), functionState, builder,
42+
arrayRefType, rsaRef);
43+
44+
globalState->getRegion(sizeType)->dealias(AFL("ConstructRSA"), functionState, builder, sizeType, capacityRef);
45+
46+
return rsaRef;
47+
}

0 commit comments

Comments
 (0)