|
| 1 | +import Node: Node; |
| 2 | +import std.stdio : writeln; |
| 3 | +import std.format : format; |
| 4 | + |
| 5 | +public immutable string ll_header = q"___ |
| 6 | +%struct.Dims = type { i32, i32 } |
| 7 | +%struct.Mat = type { %struct.Dims*, %struct.Dims*, double* } |
| 8 | +
|
| 9 | +declare void @print_string(i8* noundef %0) nounwind |
| 10 | +declare void @print_number(double noundef %0) nounwind |
| 11 | +declare void @print_integer(i32 noundef %0) nounwind |
| 12 | +declare void @print_newline() nounwind |
| 13 | +declare void @print_comma() nounwind |
| 14 | +declare void @print_semicolon() nounwind |
| 15 | +declare void @print_tab(i32 noundef %0) nounwind |
| 16 | +declare void @runtime_error(i32 noundef %0, i16 noundef %1) nounwind |
| 17 | +declare void @mat_print(double* noundef %0, i32 noundef %1, i1 noundef zeroext %2, i16 noundef %3) nounwind |
| 18 | +declare void @mat_print2(%struct.Mat* noundef %0, i1 noundef zeroext %1) nounwind |
| 19 | +declare void @mat_print_str(i8** noundef %0, i1 noundef zeroext %1) nounwind |
| 20 | +declare void @mat_read(double* noundef %0, i32 noundef %1, double* noundef %2, i32 noundef %3, i32* noundef %4, i16 noundef %5) nounwind |
| 21 | +declare void @mat_read2(%struct.Mat* noundef %0, double* noundef %1, i32 noundef %2, i32* noundef %3, i16 noundef %4) nounwind |
| 22 | +declare void @mat_read_str(i8** noundef %0, i32 noundef %1, i8** noundef %2, i32 noundef %3, i32* noundef %4, i16 noundef %5) nounwind |
| 23 | +declare void @mat_add(%struct.Mat* noundef %0, %struct.Mat* noundef %1, %struct.Mat* noundef %2, i16 noundef %3) nounwind |
| 24 | +declare void @mat_sub(%struct.Mat* noundef %0, %struct.Mat* noundef %1, %struct.Mat* noundef %2, i16 noundef %3) nounwind |
| 25 | +declare void @mat_mul(%struct.Mat* noundef %0, %struct.Mat* noundef %1, %struct.Mat* noundef %2, i16 noundef %3) nounwind |
| 26 | +declare void @mat_zer_con(%struct.Mat* noundef %0, i1 noundef zeroext %1, i16 noundef %2) nounwind |
| 27 | +declare void @mat_idn(%struct.Mat* noundef %0, i16 noundef %1) nounwind |
| 28 | +declare void @mat_trn(%struct.Mat* noundef %0, %struct.Mat* noundef %1, i16 noundef %2) nounwind |
| 29 | +declare double* @mat_minor(double* noundef %0, i32 noundef %1, i32 noundef %2, i32 noundef %3) nounwind |
| 30 | +declare double @mat_det(double* noundef %0, i32 noundef %1) nounwind |
| 31 | +declare void @mat_inv(%struct.Mat* noundef %0, %struct.Mat* noundef %1, double* noundef %2, i16 noundef %3) nounwind |
| 32 | +declare void @mat_scalar(%struct.Mat* noundef %0, %struct.Mat* noundef %1, double noundef %2, i16 noundef %3) nounwind |
| 33 | +declare void @mat_zer_con_idn_dim(%struct.Mat* noundef %0, i32 %1, i16 noundef %2) nounwind |
| 34 | +declare void @mat_input(double* noundef %0, i32 noundef %1, double* noundef %2, i16 noundef %3) nounwind |
| 35 | +declare void @change_from_string(i8** noundef %0, double* noundef %1, i32 noundef %2, i16 noundef %3) nounwind |
| 36 | +declare void @change_to_string(i8** noundef %0, double* noundef %1, i32 noundef %2, i16 noundef %3) nounwind |
| 37 | +declare void @read_number(double* noundef %0) nounwind |
| 38 | +declare void @read_string(i8* noundef %0, i32 noundef %1) nounwind |
| 39 | +declare double @random_lcg(i32 noundef %0) nounwind |
| 40 | +declare double @cot(double noundef %0) nounwind |
| 41 | +declare double @sgn(double noundef %0) nounwind |
| 42 | +declare i8* @str_dup(i8* noundef %0) nounwind |
| 43 | +
|
| 44 | +declare double @pow(double noundef %0, double noundef %1) nounwind |
| 45 | +declare double @fabs(double noundef %0) nounwind |
| 46 | +declare double @floor(double noundef %0) nounwind |
| 47 | +declare double @ceil(double noundef %0) nounwind |
| 48 | +declare double @sqrt(double noundef %0) nounwind |
| 49 | +declare double @sin(double noundef %0) nounwind |
| 50 | +declare double @cos(double noundef %0) nounwind |
| 51 | +declare double @tan(double noundef %0) nounwind |
| 52 | +declare double @asin(double noundef %0) nounwind |
| 53 | +declare double @acos(double noundef %0) nounwind |
| 54 | +declare double @atan(double noundef %0) nounwind |
| 55 | +declare double @log(double noundef %0) nounwind |
| 56 | +declare double @exp(double noundef %0) nounwind |
| 57 | +declare i8* @malloc(i64 noundef %0) nounwind |
| 58 | +declare void @free(i8* noundef %0) nounwind |
| 59 | +declare i32 @strcmp(i8* noundef %0, i8* noundef %1) nounwind |
| 60 | +declare i32 @time(i8* noundef %0) nounwind |
| 61 | +
|
| 62 | +define void @dummy_fct(i32 noundef %0, i16 noundef %1) nounwind { |
| 63 | + ret void |
| 64 | +} |
| 65 | +
|
| 66 | +___"; |
| 67 | + |
| 68 | +public immutable string ll_footer = q"___ |
| 69 | + exit: |
| 70 | + ret i32 0 |
| 71 | +} |
| 72 | +___"; |
| 73 | + |
| 74 | +public int gepDim(Node n, int ident, int idx) { |
| 75 | + auto sz = n.symtab.DimSize(ident) + 1; |
| 76 | + auto cmp = n.reg; |
| 77 | + writeln(format(" %%%d = icmp ult i32 %%%d, %d", cmp, idx, sz)); |
| 78 | + auto fct = n.reg; |
| 79 | + writeln(format(" %%%d = select i1 %%%d, void (i32, i16)* @dummy_fct, void (i32, i16)* @runtime_error", fct, cmp)); |
| 80 | + writeln(format(" call void %%%d(i32 4, i16 %d)", fct, n.symtab.line)); // error: index out of bounds |
| 81 | + auto gep = n.reg; |
| 82 | + writeln(format(" %%%d = getelementptr [ %u x double ], [ %u x double ]* %%_DATA1_%s, i32 0, i32 %%%d", |
| 83 | + gep, sz, sz, n.symtab.getId(ident), idx)); |
| 84 | + return gep; |
| 85 | +} |
| 86 | + |
| 87 | +public int gepDim2(Node n, int ident, int idx1, int idx2) { |
| 88 | + auto sz1 = n.symtab.Dim2Size(ident)[0] + 1, sz2 = n.symtab.Dim2Size(ident)[1] + 1; |
| 89 | + auto cmp1 = n.reg; |
| 90 | + writeln(format(" %%%d = icmp ult i32 %%%d, %d", cmp1, idx1, sz1)); |
| 91 | + auto cmp2 = n.reg; |
| 92 | + writeln(format(" %%%d = icmp ult i32 %%%d, %d", cmp2, idx2, sz2)); |
| 93 | + auto cmp = n.reg; |
| 94 | + writeln(format(" %%%d = and i1 %%%d, %%%d", cmp, cmp1, cmp2)); |
| 95 | + auto fct = n.reg; |
| 96 | + writeln(format(" %%%d = select i1 %%%d, void (i32, i16)* @dummy_fct, void (i32, i16)* @runtime_error", fct, cmp)); |
| 97 | + writeln(format(" call void %%%d(i32 4, i16 %d)", fct, n.symtab.line)); // error: index out of bounds |
| 98 | + auto offset_base = n.reg; |
| 99 | + writeln(format(" %%%d = mul i32 %%%d, %d ", offset_base, idx1, sz2)); |
| 100 | + auto offset = n.reg; |
| 101 | + writeln(format(" %%%d = add i32 %%%d, %%%d", offset, offset_base, idx2)); |
| 102 | + auto gep = n.reg; |
| 103 | + writeln(format(" %%%d = getelementptr [ %u x double ], [ %u x double ]* %%_DATA2_%s, i32 0, i32 %%%d", |
| 104 | + gep, sz1 * sz2, sz1 * sz2, n.symtab.getId(ident), offset)); |
| 105 | + return gep; |
| 106 | +} |
| 107 | + |
| 108 | +public int readData(Node n) { |
| 109 | + auto data_n = n.symtab.dataN; |
| 110 | + auto ptr = n.reg; |
| 111 | + writeln(format(" %%%d = load i32, i32* %%_DATA_NUM_P", ptr)); |
| 112 | + auto cmp = n.reg; |
| 113 | + writeln(format(" %%%d = icmp ult i32 %%%d, %d", cmp, ptr, data_n)); |
| 114 | + auto fct = n.reg; |
| 115 | + writeln(format(" %%%d = select i1 %%%d, void (i32, i16)* @dummy_fct, void (i32, i16)* @runtime_error", fct, cmp)); |
| 116 | + writeln(format(" call void %%%d(i32 1, i16 %d)", fct, n.symtab.line)); // error: out of data |
| 117 | + auto new_ptr = n.reg; |
| 118 | + writeln(format(" %%%d = add i32 %%%d, 1", new_ptr, ptr)); |
| 119 | + writeln(format(" store i32 %%%d, i32* %%_DATA_NUM_P", new_ptr)); |
| 120 | + auto gep = n.reg; |
| 121 | + writeln(format(" %%%d = getelementptr [ %d x double ], [ %d x double ]* @_DATA, i32 0, i32 %%%d", gep, data_n, data_n, ptr)); |
| 122 | + auto data = n.reg; |
| 123 | + writeln(format(" %%%d = load double, double* %%%d", data, gep)); |
| 124 | + return data; |
| 125 | +} |
| 126 | + |
| 127 | +public int gepStringDim(Node n, int ident, int idx) { |
| 128 | + auto sz = n.symtab.strDimSize(ident) + 1; |
| 129 | + auto cmp = n.reg; |
| 130 | + writeln(format(" %%%d = icmp ult i32 %%%d, %d", cmp, idx, sz)); |
| 131 | + auto fct = n.reg; |
| 132 | + writeln(format(" %%%d = select i1 %%%d, void (i32, i16)* @dummy_fct, void (i32, i16)* @runtime_error", fct, cmp)); |
| 133 | + writeln(format(" call void %%%d(i32 4, i16 %d)", fct, n.symtab.line)); // error: index out of bounds |
| 134 | + auto gep = n.reg; |
| 135 | + writeln(format(" %%%d = getelementptr [ %u x i8* ], [ %u x i8* ]* %%_DATAS_%s, i32 0, i32 %%%d", |
| 136 | + gep, sz, sz, n.symtab.getId(ident), idx)); |
| 137 | + return gep; |
| 138 | +} |
| 139 | + |
| 140 | +public int readStringData(Node n) { |
| 141 | + auto data_n = n.symtab.dataStrN; |
| 142 | + auto ptr = n.reg; |
| 143 | + writeln(format(" %%%d = load i32, i32* %%_DATA_STR_P", ptr)); |
| 144 | + auto cmp = n.reg; |
| 145 | + writeln(format(" %%%d = icmp ult i32 %%%d, %d", cmp, ptr, data_n)); |
| 146 | + auto fct = n.reg; |
| 147 | + writeln(format(" %%%d = select i1 %%%d, void (i32, i16)* @dummy_fct, void (i32, i16)* @runtime_error", fct, cmp)); |
| 148 | + writeln(format(" call void %%%d(i32 1, i16 %d)", fct, n.symtab.line)); // error: out of data |
| 149 | + auto new_ptr = n.reg; |
| 150 | + writeln(format(" %%%d = add i32 %%%d, 1", new_ptr, ptr)); |
| 151 | + writeln(format(" store i32 %%%d, i32* %%_DATA_STR_P", new_ptr)); |
| 152 | + auto gep = n.reg; |
| 153 | + writeln(format(" %%%d = getelementptr [%d x i8*], [%d x i8*]* @_DATA_STR, i32 0, i32 %%%d", gep, data_n, data_n, ptr)); |
| 154 | + auto data = n.reg; |
| 155 | + writeln(format(" %%%d = load i8*, i8** %%%d", data, gep)); |
| 156 | + auto r = n.reg; |
| 157 | + writeln(format(" %%%d = call i8* @str_dup(i8* %%%d)", r, data)); |
| 158 | + return r; |
| 159 | +} |
| 160 | + |
| 161 | +public int matrix(Node n, int ident) { |
| 162 | + auto dim = n.reg; |
| 163 | + writeln(format(" %%%d = alloca %%struct.Dims", dim)); |
| 164 | + auto gep1 = n.reg; |
| 165 | + writeln(format(" %%%d = getelementptr %%struct.Dims, %%struct.Dims* %%%d, i32 0, i32 0", gep1, dim)); |
| 166 | + writeln(format(" store i32 %d, i32* %%%d", n.symtab.Dim2Size(ident)[0], gep1)); |
| 167 | + auto gep2 = n.reg; |
| 168 | + writeln(format(" %%%d = getelementptr %%struct.Dims, %%struct.Dims* %%%d, i32 0, i32 1", gep2, dim)); |
| 169 | + writeln(format(" store i32 %d, i32* %%%d", n.symtab.Dim2Size(ident)[1], gep2)); |
| 170 | + auto mat = n.reg; |
| 171 | + writeln(format(" %%%d = alloca %%struct.Mat", mat)); |
| 172 | + auto gep3 = n.reg; |
| 173 | + writeln(format(" %%%d = getelementptr %%struct.Mat, %%struct.Mat* %%%d, i32 0, i32 0", gep3, mat)); |
| 174 | + writeln(format(" store %%struct.Dims* %%%d, %%struct.Dims** %%%d", dim, gep3)); |
| 175 | + auto gep4 = n.reg; |
| 176 | + writeln(format(" %%%d = getelementptr %%struct.Mat, %%struct.Mat* %%%d, i32 0, i32 1", gep4, mat)); |
| 177 | + writeln(format(" store %%struct.Dims* %%_MAT_%s, %%struct.Dims** %%%d", n.symtab.getId(ident), gep4)); |
| 178 | + auto gep5 = n.reg; |
| 179 | + writeln(format(" %%%d = getelementptr %%struct.Mat, %%struct.Mat* %%%d, i32 0, i32 2", gep5, mat)); |
| 180 | + auto ptr3 = n.reg; |
| 181 | + writeln(format(" %%%d = bitcast [ %d x double ]* %%_DATA2_%s to double*", |
| 182 | + ptr3, (n.symtab.Dim2Size(ident)[0] + 1) * (n.symtab.Dim2Size(ident)[1] + 1), n.symtab.getId(ident))); |
| 183 | + writeln(format(" store double* %%%d, double** %%%d", ptr3, gep5)); |
| 184 | + return mat; |
| 185 | +} |
0 commit comments