Skip to content

Commit 102023f

Browse files
committed
Finished asm
1 parent 9281c95 commit 102023f

File tree

7 files changed

+61
-31
lines changed

7 files changed

+61
-31
lines changed

.gitignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.antlr/
2+
.vscode/
3+
.cache/
4+
.DS_Store/
5+
build/

asm/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ add_subdirectory(instruction-creator)
7575

7676
add_library(emulatorGenerated ${asm_ISA_SOURCES})
7777

78-
llvm_map_components_to_libnames(llvm_libs core executionengine mcjit ${LLVM_TARGETS_TO_BUILD})
78+
llvm_map_components_to_libnames(llvm_libs core executionengine mcjit interpreter ${LLVM_TARGETS_TO_BUILD})
7979
# this will also bring includes
8080
target_link_libraries(emulatorGenerated instructionCreator ${llvm_libs})
8181

asm/app.s

+31-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
1+
; x12, x13 are temps
12
fun draw_half_circle:
2-
; x9 - x, x10 - y, x14 - r, x11 - dir, x2 - color
3+
; x9 - xc, x10 - yc, x14 - r, x11 - dir, x2 - color
34
muladd x15, x14, x11, x10
45
putpxl x9, x15, x2
56
muladd x15, x14, x11, x9
67
putpxl x15, x10, x2
78
mul x15, x14, x11
89
sub x15, x9, x15
910
putpxl x15, x10, x2
10-
cmplt x15, x14, 0
11+
jnpos x14, quit
1112

1213
mov x12, 0 ; x12 is x
1314
mov x13, x14 ; x13 is y
1415

15-
cj x15, skip_ret
16-
ret
17-
skip_ret:
1816
shl x15, x14, 1
19-
sub x15, 3, x15
20-
; x15 is d
17+
sub x15, 3, x15 ; x15 is d
18+
; x16 is temp
2119

2220
loop:
2321
jnpos x15, d_not_positive
24-
sub x13, x13, 1
22+
sub x13, x13, 1 ; y--
2523
sub x16, x12, x13
2624
shl x16, x16, 2
2725
add x16, x16, 10
@@ -32,21 +30,33 @@ d_not_positive:
3230
add x16, x16, 6
3331
add x15, x15, x16
3432
draw:
35-
add x12, x12, 1
33+
add x12, x12, 1 ; x++
34+
35+
; x17 is x * dir, x19 is dir * x + xc
3636
mul x17, x11, x12
3737
add x19, x17, x9
38+
39+
; x18 is dir * y, x20 is dir * y + yc
3840
mul x18, x11, x13
3941
add x20, x18, x10
4042
putpxl x19, x20, x2
43+
44+
; x19 is xc - x * dir
4145
sub x19, x9, x17
42-
putpxl x19, x20, x2
46+
putpxl x19, x20, x2
47+
48+
; x19 is dir * y + xc, x20 is x * dir + yc
4349
add x19, x18, x9
4450
add x20, x17, x10
4551
putpxl x19, x20, x2
52+
53+
; x19 is xc - dir * y
4654
sub x19, x9, x18
4755
putpxl x19, x20, x2
56+
4857
cmpgte x19, x13, x12
4958
cj x19, loop
59+
quit:
5060
ret
5161

5262
fun main:
@@ -59,12 +69,15 @@ fun main:
5969
; x0 is cur_x, x1 is cur_y, x2 is color, x3 is dir_x, x4 is bc_delta, x5 is dir_y
6070
; x6 is x, x7 is y, x8 is background_color
6171
mov x11, 1
72+
mov x4, -16843009 ; init bc_delta
6273

6374
label22:
6475
label27:
6576
putpxl x6, x7, x8
6677
incjneq x6, 512, label27
78+
mov x6, 0
6779
incjneq x7, 256, label22
80+
mov x7, 0
6881

6982
add x9, x0, 30
7083
add x10, x1, 30
@@ -88,28 +101,31 @@ label27:
88101
call draw_half_circle
89102

90103
mov x14, 15
91-
104+
add x9, x0, 30
92105
add x10, x1, 33
93-
call draw_half_circle
94106
mov x11, 1
95107
call draw_half_circle
96108

97-
add x9, x0, 63
109+
; adjust positions
110+
add x9, x0, 63 ; cur_x + 2 * RADIUS + STEP
98111
cmpgt x12, x9, 511
99112
cmplt x13, x0, 3
100113
or x12, x12, x13
101114
sub x13, 0, x3
102-
select x3, x3, x13, x3
115+
select x3, x12, x13, x3
116+
103117
cmplt x12, x1, 3
104118
add x10, x1, 63
105119
cmpgt x13, x10, 255
106120
or x12, x12, x13
107121
sub x13, 0, x5
108-
select x5, x5, x13, x5
122+
select x5, x12, x13, x5
109123

110124
muladd x0, x3, 3, x0
111125
muladd x1, x5, 3, x1
112126

127+
; change colors
128+
113129
cmpneq x12, x8, 0
114130
cmpneq x13, x8, -1
115131
and x12, x12, x13

