Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"version": 3,
"configurePresets": [
{
"name": "linux-base",
"hidden": true,
"generator": "Unix Makefiles",
"binaryDir": "${sourceDir}/build/${presetName}",
"installDir": "${sourceDir}/install/${presetName}",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Linux"
}
},
{
"name": "clang-debug",
"displayName": "Clang debug",
"inherits": [
"linux-base"
],
"hidden": false,
"cacheVariables": {
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_CXX_COMPILER": "clang++",
"CMAKE_EXPORT_COMPILE_COMMANDS": "On",
"ENABLE_PY_BINDINGS": "On",
"BUILD_DOCS": "Off"
}
},
{
"name": "clang-release",
"displayName": "Clang release",
"inherits": [
"linux-base"
],
"hidden": false,
"cacheVariables": {
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_CXX_COMPILER": "clang++",
"ENABLE_PY_BINDINGS": "On",
"BUILD_DOCS": "Off"
}
},
{
"name": "clang-sanitize",
"displayName": "Clang sanitize",
"inherits": [
"linux-base"
],
"hidden": false,
"cacheVariables": {
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_CXX_COMPILER": "clang++",
"USE_SANITIZER": "undefined,address,leak",
"USE_STATIC_ANALYZER": "clang-tidy",
"BUILD_DOCS": "Off"
}
},
{
"name": "clang-coverage",
"displayName": "Clang release with coverage",
"inherits": [
"linux-base"
],
"hidden": false,
"cacheVariables": {
"CMAKE_INSTALL_PREFIX": "${sourceDir}/out/install/${presetName}",
"CMAKE_BUILD_TYPE": "Release",
"CMAKE_CXX_COMPILER": "clang++-21",
"CODE_COVERAGE": "ON",
"BUILD_DOCS": "Off"
}
},
{
"name": "gcc-debug",
"displayName": "GCC debug",
"inherits": [
"linux-base"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_CXX_COMPILER": "g++",
"ENABLE_PY_BINDINGS": "On",
"BUILD_DOCS": "Off"
}
},
{
"name": "gcc-release",
"displayName": "GCC release",
"inherits": [
"linux-base"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_CXX_COMPILER": "g++",
"ENABLE_PY_BINDINGS": "On",
"BUILD_DOCS": "Off"
}
},
{
"name": "macos-base",
"hidden": true,
"generator": "Unix Makefiles",
"binaryDir": "${sourceDir}/build/${presetName}",
"installDir": "${sourceDir}/install/${presetName}",
"condition": {
"type": "equals",
"lhs": "${hostSystemName}",
"rhs": "Darwin"
}
},
{
"name": "macos-debug",
"displayName": "macOS debug",
"inherits": [
"macos-base"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"CMAKE_EXPORT_COMPILE_COMMANDS": "On",
"ENABLE_PY_BINDINGS": "On",
"BUILD_DOCS": "Off"
}
},
{
"name": "macos-release",
"displayName": "macOS Release",
"inherits": [
"macos-base"
],
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"ENABLE_PY_BINDINGS": "On",
"BUILD_DOCS": "Off"
}
},
{
"name": "docs",
"displayName": "Documentation",
"hidden": false,
"generator": "Ninja",
"binaryDir": "${sourceDir}/build/${presetName}",
"cacheVariables": {
"BUILD_DOCS": "On"
}
}
]
}
4 changes: 4 additions & 0 deletions hex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const char *hex::oprInstrEnumToStr(OprInstr oprInstr) {
return "SUB";
case SVC:
return "SVC";
case IN:
return "IN";
case OUT:
return "OUT";
default:
return "UNKNOWN";
}
Expand Down
2 changes: 1 addition & 1 deletion hex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ enum Instr {
NFIX = 0xF,
};

enum OprInstr { BRB = 0x0, ADD = 0x1, SUB = 0x2, SVC = 0x3 };
enum OprInstr { BRB = 0x0, ADD = 0x1, SUB = 0x2, SVC = 0x3, IN = 0x4, OUT = 0x5 };

enum class Syscall { EXIT = 0, WRITE = 1, READ = 2, NUM_VALUES };

