Skip to content

Commit

Permalink
Breaking changes:
Browse files Browse the repository at this point in the history
 * 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
  • Loading branch information
Verdagon committed Dec 10, 2021
1 parent 70173bf commit bf86d7a
Show file tree
Hide file tree
Showing 130 changed files with 3,259 additions and 918 deletions.
6 changes: 6 additions & 0 deletions Driver/src/build.vale
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ fn build_stuff(compiler_dir &Path, all_args &Array<imm, final, str>) {
"Whether to reuse existing VAST",
"false",
"Whether to reuse the VAST created by (presumably) the last run."),
Flag(
"--override_known_live_true",
FLAG_BOOL(),
"Whether to always gen-check, even if known live is true.",
"false",
"Whether to always gen-check, even if known live is true."),
Flag(
"-o",
FLAG_STR(),
Expand Down
3 changes: 2 additions & 1 deletion Midas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ add_executable(midas
src/c-compiler/function/expressions/constantstr.cpp
src/c-compiler/function/expressions/localload.cpp
src/c-compiler/function/expressions/while.cpp
src/c-compiler/function/expressions/constructunknownsizearray.cpp
src/c-compiler/function/expressions/newimmruntimesizearray.cpp
src/c-compiler/function/expressions/newmutruntimesizearray.cpp
src/c-compiler/function/expressions/staticarrayfromcallable.cpp
src/c-compiler/function/expressions/newarrayfromvalues.cpp

Expand Down
168 changes: 146 additions & 22 deletions Midas/src/c-compiler/function/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,6 @@ Ref translateExpressionInner(
std::vector<Ref> argExprRefs = { consumerRef, elementRef };

buildCall(globalState, functionState, bodyBuilder, consumerMethod, argExprRefs);
//
// auto consumerInterfaceMT = dynamic_cast<InterfaceKind*>(consumerType->kind);
// assert(consumerInterfaceMT);
// int indexInEdge = globalState->getInterfaceMethodIndex(consumerInterfaceMT, consumerMethod);
// auto methodFunctionPtrLE =
// globalState->getRegion(consumerType)
// ->getInterfaceMethodFunctionPtr(functionState, bodyBuilder, consumerType, consumerRef, indexInEdge);
// buildInterfaceCall(
// globalState, functionState, bodyBuilder, consumerMethod, methodFunctionPtrLE, argExprRefs, 0);
});

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


globalState->getRegion(consumerType)
->dealias(
AFL("DestroySSAIntoF"), functionState, builder, consumerType, consumerRef);

return makeEmptyTupleRef(globalState);
} else if (auto destroyRuntimeSizedArrayIntoFunction = dynamic_cast<DestroyRuntimeSizedArray*>(expr)) {
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
auto consumerType = destroyRuntimeSizedArrayIntoFunction->consumerType;
auto arrayKind = destroyRuntimeSizedArrayIntoFunction->arrayKind;
auto arrayExpr = destroyRuntimeSizedArrayIntoFunction->arrayExpr;
auto consumerExpr = destroyRuntimeSizedArrayIntoFunction->consumerExpr;
auto consumerMethod = destroyRuntimeSizedArrayIntoFunction->consumerMethod;
auto arrayType = destroyRuntimeSizedArrayIntoFunction->arrayType;
} else if (auto pushRuntimeSizedArray = dynamic_cast<PushRuntimeSizedArray*>(expr)) {
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
auto arrayExpr = pushRuntimeSizedArray->arrayExpr;
auto arrayType = pushRuntimeSizedArray->arrayType;
auto arrayMT = dynamic_cast<RuntimeSizedArrayT*>(arrayType->kind);
assert(arrayMT);
bool arrayKnownLive = true;
auto newcomerExpr = pushRuntimeSizedArray->newcomerExpr;
auto newcomerType = pushRuntimeSizedArray->newcomerType;
bool newcomerKnownLive = true;

auto arrayRef = translateExpression(globalState, functionState, blockState, builder, arrayExpr);
globalState->getRegion(arrayType)
->checkValidReference(FL(), functionState, builder, arrayType, arrayRef);

auto arrayLenRef =
globalState->getRegion(arrayType)
->getRuntimeSizedArrayLength(
functionState, builder, arrayType, arrayRef, arrayKnownLive);
auto arrayLenLE =
globalState->getRegion(globalState->metalCache->i32Ref)
->checkValidReference(FL(),
functionState, builder, globalState->metalCache->i32Ref, arrayLenRef);

auto arrayCapacityRef =
globalState->getRegion(arrayType)
->getRuntimeSizedArrayCapacity(
functionState, builder, arrayType, arrayRef, arrayKnownLive);
auto arrayCapacityLE =
globalState->getRegion(globalState->metalCache->i32Ref)
->checkValidReference(FL(),
functionState, builder, globalState->metalCache->i32Ref, arrayCapacityRef);

auto hasSpaceLE = LLVMBuildICmp(builder, LLVMIntULT, arrayLenLE, arrayCapacityLE, "hasSpace");
buildIf(globalState, functionState, builder, hasSpaceLE, [globalState](LLVMBuilderRef bodyBuilder) {
buildPrint(globalState, bodyBuilder, "Error: Runtime-sized array has no room for new element!");
});

auto newcomerRef = translateExpression(globalState, functionState, blockState, builder, newcomerExpr);
globalState->getRegion(newcomerType)
->checkValidReference(FL(), functionState, builder, newcomerType, newcomerRef);

globalState->getRegion(arrayType)
->pushRuntimeSizedArrayNoBoundsCheck(functionState, builder, arrayType, arrayMT, arrayRef, arrayKnownLive, arrayLenRef, newcomerRef);

globalState->getRegion(arrayType)
->dealias(
AFL("pushRuntimeSizedArrayNoBoundsCheck"), functionState, builder, arrayType, arrayRef);

return makeEmptyTupleRef(globalState);
} else if (auto popRuntimeSizedArray = dynamic_cast<PopRuntimeSizedArray*>(expr)) {
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
auto arrayExpr = popRuntimeSizedArray->arrayExpr;
auto arrayType = popRuntimeSizedArray->arrayType;
auto arrayMT = dynamic_cast<RuntimeSizedArrayT*>(arrayType->kind);
assert(arrayMT);
bool arrayKnownLive = true;

auto arrayRef = translateExpression(globalState, functionState, blockState, builder, arrayExpr);
globalState->getRegion(arrayType)
->checkValidReference(FL(), functionState, builder, arrayType, arrayRef);

auto arrayLenRef =
globalState->getRegion(arrayType)
->getRuntimeSizedArrayLength(
functionState, builder, arrayType, arrayRef, arrayKnownLive);
auto arrayLenLE =
globalState->getRegion(globalState->metalCache->i32Ref)
->checkValidReference(FL(),
functionState, builder, globalState->metalCache->i32Ref, arrayLenRef);
globalState->getRegion(globalState->metalCache->i32Ref)
->checkValidReference(FL(),
functionState, builder, globalState->metalCache->i32Ref, arrayLenRef);

auto indexLE = LLVMBuildSub(builder, arrayLenLE, constI32LE(globalState, 1), "index");
auto indexRef =
wrap(globalState->getRegion(globalState->metalCache->i32Ref), globalState->metalCache->i32Ref, indexLE);

auto hasElementsLE = LLVMBuildICmp(builder, LLVMIntNE, arrayLenLE, constI32LE(globalState, 0), "hasElements");
buildIf(globalState, functionState, builder, hasElementsLE, [globalState](LLVMBuilderRef bodyBuilder) {
buildPrint(globalState, bodyBuilder, "Error: Cannot pop element from empty runtime-sized array!");
});

auto resultRef =
globalState->getRegion(arrayType)
->popRuntimeSizedArrayNoBoundsCheck(functionState, builder, arrayType, arrayMT, arrayRef, arrayKnownLive, indexRef);

globalState->getRegion(arrayType)
->dealias(
AFL("popRuntimeSizedArrayNoBoundsCheck"), functionState, builder, arrayType, arrayRef);

return resultRef;
} else if (auto dmrsa = dynamic_cast<DestroyMutRuntimeSizedArray*>(expr)) {
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
auto arrayKind = dmrsa->arrayKind;
auto arrayExpr = dmrsa->arrayExpr;
auto arrayType = dmrsa->arrayType;
bool arrayKnownLive = true;

auto arrayRef = translateExpression(globalState, functionState, blockState, builder, arrayExpr);
globalState->getRegion(arrayType)
->checkValidReference(FL(), functionState, builder, arrayType, arrayRef);
auto arrayLenRef =
globalState->getRegion(arrayType)
->getRuntimeSizedArrayLength(
functionState, builder, arrayType, arrayRef, arrayKnownLive);
auto arrayLenLE =
globalState->getRegion(globalState->metalCache->i32Ref)
->checkValidReference(FL(),
functionState, builder, globalState->metalCache->i32Ref, arrayLenRef);

auto hasElementsLE = LLVMBuildICmp(builder, LLVMIntNE, arrayLenLE, constI32LE(globalState, 0), "hasElements");
buildIf(globalState, functionState, builder, hasElementsLE, [globalState](LLVMBuilderRef bodyBuilder) {
buildPrint(globalState, bodyBuilder, "Error: Destroying non-empty array!");
});

if (arrayType->ownership == Ownership::OWN) {
globalState->getRegion(arrayType)
->discardOwningRef(FL(), functionState, blockState, builder, arrayType, arrayRef);
} else if (arrayType->ownership == Ownership::SHARE) {
// We dont decrement anything here, we're only here because we already hit zero.

// Free it!
globalState->getRegion(arrayType)
->deallocate(
AFL("DestroyRSAIntoF"), functionState, builder, arrayType, arrayRef);
} else {
assert(false);
}

return makeEmptyTupleRef(globalState);
} else if (auto dirsa = dynamic_cast<DestroyImmRuntimeSizedArray*>(expr)) {
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
auto consumerType = dirsa->consumerType;
auto arrayKind = dirsa->arrayKind;
auto arrayExpr = dirsa->arrayExpr;
auto consumerExpr = dirsa->consumerExpr;
auto consumerMethod = dirsa->consumerMethod;
auto arrayType = dirsa->arrayType;
bool arrayKnownLive = true;

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

auto elementRef =
globalState->getRegion(arrayType)
->deinitializeElementFromRSA(
->popRuntimeSizedArrayNoBoundsCheck(
functionState, bodyBuilder, arrayType, arrayKind, arrayRef, arrayKnownLive, indexRef);
std::vector<Ref> argExprRefs = { consumerRef, elementRef };

Expand Down Expand Up @@ -457,7 +578,7 @@ Ref translateExpressionInner(
auto arrayKind = runtimeSizedArrayStore->arrayKind;
bool arrayKnownLive = runtimeSizedArrayStore->arrayKnownLive || globalState->opt->overrideKnownLiveTrue;

auto elementType = globalState->program->getRuntimeSizedArray(arrayKind)->rawArray->elementType;
auto elementType = globalState->program->getRuntimeSizedArray(arrayKind)->elementType;

auto arrayRefLE = translateExpression(globalState, functionState, blockState, builder, arrayExpr);
globalState->getRegion(arrayType)
Expand Down Expand Up @@ -533,9 +654,12 @@ Ref translateExpressionInner(
} else if (auto newArrayFromValues = dynamic_cast<NewArrayFromValues*>(expr)) {
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
return translateNewArrayFromValues(globalState, functionState, blockState, builder, newArrayFromValues);
} else if (auto constructRuntimeSizedArray = dynamic_cast<ConstructRuntimeSizedArray*>(expr)) {
} else if (auto nirsa = dynamic_cast<NewImmRuntimeSizedArray*>(expr)) {
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
return translateNewImmRuntimeSizedArray(globalState, functionState, blockState, builder, nirsa);
} else if (auto nmrsa = dynamic_cast<NewMutRuntimeSizedArray*>(expr)) {
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
return translateConstructRuntimeSizedArray(globalState, functionState, blockState, builder, constructRuntimeSizedArray);
return translateNewMutRuntimeSizedArray(globalState, functionState, blockState, builder, nmrsa);
} else if (auto staticArrayFromCallable = dynamic_cast<StaticArrayFromCallable*>(expr)) {
buildFlare(FL(), globalState, functionState, builder, typeid(*expr).name());
return translateStaticArrayFromCallable(globalState, functionState, blockState, builder, staticArrayFromCallable);
Expand Down
11 changes: 9 additions & 2 deletions Midas/src/c-compiler/function/expressions/expressions.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,19 @@ Ref translateStaticArrayFromCallable(
LLVMBuilderRef builder,
StaticArrayFromCallable* staticArrayFromCallable);

Ref translateConstructRuntimeSizedArray(
Ref translateNewImmRuntimeSizedArray(
GlobalState* globalState,
FunctionState* functionState,
BlockState* blockState,
LLVMBuilderRef builder,
ConstructRuntimeSizedArray* constructRuntimeSizedArray);
NewImmRuntimeSizedArray* constructRuntimeSizedArray);

Ref translateNewMutRuntimeSizedArray(
GlobalState* globalState,
FunctionState* functionState,
BlockState* blockState,
LLVMBuilderRef builder,
NewMutRuntimeSizedArray* constructRuntimeSizedArray);

Ref translateConstantStr(
AreaAndFileAndLine from,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Ref translateNewArrayFromValues(
globalState, functionState, blockState, builder, newArrayFromValues->sourceExprs);
auto ssaDefM = globalState->program->getStaticSizedArray(newArrayFromValues->arrayKind);
for (auto elementLE : elementsLE) {
globalState->getRegion(ssaDefM->rawArray->elementType)
globalState->getRegion(ssaDefM->elementType)
->checkValidReference(
FL(), functionState, builder, ssaDefM->rawArray->elementType, elementLE);
FL(), functionState, builder, ssaDefM->elementType, elementLE);
}

auto staticSizedArrayMT = dynamic_cast<StaticSizedArrayT*>(newArrayFromValues->arrayRefType->kind);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <iostream>
#include <region/common/common.h>
#include "region/common/controlblock.h"
#include "function/expressions/shared/elements.h"

#include "translatetype.h"

#include "function/expressions/shared/members.h"
#include "function/expression.h"
#include "function/expressions/shared/shared.h"
#include "region/common/heap.h"

Ref translateNewImmRuntimeSizedArray(
GlobalState* globalState,
FunctionState* functionState,
BlockState* blockState,
LLVMBuilderRef builder,
NewImmRuntimeSizedArray* constructRuntimeSizedArray) {

auto generatorType = constructRuntimeSizedArray->generatorType;
auto generatorExpr = constructRuntimeSizedArray->generatorExpr;
auto sizeKind = constructRuntimeSizedArray->sizeKind;
auto sizeExpr = constructRuntimeSizedArray->sizeExpr;
auto sizeType = constructRuntimeSizedArray->sizeType;
auto elementType = constructRuntimeSizedArray->elementType;
auto arrayRefType = constructRuntimeSizedArray->arrayRefType;

auto runtimeSizedArrayMT = dynamic_cast<RuntimeSizedArrayT*>(constructRuntimeSizedArray->arrayRefType->kind);

auto capacityRef = translateExpression(globalState, functionState, blockState, builder, sizeExpr);

auto generatorRef = translateExpression(globalState, functionState, blockState, builder, generatorExpr);
globalState->getRegion(generatorType)->checkValidReference(FL(), functionState, builder,
generatorType, generatorRef);

// If we get here, arrayLT is a pointer to our counted struct.
auto rsaRef =
globalState->getRegion(arrayRefType)->constructRuntimeSizedArray(
makeEmptyTupleRef(globalState),
functionState,
builder,
arrayRefType,
runtimeSizedArrayMT,
capacityRef,
runtimeSizedArrayMT->name->name);
buildFlare(FL(), globalState, functionState, builder);
globalState->getRegion(arrayRefType)->checkValidReference(FL(), functionState, builder,
arrayRefType, rsaRef);

buildFlare(FL(), globalState, functionState, builder);
fillRuntimeSizedArray(
globalState,
functionState,
builder,
arrayRefType,
runtimeSizedArrayMT,
elementType,
generatorType,
constructRuntimeSizedArray->generatorMethod,
generatorRef,
capacityRef,
rsaRef);//getRuntimeSizedArrayContentsPtr(builder, rsaWrapperPtrLE));
buildFlare(FL(), globalState, functionState, builder);

globalState->getRegion(sizeType)->dealias(AFL("ConstructRSA"), functionState, builder, sizeType, capacityRef);
globalState->getRegion(generatorType)->dealias(AFL("ConstructRSA"), functionState, builder, generatorType, generatorRef);

return rsaRef;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <iostream>
#include <region/common/common.h>
#include "region/common/controlblock.h"
#include "function/expressions/shared/elements.h"

#include "translatetype.h"

#include "function/expressions/shared/members.h"
#include "function/expression.h"
#include "function/expressions/shared/shared.h"
#include "region/common/heap.h"

Ref translateNewMutRuntimeSizedArray(
GlobalState* globalState,
FunctionState* functionState,
BlockState* blockState,
LLVMBuilderRef builder,
NewMutRuntimeSizedArray* constructRuntimeSizedArray) {

auto sizeKind = constructRuntimeSizedArray->sizeKind;
auto sizeExpr = constructRuntimeSizedArray->sizeExpr;
auto sizeType = constructRuntimeSizedArray->sizeType;
auto elementType = constructRuntimeSizedArray->elementType;
auto arrayRefType = constructRuntimeSizedArray->arrayRefType;

auto runtimeSizedArrayMT = dynamic_cast<RuntimeSizedArrayT*>(constructRuntimeSizedArray->arrayRefType->kind);

auto capacityRef = translateExpression(globalState, functionState, blockState, builder, sizeExpr);

// If we get here, arrayLT is a pointer to our counted struct.
auto rsaRef =
globalState->getRegion(arrayRefType)->constructRuntimeSizedArray(
makeEmptyTupleRef(globalState),
functionState,
builder,
arrayRefType,
runtimeSizedArrayMT,
capacityRef,
runtimeSizedArrayMT->name->name);
buildFlare(FL(), globalState, functionState, builder);
globalState->getRegion(arrayRefType)->checkValidReference(FL(), functionState, builder,
arrayRefType, rsaRef);

globalState->getRegion(sizeType)->dealias(AFL("ConstructRSA"), functionState, builder, sizeType, capacityRef);

return rsaRef;
}
Loading

0 comments on commit bf86d7a

Please sign in to comment.