Skip to content

Commit

Permalink
Remove more unused resources (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Jan 5, 2024
1 parent 708a18d commit 55ac3a7
Show file tree
Hide file tree
Showing 45 changed files with 275 additions and 288 deletions.
2 changes: 1 addition & 1 deletion .run/spice.run.xml
@@ -1,5 +1,5 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="build -O2 -d ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<configuration default="false" name="spice" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="build -O0 -ir ../../media/test-project/test.spice" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Spice" TARGET_NAME="spice" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Spice" RUN_TARGET_NAME="spice">
<envs>
<env name="LLVM_LIB_DIR" value="D:/LLVM/build-release/lib" />
<env name="LLVM_INCLUDE_DIR" value="D:/LLVM/llvm/include" />
Expand Down
22 changes: 20 additions & 2 deletions media/test-project/test.spice
@@ -1,4 +1,22 @@
import "std/time/timer";
type Letter struct {
string content
}

f<string> Letter.getContent() {
return this.content;
}

p Letter.setContent(string text) {
this.content = text;
}

f<int> main() {
dyn letter = Letter { "No content" };
letter.setContent("Hello World!");
printf("Content: %s\n", letter.getContent());
}

/*import "std/time/timer";
import "../../src-bootstrap/lexer/lexer";

f<int> main(int argc, string[] argv) {
Expand All @@ -15,7 +33,7 @@ f<int> main(int argc, string[] argv) {
}
timer.stop();
printf("Tokens: %d in %d ms\n", i, timer.getDurationInMillis());
}
}*/

/*type Visitable interface {
p print();
Expand Down
7 changes: 2 additions & 5 deletions src-bootstrap/ast/ast-nodes.spice
Expand Up @@ -18,7 +18,7 @@ public type CompileTimeValue struct {
long longValue
byte byteValue
char charValue
string stringValue
unsigned long stringValueOffset = 0l // Offset into vector of strings in GlobalResourceManager
bool boolValue
}

Expand All @@ -34,9 +34,6 @@ public type ASTNode struct /*: IVisitable*/ {
CodeLoc codeLoc
string errorMessage
//Vector<SymbolType> symbolTypes
CompileTimeValue compileTimeValue
string compileTimeStringValue
bool hasDirectCompileTimeValue = false
bool unreachable = false
//Vector<Vector<const Function*>> opFct // Operator overloading functions
}
Expand Down Expand Up @@ -533,7 +530,7 @@ public type ASTValueNode struct /*: IVisitable*/ {

public type ASTConstantNode struct /*: IVisitable*/ {
compose public ASTNode node

CompileTimeValue compileTimeValue
}

// ========================================================== FctCallNode ========================================================
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Expand Up @@ -113,7 +113,6 @@ set(SOURCES
model/StructBase.h
model/Struct.cpp
model/Struct.h
model/Interface.cpp
model/Interface.h
model/GenericType.cpp
model/GenericType.h
Expand Down
1 change: 0 additions & 1 deletion src/SourceFile.h
Expand Up @@ -35,7 +35,6 @@ enum CompileStageType : uint8_t {
PARSER,
CST_VISUALIZER,
AST_BUILDER,
AST_OPTIMIZER,
AST_VISUALIZER,
IMPORT_COLLECTOR,
SYMBOL_TABLE_BUILDER,
Expand Down
131 changes: 65 additions & 66 deletions src/ast/ASTBuilder.cpp
Expand Up @@ -523,7 +523,7 @@ std::any ASTBuilder::visitSpecifier(SpiceParser::SpecifierContext *ctx) {
else if (symbolType == SpiceParser::COMPOSE)
specifierNode->type = SpecifierNode::TY_COMPOSITION;
else
assert(false && "Unknown specifier type"); // GCOV_EXCL_LINE
assert_fail("Unknown specifier type"); // GCOV_EXCL_LINE
}

return concludeNode(specifierNode);
Expand Down Expand Up @@ -986,35 +986,37 @@ std::any ASTBuilder::visitConstant(SpiceParser::ConstantContext *ctx) {
auto constantNode = createNode<ConstantNode>(ctx);

// Enrich
CompileTimeValue &value = constantNode->compileTimeValue;
if (ctx->DOUBLE_LIT()) {
constantNode->type = ConstantNode::TYPE_DOUBLE;
constantNode->compileTimeValue.doubleValue = std::stod(ctx->DOUBLE_LIT()->toString());
value.doubleValue = std::stod(ctx->DOUBLE_LIT()->toString());
} else if (ctx->INT_LIT()) {
constantNode->type = ConstantNode::TYPE_INT;
constantNode->compileTimeValue.intValue = parseInt(constantNode, ctx->INT_LIT());
value.intValue = parseInt(constantNode, ctx->INT_LIT());
} else if (ctx->SHORT_LIT()) {
constantNode->type = ConstantNode::TYPE_SHORT;
constantNode->compileTimeValue.shortValue = parseShort(constantNode, ctx->SHORT_LIT());
value.shortValue = parseShort(constantNode, ctx->SHORT_LIT());
} else if (ctx->LONG_LIT()) {
constantNode->type = ConstantNode::TYPE_LONG;
constantNode->compileTimeValue.longValue = parseLong(constantNode, ctx->LONG_LIT());
value.longValue = parseLong(constantNode, ctx->LONG_LIT());
} else if (ctx->CHAR_LIT()) {
constantNode->type = ConstantNode::TYPE_CHAR;
constantNode->compileTimeValue.charValue = parseChar(ctx->CHAR_LIT());
value.charValue = parseChar(ctx->CHAR_LIT());
} else if (ctx->STRING_LIT()) {
// Transfer ownership to the AST node
constantNode->compileTimeStringValue = parseString(ctx->STRING_LIT()->toString());
// Save a pointer to the string in the compile time value
constantNode->type = ConstantNode::TYPE_STRING;
constantNode->compileTimeValue.stringValue = constantNode->compileTimeStringValue.c_str();
value.stringValueOffset = resourceManager.compileTimeStringValues.size();
// Add the string to the global compile time string list
resourceManager.compileTimeStringValues.push_back(parseString(ctx->STRING_LIT()->toString()));
} else if (ctx->TRUE()) {
constantNode->type = ConstantNode::TYPE_BOOL;
constantNode->compileTimeValue.boolValue = true;
value.boolValue = true;
} else if (ctx->FALSE()) {
constantNode->type = ConstantNode::TYPE_BOOL;
constantNode->compileTimeValue.boolValue = false;
value.boolValue = false;
} else {
assert_fail("Unknown constant type"); // GCOV_EXCL_LINE
}
constantNode->hasDirectCompileTimeValue = true;

return concludeNode(constantNode);
}
Expand Down Expand Up @@ -1182,6 +1184,8 @@ std::any ASTBuilder::visitBaseDataType(SpiceParser::BaseDataTypeContext *ctx) {
baseDataTypeNode->type = BaseDataTypeNode::TYPE_CUSTOM;
else if (ctx->functionDataType())
baseDataTypeNode->type = BaseDataTypeNode::TYPE_FUNCTION;
else
assert_fail("Unknown base data type");

// Visit children
visitChildren(ctx);
Expand Down Expand Up @@ -1251,7 +1255,7 @@ std::any ASTBuilder::visitAssignOp(SpiceParser::AssignOpContext *ctx) {
else if (ctx->XOR_EQUAL())
assignExprNode->op = AssignExprNode::OP_XOR_EQUAL;
else
assert(false && "Unknown assign operator");
assert_fail("Unknown assign operator");
assignExprNode->hasOperator = true;

return nullptr;
Expand All @@ -1261,51 +1265,37 @@ std::any ASTBuilder::visitOverloadableOp(SpiceParser::OverloadableOpContext *ctx
auto fctNameNode = spice_pointer_cast<FctNameNode *>(parentStack.top());

// Enrich
if (ctx->PLUS()) {
fctNameNode->overloadedOperator = FctNameNode::OP_PLUS;
if (ctx->PLUS())
fctNameNode->name = OP_FCT_PLUS;
} else if (ctx->MINUS()) {
fctNameNode->overloadedOperator = FctNameNode::OP_MINUS;
else if (ctx->MINUS())
fctNameNode->name = OP_FCT_MINUS;
} else if (ctx->MUL()) {
fctNameNode->overloadedOperator = FctNameNode::OP_MUL;
else if (ctx->MUL())
fctNameNode->name = OP_FCT_MUL;
} else if (ctx->DIV()) {
fctNameNode->overloadedOperator = FctNameNode::OP_DIV;
else if (ctx->DIV())
fctNameNode->name = OP_FCT_DIV;
} else if (ctx->EQUAL()) {
fctNameNode->overloadedOperator = FctNameNode::OP_EQUAL;
else if (ctx->EQUAL())
fctNameNode->name = OP_FCT_EQUAL;
} else if (ctx->NOT_EQUAL()) {
fctNameNode->overloadedOperator = FctNameNode::OP_NOT_EQUAL;
else if (ctx->NOT_EQUAL())
fctNameNode->name = OP_FCT_NOT_EQUAL;
} else if (ctx->LESS().size() == 2) {
fctNameNode->overloadedOperator = FctNameNode::OP_SHL;
else if (ctx->LESS().size() == 2)
fctNameNode->name = OP_FCT_SHL;
} else if (ctx->GREATER().size() == 2) {
fctNameNode->overloadedOperator = FctNameNode::OP_SHR;
else if (ctx->GREATER().size() == 2)
fctNameNode->name = OP_FCT_SHR;
} else if (ctx->PLUS_EQUAL()) {
fctNameNode->overloadedOperator = FctNameNode::OP_PLUS_EQUAL;
else if (ctx->PLUS_EQUAL())
fctNameNode->name = OP_FCT_PLUS_EQUAL;
} else if (ctx->MINUS_EQUAL()) {
fctNameNode->overloadedOperator = FctNameNode::OP_MINUS_EQUAL;
else if (ctx->MINUS_EQUAL())
fctNameNode->name = OP_FCT_MINUS_EQUAL;
} else if (ctx->MUL_EQUAL()) {
fctNameNode->overloadedOperator = FctNameNode::OP_MUL_EQUAL;
else if (ctx->MUL_EQUAL())
fctNameNode->name = OP_FCT_MUL_EQUAL;
} else if (ctx->DIV_EQUAL()) {
fctNameNode->overloadedOperator = FctNameNode::OP_DIV_EQUAL;
else if (ctx->DIV_EQUAL())
fctNameNode->name = OP_FCT_DIV_EQUAL;
} else if (ctx->PLUS_PLUS()) {
fctNameNode->overloadedOperator = FctNameNode::OP_PLUS_PLUS;
else if (ctx->PLUS_PLUS())
fctNameNode->name = OP_FCT_POSTFIX_PLUS_PLUS;
} else if (ctx->MINUS_MINUS()) {
fctNameNode->overloadedOperator = FctNameNode::OP_MINUS_MINUS;
else if (ctx->MINUS_MINUS())
fctNameNode->name = OP_FCT_POSTFIX_MINUS_MINUS;
} else {
assert(false && "Unsupported overloadable operator"); // GCOV_EXCL_LINE
}
else
assert_fail("Unsupported overloadable operator"); // GCOV_EXCL_LINE

fctNameNode->fqName = fctNameNode->name;
fctNameNode->nameFragments.push_back(fctNameNode->name);

Expand Down Expand Up @@ -1339,21 +1329,35 @@ template <typename T> T *ASTBuilder::concludeNode(T *node) {
}

int32_t ASTBuilder::parseInt(ConstantNode *constantNode, TerminalNode *terminal) {
std::function<int32_t(const std::string &, int)> cb = [](const std::string &substr, int base) {
return std::stoi(substr, nullptr, base);
NumericParserCallback<int32_t> cb = [](const std::string &substr, int base, bool isSigned) -> int32_t {
// Prepare limits
const int64_t upperLimit = isSigned ? INT32_MAX : UINT32_MAX;
const int64_t lowerLimit = isSigned ? INT32_MIN : 0;
// Parse number and check for limits
const int64_t number = std::stoll(substr, nullptr, base);
if (number < lowerLimit || number > upperLimit)
throw std::out_of_range("Number out of range");
return static_cast<int32_t>(number);
};
return parseNumeric(constantNode, terminal, cb);
}

int16_t ASTBuilder::parseShort(ConstantNode *constantNode, TerminalNode *terminal) {
std::function<int16_t(const std::string &, int)> cb = [](const std::string &substr, int base) {
return (int16_t)std::stoi(substr, nullptr, base);
NumericParserCallback<int16_t> cb = [](const std::string &substr, int base, bool isSigned) -> int16_t {
// Prepare limits
const int64_t upperLimit = isSigned ? INT16_MAX : UINT16_MAX;
const int64_t lowerLimit = isSigned ? INT16_MIN : 0;
// Parse number and check for limits
const int64_t number = std::stoll(substr, nullptr, base);
if (number < lowerLimit || number > upperLimit)
throw std::out_of_range("Number out of range");
return static_cast<int16_t>(number);
};
return parseNumeric(constantNode, terminal, cb);
}

int64_t ASTBuilder::parseLong(ConstantNode *constantNode, TerminalNode *terminal) {
std::function<int64_t(const std::string &, int)> cb = [](const std::string &substr, int base) {
NumericParserCallback<int64_t> cb = [](const std::string &substr, int base, bool isSigned) -> int64_t {
return std::stoll(substr, nullptr, base);
};
return parseNumeric(constantNode, terminal, cb);
Expand Down Expand Up @@ -1402,40 +1406,35 @@ std::string ASTBuilder::parseString(std::string input) {
}

template <typename T>
T ASTBuilder::parseNumeric(ConstantNode *constantNode, TerminalNode *terminal, std::function<T(const std::string &, int)> cb) {
T ASTBuilder::parseNumeric(ConstantNode *constantNode, TerminalNode *terminal,
std::function<T(const std::string &, int, bool)> cb) {
const std::string input = terminal->toString();

// Set to signed if the input string ends with 'u'
if (constantNode)
constantNode->isSigned = input.ends_with('u');
// Set to signed if the input string does not end with 'u'
const bool isSigned = !input.ends_with('u');

try {
if (input.length() >= 3) {
const char c1 = input[0];
const char c2 = input[1];
const std::string subStr = input.substr(2);
if (c1 == '0') {
switch (c2) {
case 'd':
case 'D':
return cb(subStr, 10);
if (input[0] == '0') {
const std::string subStr = input.substr(2);
switch (input[1]) {
case 'b':
case 'B':
return cb(subStr, 2);
return cb(subStr, 2, isSigned);
case 'h':
case 'H':
case 'x':
case 'X':
return cb(subStr, 16);
return cb(subStr, 16, isSigned);
case 'o':
case 'O':
return cb(subStr, 8);
return cb(subStr, 8, isSigned);
default:
return cb(input, 10);
return cb(input, 10, isSigned);
}
}
}
return cb(input, 10);
return cb(input, 10, isSigned);
} catch (std::out_of_range &e) {
const CodeLoc codeLoc(terminal->getSymbol(), sourceFile);
throw ParserError(codeLoc, NUMBER_OUT_OF_RANGE, "The provided number is out of range");
Expand Down
4 changes: 2 additions & 2 deletions src/ast/ASTBuilder.h
Expand Up @@ -26,6 +26,7 @@ class ASTBuilder : private CompilerPass, public SpiceVisitor {
// Private type defs
using TerminalNode = antlr4::tree::TerminalNode;
using ParserRuleContext = antlr4::ParserRuleContext;
template <typename T> using NumericParserCallback = std::function<T(const std::string &, int, bool)>;

public:
// Constructors
Expand Down Expand Up @@ -134,8 +135,7 @@ class ASTBuilder : private CompilerPass, public SpiceVisitor {
int64_t parseLong(ConstantNode *constantNode, TerminalNode *terminal);
int8_t parseChar(TerminalNode *terminal);
static std::string parseString(std::string input);
template <typename T>
T parseNumeric(ConstantNode *constantNode, TerminalNode *terminal, std::function<T(const std::string &, int)> cb);
template <typename T> T parseNumeric(ConstantNode *constantNode, TerminalNode *terminal, NumericParserCallback<T> cb);
static void replaceEscapeChars(std::string &string);
std::string getIdentifier(TerminalNode *terminal);
};
Expand Down
2 changes: 1 addition & 1 deletion src/ast/ASTNodes.cpp
Expand Up @@ -208,7 +208,7 @@ CompileTimeValue TernaryExprNode::getCompileTimeValue() const {
// If the condition has no compile time value, we do not need to evaluate the true and false values
const LogicalOrExprNode *condition = ops[0];
if (!condition->hasCompileTimeValue())
return compileTimeValue;
return {};

// Check if condition always evaluates to 'true'
if (condition->getCompileTimeValue().boolValue) {
Expand Down

0 comments on commit 55ac3a7

Please sign in to comment.