Expand Down
28 changes: 22 additions & 6 deletions hexasm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ enum class Token {
SVC,
ADD,
SUB,
IN,
OUT,
OPR,
IDENTIFIER,
END_OF_FILE,
Expand Down Expand Up @@ -138,6 +140,10 @@ static const char *tokenEnumStr(Token token) {
return "ADD";
case Token::SUB:
return "SUB";
case Token::IN:
return "IN";
case Token::OUT:
return "OUT";
case Token::OPR:
return "OPR";
case Token::IDENTIFIER:
Expand Down Expand Up @@ -208,6 +214,10 @@ static hex::OprInstr tokenToOprInstr(Token token) {
return hex::OprInstr::ADD;
case Token::SUB:
return hex::OprInstr::SUB;
case Token::IN:
return hex::OprInstr::IN;
case Token::OUT:
return hex::OprInstr::OUT;
default:
throw std::runtime_error(
std::string("unexpected operand instrucion token: ") +
Expand Down Expand Up @@ -437,14 +447,14 @@ class InstrOp : public Directive {
public:
InstrOp(Token token, Token opcode) : Directive(token), opcode(opcode) {
if (opcode != Token::BRB && opcode != Token::ADD && opcode != Token::SUB &&
opcode != Token::SVC) {
opcode != Token::SVC && opcode != Token::IN && opcode != Token::OUT) {
throw InvalidOprError(opcode);
}
}
InstrOp(Location location, Token token, Token opcode)
: Directive(location, token), opcode(opcode) {
if (opcode != Token::BRB && opcode != Token::ADD && opcode != Token::SUB &&
opcode != Token::SVC) {
opcode != Token::SVC && opcode != Token::IN && opcode != Token::OUT) {
throw InvalidOprError(location, opcode);
}
}
Expand Down Expand Up @@ -512,6 +522,7 @@ class Lexer {
table.insert("BRZ", Token::BRZ);
table.insert("DATA", Token::DATA);
table.insert("FUNC", Token::FUNC);
table.insert("IN", Token::IN);
table.insert("LDAC", Token::LDAC);
table.insert("LDAI", Token::LDAI);
table.insert("LDAM", Token::LDAM);
Expand All @@ -520,6 +531,7 @@ class Lexer {
table.insert("LDBI", Token::LDBI);
table.insert("LDBM", Token::LDBM);
table.insert("OPR", Token::OPR);
table.insert("OUT", Token::OUT);
table.insert("PROC", Token::PROC);
table.insert("STAI", Token::STAI);
table.insert("STAM", Token::STAM);
Expand Down Expand Up @@ -962,17 +974,21 @@ class CodeGen {
}
}

/// Emit the binary.
void emitBin(std::string outputFilename) {
std::fstream outputFile(outputFilename, std::ios::out | std::ios::binary);
/// Emit a complete image (size-word + program + debug info) to a stream.
void emitImage(std::ostream &outputFile) {
// The first four bytes are the remaining binary size.
uint32_t programSizeWords = programSizeBytes >> 2;
outputFile.write(reinterpret_cast<const char *>(&programSizeWords),
sizeof(uint32_t));
// Emit the program.
emitProgramBin(outputFile);
emitDebugInfo(outputFile);
// Done.
}

/// Emit the binary.
void emitBin(std::string outputFilename) {
std::fstream outputFile(outputFilename, std::ios::out | std::ios::binary);
emitImage(outputFile);
outputFile.close();
}
};
Expand Down
11 changes: 7 additions & 4 deletions hexsim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,16 @@ int main(int argc, const char *argv[]) {
help(argv);
return 1;
}
hexsim::Processor p(std::cin, std::cout, maxCycles);
p.setTracing(trace);
p.load(filename, dumpBinary);
if (dumpBinary) {
// Dumping inspects a single image directly.
hexsim::Processor p(std::cin, std::cout, maxCycles);
p.load(filename, true);
return 0;
}
return p.run();
hexsim::System system(std::cin, std::cout, maxCycles);
system.setTracing(trace);
system.loadNetwork(filename);
return system.run();
} catch (std::exception &e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
Expand Down
Loading
Loading