asm/instruction-creator/branchInstructionCreator.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ std::any branchInstructionCreator::visitBranch(asmParser::BranchContext* ctx) {
110110

111111
int64_t imm = std::stoll(ctx->IMM()->getText());
112112
Value* cmp = moduleCtx_.getBuilder().CreateICmp(
113-
CmpInst::Predicate::ICMP_SLE, getRegisterValue(regIdx),
113+
CmpInst::Predicate::ICMP_NE, inc,
114114
ConstantInt::get(Type::getInt64Ty(moduleCtx_.getContext()), imm));
115115

116116
BasicBlock* nextBB = desc_.getDescriptor(curFun_).getBB(nextLabel.value());

asm/instruction-creator/utils/utils.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
std::pair<OpType, int64_t> unpackRegOrImm(asmParser::RegOrImmContext* ctx) {
44
if (ctx->IMM()) {
5-
return {OpType::IMM, std::stoll(ctx->IMM()->getText())};
5+
bool minus = ctx->MINUS();
6+
int64_t val = std::stoll(ctx->IMM()->getText());
7+
val = minus ? -val : val;
8+
return {OpType::IMM, val};
69
}
710

811
return {OpType::REG, getRegisterIdx(ctx->REG()->getText())};

asm/isa-creator/isaCreator.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,15 @@ void isaCreator::exitInstruction(isaParser::InstructionContext* ctx) {
108108
}
109109

110110
void isaCreator::generateFunctionCreator(isaParser::IsaContext* ctx) {
111-
emulatorSource_.println(std::format("void* {}::{}(const std::string& fn) {{",
111+
emulatorSource_.println(std::format("void* {}::{}(std::string_view fn) {{",
112112
getEmulatorNamespace(),
113113
getFunctionCreatorName()));
114114
emulatorSource_.indent();
115+
emulatorSource_.println("#if defined(__APPLE__) && defined(__MACH__)");
116+
emulatorSource_.println("// mach o symbols are prefixed with '_'");
117+
emulatorSource_.println("fn.remove_prefix(1);");
118+
emulatorSource_.println("#endif");
119+
115120
for (auto* instruction : ctx->instruction()) {
116121
emulatorSource_.println(std::format("if (fn == \"{}\") {{",
117122
instruction->INSTR_NAME()->getText()));
@@ -176,8 +181,8 @@ void isaCreator::generateEmulatorPreamble() {
176181
getRegisterType(), getRegisterCount(),
177182
getRegsVarName()));
178183

179-
emulatorHeader_.println(std::format("void* {}(const std::string& fn);",
180-
getFunctionCreatorName()));
184+
emulatorHeader_.println(
185+
std::format("void* {}(std::string_view fn);", getFunctionCreatorName()));
181186

182187
emulatorHeader_.println(std::format("void {}(moduleContext& module);",
183188
getModuleRegistratorName()));

asm/lift.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ extern "C" {
2828
#include "source-descriptor/sourceDescriptor.h"
2929

3030
namespace {
31-
constexpr size_t REG_FILE_SIZE = 32;
32-
std::array<int64_t, REG_FILE_SIZE> regs = {0};
33-
3431
constexpr std::string_view ASM_PATH = "asm-path";
3532
constexpr std::string_view MODE = "mode";
3633
constexpr std::string_view DUMP_SHORT = "d";
@@ -96,7 +93,7 @@ int main(int argc, char** argv) {
9693
};
9794

9895
llvm::ArrayType* regFileType =
99-
llvm::ArrayType::get(builder.getInt64Ty(), REG_FILE_SIZE);
96+
llvm::ArrayType::get(builder.getInt64Ty(), asmEmulator::REGS.size());
10097
if (emulating) {
10198
llvmModule->getOrInsertGlobal(REG_FILE_NAME, regFileType);
10299
regFileHolder = llvmModule->getNamedGlobal(REG_FILE_NAME);
@@ -107,7 +104,7 @@ int main(int argc, char** argv) {
107104
};
108105

109106
moduleContext ctx("asmGenerated", *std::visit(regFileGetter, regFileHolder),
110-
moduleCtx, builder, *llvmModule, REG_FILE_SIZE);
107+
moduleCtx, builder, *llvmModule, asmEmulator::REGS.size());
111108

112109
asmEmulator::registerFunctions(ctx);
113110

@@ -147,17 +144,21 @@ int main(int argc, char** argv) {
147144
LLVMInitializeNativeTarget();
148145
LLVMInitializeNativeAsmPrinter();
149146

150-
llvm::ExecutionEngine* ee =
151-
llvm::EngineBuilder(std::move(llvmModule)).create();
147+
std::string errorStr;
148+
149+
llvm::ExecutionEngine* ee = llvm::EngineBuilder(std::move(llvmModule))
150+
.setErrorStr(&errorStr)
151+
.create();
152152
if (!ee) {
153-
std::cerr << "Failed creating execution engine." << std::endl;
153+
std::cerr << "Failed creating execution engine: " << errorStr << std::endl;
154154
return 1;
155155
}
156156

157157
ee->InstallLazyFunctionCreator(asmEmulator::emulatorCaller);
158-
ee->finalizeObject();
158+
// ee->finalizeObject();
159159

160-
ee->addGlobalMapping(&ctx.getRegFile(), reinterpret_cast<void*>(regs.data()));
160+
ee->addGlobalMapping(&ctx.getRegFile(),
161+
reinterpret_cast<void*>(asmEmulator::REGS.data()));
161162

162163
simInit();
163164
llvm::GenericValue res =

0 commit comments

Comments
 (0)