rx2c.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. import random
  2. import sys
  3. import os
  4. PROGRAM_SIZE = 512
  5. INSTRUCTION_COUNT = 1024 * 1024
  6. INSTRUCTION_WEIGHTS = [
  7. ("ADD_64", 16),
  8. ("ADD_32", 8),
  9. ("SUB_64", 16),
  10. ("SUB_32", 8),
  11. ("MUL_64", 7),
  12. ("MULH_64", 7),
  13. ("MUL_32", 7),
  14. ("IMUL_32", 7),
  15. ("IMULH_64", 7),
  16. ("DIV_64", 1),
  17. ("IDIV_64", 1),
  18. ("AND_64", 4),
  19. ("AND_32", 3),
  20. ("OR_64", 4),
  21. ("OR_32", 3),
  22. ("XOR_64", 4),
  23. ("XOR_32", 3),
  24. ("SHL_64", 6),
  25. ("SHR_64", 6),
  26. ("SAR_64", 6),
  27. ("ROL_64", 9),
  28. ("ROR_64", 9),
  29. ("FADD", 22),
  30. ("FSUB", 22),
  31. ("FMUL", 22),
  32. ("FDIV", 8),
  33. ("FSQRT", 6),
  34. ("FROUND", 2),
  35. ("CALL", 17),
  36. ("RET", 15),
  37. ]
  38. def genBytes(count):
  39. return ', '.join(str(random.getrandbits(8)) for i in range(count))
  40. class OperandType:
  41. INT32 = 0
  42. UINT32 = 1
  43. INT64 = 2
  44. UINT64 = 3
  45. FLOAT = 4
  46. SHIFT = 5
  47. def declareType(type):
  48. converters = {
  49. 0: "int32_t",
  50. 1: "uint32_t",
  51. 2: "int64_t",
  52. 3: "uint64_t",
  53. 4: "double",
  54. 5: "int32_t"
  55. }
  56. return converters.get(type)
  57. def toSigned32(x):
  58. return x - ((x & 0x80000000) << 1)
  59. def toSigned64(x):
  60. return x - ((x & 0x8000000000000000) << 1)
  61. def immediateTo(symbol, type):
  62. converters = {
  63. 0: toSigned32(symbol.imm1),
  64. 1: symbol.imm1,
  65. 2: toSigned32(symbol.imm1),
  66. 3: symbol.imm1,
  67. 4: float(toSigned32(symbol.imm1) << 32),
  68. 5: symbol.imm0 & 63
  69. }
  70. return repr(converters.get(type))
  71. def registerTo(expr, type):
  72. converters = {
  73. 0: "(int64_t){0}",
  74. 1: "{0}",
  75. 2: "(int64_t){0}",
  76. 3: "{0}",
  77. 4: "{0}",
  78. 5: "({0} & 63)"
  79. }
  80. return converters.get(type).format(expr)
  81. def registerFrom(num, type):
  82. converters = {
  83. 0: "r{0}",
  84. 1: "r{0}",
  85. 2: "r{0}",
  86. 3: "r{0}",
  87. 4: "((convertible_t)f{0}).u64",
  88. 5: "r{0}"
  89. }
  90. return converters.get(type).format(num)
  91. def convertibleTo(expr, type):
  92. converters = {
  93. 0: "{0}.i32",
  94. 1: "{0}.u32",
  95. 2: "{0}.i64",
  96. 3: "{0}.u64",
  97. 4: "(double){0}.i64",
  98. 5: "({0}.u64 & 63)"
  99. }
  100. return converters.get(type).format(expr)
  101. def convertibleFrom(expr, type):
  102. converters = {
  103. 0: "{0}.i32",
  104. 1: "{0}.u32",
  105. 2: "{0}.i64",
  106. 3: "{0}.u64",
  107. 4: "{0}.f64",
  108. 5: "({0}.u64 & 63)"
  109. }
  110. return converters.get(type).format(expr)
  111. def getRegister(num, type):
  112. registers = {
  113. 0: "r{0}",
  114. 1: "r{0}",
  115. 2: "r{0}",
  116. 3: "r{0}",
  117. 4: "f{0}",
  118. 5: "r{0}"
  119. }
  120. return registers.get(type).format(num)
  121. def writeInitialValues(file):
  122. file.write("#ifdef RAM\n")
  123. file.write("\tmmu.buffer = (char*)_mm_malloc(DRAM_SIZE, 16);\n")
  124. file.write("\tif(!mmu.buffer) {\n")
  125. file.write('\t\tprintf("DRAM buffer allocation failed\\n");\n')
  126. file.write("\t\treturn 1;\n")
  127. file.write("\t}\n")
  128. file.write('\tprintf("Initializing DRAM buffer...\\n");\n')
  129. file.write("\taesInitialize((__m128i*)aesKey, (__m128i*)aesSeed, (__m128i*)mmu.buffer, DRAM_SIZE);\n")
  130. file.write("#endif\n")
  131. file.write("\tclock_t clockStart = clock(), clockEnd;\n")
  132. for i in range(8):
  133. file.write("\tr{0} = *(uint64_t*)(aesSeed + {1});\n".format(i, i * 8))
  134. for i in range(8):
  135. file.write("\tf{0} = *(int64_t*)(aesSeed + {1});\n".format(i, 64 + i * 8))
  136. file.write("\taesInitialize((__m128i*)aesKey, (__m128i*)aesSeed, (__m128i*)scratchpad, SCRATCHPAD_SIZE);\n")
  137. file.write("\tmmu.m0 = (aesKey[10] << 16) | (aesKey[11] << 24);\n")
  138. file.write("\tmmu.mx = 0;\n")
  139. file.write("\tsp = 0;\n")
  140. file.write("\tic = {0};\n".format(INSTRUCTION_COUNT))
  141. file.write("\tmxcsr = (_mm_getcsr() | _MM_FLUSH_ZERO_ON) & ~_MM_ROUND_MASK; //flush denormals to zero, round to nearest\n")
  142. file.write("\t_mm_setcsr(mxcsr);\n")
  143. def writeEpilog(file):
  144. file.write("\tend:\n")
  145. file.write("\t\tclockEnd = clock();\n")
  146. for i in range(8):
  147. file.write('\t\tprintf("r{0} = %-36" PRIu64 " f{0} = %g\\n", r{0}, f{0});\n'.format(i))
  148. file.write(("\t\tuint64_t spadsum = 0;\n"
  149. "\t\tfor(int i = 0; i < SCRATCHPAD_LENGTH; ++i) {\n"
  150. "\t\t spadsum += scratchpad[i].u64;\n"
  151. "\t\t}\n"
  152. '\t\tprintf("scratchpad sum = %" PRIu64 "\\n", spadsum);\n'
  153. '\t\tprintf("runtime: %f\\n", (clockEnd - clockStart) / (double)CLOCKS_PER_SEC);\n'
  154. "#ifdef RAM\n"
  155. "\t\t_mm_free((void*)mmu.buffer);\n"
  156. "#endif\n"))
  157. file.write("\t\treturn 0;")
  158. file.write("}")
  159. def writeCommon(file, i, symbol, type, name):
  160. file.write("\ti_{0}: {{ //{1}\n".format(i, name))
  161. file.write("\t\tif(0 == ic--) goto end;\n")
  162. file.write("\t\tr{0} ^= {1};\n".format(symbol.rega, symbol.addr0))
  163. file.write("\t\taddr_t addr = r{0};\n".format(symbol.rega))
  164. def readA(symbol, type):
  165. location = {
  166. 0: "readDram(&mmu, addr)",
  167. 1: "readDram(&mmu, addr)",
  168. 2: "readDram(&mmu, addr)",
  169. 3: "readDram(&mmu, addr)",
  170. 4: "SCRATCHPAD_256K(addr)",
  171. 5: "SCRATCHPAD_16K(addr)",
  172. 6: "SCRATCHPAD_16K(addr)",
  173. 7: "SCRATCHPAD_16K(addr)",
  174. }
  175. return convertibleTo(location.get(symbol.loca), type)
  176. def writeC(symbol, type):
  177. location = {
  178. 0: "SCRATCHPAD_256K(r{0} ^ {1})",
  179. 1: "SCRATCHPAD_16K(r{0} ^ {1})",
  180. 2: "SCRATCHPAD_16K(r{0} ^ {1})",
  181. 3: "SCRATCHPAD_16K(r{0} ^ {1})",
  182. 4: "",
  183. 5: "",
  184. 6: "",
  185. 7: ""
  186. }
  187. c = location.get(symbol.locc)
  188. if c == "":
  189. c = getRegister(symbol.regc, type)
  190. else:
  191. c = convertibleFrom(c.format(symbol.regc, symbol.addr1), type)
  192. return c
  193. def readB(symbol, type):
  194. if symbol.locb < 6:
  195. return registerTo(getRegister(symbol.regb, type), type)
  196. else:
  197. return immediateTo(symbol, type)
  198. class CodeSymbol:
  199. def __init__(self, qi):
  200. self.opcode = qi & 255
  201. self.loca = (qi >> 8) & 7
  202. self.rega = (qi >> 16) & 7
  203. self.locb = (qi >> 24) & 7
  204. self.regb = (qi >> 32) & 7
  205. self.locc = (qi >> 40) & 7
  206. self.regc = (qi >> 48) & 7
  207. self.imm0 = (qi >> 56) & 255
  208. self.addr0 = (qi >> 64) & 0xFFFFFFFF
  209. self.addr1 = self.imm1 = qi >> 96
  210. def writeOperation(file, i, symbol, type, name, op):
  211. writeCommon(file, i, symbol, type, name)
  212. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  213. file.write("\t\t{0} B = {1};\n".format(declareType(type), readB(symbol, type)))
  214. file.write("\t\t{0} = A {1} B; }}\n".format(writeC(symbol, type), op))
  215. def write_ADD_64(file, i, symbol):
  216. writeOperation(file, i, symbol, OperandType.UINT64, 'ADD_64', '+');
  217. def write_ADD_32(file, i, symbol):
  218. writeOperation(file, i, symbol, OperandType.UINT32, 'ADD_32', '+');
  219. def write_SUB_64(file, i, symbol):
  220. writeOperation(file, i, symbol, OperandType.UINT64, 'SUB_64', '-');
  221. def write_SUB_32(file, i, symbol):
  222. writeOperation(file, i, symbol, OperandType.UINT32, 'SUB_32', '-');
  223. def write_MUL_64(file, i, symbol):
  224. writeOperation(file, i, symbol, OperandType.UINT64, 'MUL_64', '*');
  225. def write_MULH_64(file, i, symbol):
  226. type = OperandType.UINT64
  227. writeCommon(file, i, symbol, type, 'MULH_64')
  228. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  229. file.write("\t\t{0} B = {1};\n".format(declareType(type), readB(symbol, type)))
  230. file.write("\t\t{0} = ((uint128_t)A * B) >> 64; }}\n".format(writeC(symbol, type)))
  231. def write_MUL_32(file, i, symbol):
  232. type = OperandType.UINT32
  233. writeCommon(file, i, symbol, type, 'MUL_32')
  234. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  235. file.write("\t\t{0} B = {1};\n".format(declareType(type), readB(symbol, type)))
  236. file.write("\t\t{0} = (uint64_t)A * B; }}\n".format(writeC(symbol, OperandType.UINT64)))
  237. def write_IMUL_32(file, i, symbol):
  238. type = OperandType.INT32
  239. writeCommon(file, i, symbol, type, 'IMUL_32')
  240. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  241. file.write("\t\t{0} B = {1};\n".format(declareType(type), readB(symbol, type)))
  242. file.write("\t\t{0} = (int64_t)A * B; }}\n".format(writeC(symbol, OperandType.INT64)))
  243. def write_IMULH_64(file, i, symbol):
  244. type = OperandType.INT64
  245. writeCommon(file, i, symbol, type, 'IMULH_64')
  246. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  247. file.write("\t\t{0} B = {1};\n".format(declareType(type), readB(symbol, type)))
  248. file.write("\t\t{0} = ((int128_t)A * B) >> 64; }}\n".format(writeC(symbol, type)))
  249. def write_DIV_64(file, i, symbol):
  250. type = OperandType.UINT64
  251. writeCommon(file, i, symbol, type, 'DIV_64')
  252. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  253. file.write("\t\t{0} B = {1};\n".format(declareType(OperandType.UINT32), readB(symbol, OperandType.UINT32)))
  254. file.write("\t\tif(B == 0) B = 1;\n".format(declareType(type), readB(symbol, type)))
  255. file.write("\t\t{0} = A / B; }}\n".format(writeC(symbol, type)))
  256. def write_IDIV_64(file, i, symbol):
  257. type = OperandType.INT64
  258. writeCommon(file, i, symbol, type, 'IDIV_64')
  259. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  260. file.write("\t\t{0} B = {1};\n".format(declareType(OperandType.INT32), readB(symbol, OperandType.INT32)))
  261. file.write("\t\tif(B == 0) B = 1;\n".format(declareType(type), readB(symbol, type)))
  262. file.write("\t\t{0} = A / B; }}\n".format(writeC(symbol, type)))
  263. def write_AND_64(file, i, symbol):
  264. writeOperation(file, i, symbol, OperandType.UINT64, 'AND_64', '&');
  265. def write_AND_32(file, i, symbol):
  266. writeOperation(file, i, symbol, OperandType.UINT32, 'AND_32', '&');
  267. def write_OR_64(file, i, symbol):
  268. writeOperation(file, i, symbol, OperandType.UINT64, 'OR_64', '|');
  269. def write_OR_32(file, i, symbol):
  270. writeOperation(file, i, symbol, OperandType.UINT32, 'OR_32', '|');
  271. def write_XOR_64(file, i, symbol):
  272. writeOperation(file, i, symbol, OperandType.UINT64, 'XOR_64', '^');
  273. def write_XOR_32(file, i, symbol):
  274. writeOperation(file, i, symbol, OperandType.UINT32, 'XOR_32', '^');
  275. def write_SHL_64(file, i, symbol):
  276. type = OperandType.UINT64
  277. writeCommon(file, i, symbol, type, 'SHL_64')
  278. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  279. file.write("\t\t{0} B = {1};\n".format(declareType(OperandType.SHIFT), readB(symbol, OperandType.SHIFT)))
  280. file.write("\t\t{0} = A << B; }}\n".format(writeC(symbol, type)))
  281. def write_SHR_64(file, i, symbol):
  282. type = OperandType.UINT64
  283. writeCommon(file, i, symbol, type, 'SHR_64')
  284. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  285. file.write("\t\t{0} B = {1};\n".format(declareType(OperandType.SHIFT), readB(symbol, OperandType.SHIFT)))
  286. file.write("\t\t{0} = A >> B; }}\n".format(writeC(symbol, type)))
  287. def write_SAR_64(file, i, symbol):
  288. type = OperandType.INT64
  289. writeCommon(file, i, symbol, type, 'SAR_64')
  290. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  291. file.write("\t\t{0} B = {1};\n".format(declareType(OperandType.SHIFT), readB(symbol, OperandType.SHIFT)))
  292. file.write("\t\t{0} = A >> B; }}\n".format(writeC(symbol, type)))
  293. def write_ROL_64(file, i, symbol):
  294. type = OperandType.UINT64
  295. writeCommon(file, i, symbol, type, 'ROL_64')
  296. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  297. file.write("\t\t{0} B = {1};\n".format(declareType(OperandType.SHIFT), readB(symbol, OperandType.SHIFT)))
  298. file.write("\t\t{0} = __rolq(A, B); }}\n".format(writeC(symbol, type)))
  299. def write_ROR_64(file, i, symbol):
  300. type = OperandType.UINT64
  301. writeCommon(file, i, symbol, type, 'ROR_64')
  302. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  303. file.write("\t\t{0} B = {1};\n".format(declareType(OperandType.SHIFT), readB(symbol, OperandType.SHIFT)))
  304. file.write("\t\t{0} = __rorq(A, B); }}\n".format(writeC(symbol, type)))
  305. def write_FADD(file, i, symbol):
  306. writeOperation(file, i, symbol, OperandType.FLOAT, 'FADD', '+');
  307. def write_FSUB(file, i, symbol):
  308. writeOperation(file, i, symbol, OperandType.FLOAT, 'FSUB', '-');
  309. def write_FMUL(file, i, symbol):
  310. writeOperation(file, i, symbol, OperandType.FLOAT, 'FMUL', '*');
  311. def write_FDIV(file, i, symbol):
  312. writeOperation(file, i, symbol, OperandType.FLOAT, 'FDIV', '/');
  313. def write_FSQRT(file, i, symbol):
  314. type = OperandType.FLOAT
  315. writeCommon(file, i, symbol, type, 'FSQRT')
  316. file.write("\t\t{0} A = fabs({1});\n".format(declareType(type), readA(symbol, type)))
  317. file.write("\t\t{0} = _mm_cvtsd_f64(_mm_sqrt_sd(_mm_setzero_pd(), _mm_load_pd(&A))); }}\n".format(writeC(symbol, type)))
  318. def write_FROUND(file, i, symbol):
  319. type = OperandType.FLOAT
  320. writeCommon(file, i, symbol, type, 'FROUND')
  321. file.write("\t\t{0} A = {1};\n".format(declareType(OperandType.INT64), readA(symbol, OperandType.INT64)))
  322. file.write("\t\t{0} = A;\n".format(writeC(symbol, type)))
  323. file.write("\t\t_mm_setcsr(mxcsr | ((uint32_t)(A << 13) & _MM_ROUND_MASK)); }\n")
  324. def write_CALL(file, i, symbol):
  325. type = OperandType.UINT64
  326. writeCommon(file, i, symbol, type, 'CALL')
  327. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  328. if symbol.locb < 6:
  329. file.write("\t\tif((uint32_t)r{0} <= {1}) {{\n".format(symbol.regb, symbol.imm1))
  330. file.write("\t\t\tPUSH_VALUE(A);\n");
  331. file.write("\t\t\tPUSH_ADDRESS(&&i_{0});\n".format((i + 1) & (PROGRAM_SIZE - 1)));
  332. file.write("\t\t\tgoto i_{0};\n".format((i + 1 + (symbol.imm0 & ((PROGRAM_SIZE >> 2) - 1))) & (PROGRAM_SIZE - 1)));
  333. if symbol.locb < 6:
  334. file.write("\t\t}}\n\t\t{0} = A;".format(writeC(symbol, type)))
  335. file.write("\t\t}\n")
  336. def write_RET(file, i, symbol):
  337. type = OperandType.UINT64
  338. writeCommon(file, i, symbol, type, 'RET')
  339. file.write("\t\t{0} A = {1};\n".format(declareType(type), readA(symbol, type)))
  340. file.write("\t\tif(!STACK_IS_EMPTY()")
  341. if symbol.locb < 6:
  342. file.write(" && (uint32_t)r{0} <= {1}".format(symbol.regb, symbol.imm1))
  343. file.write(") {\n")
  344. file.write("\t\t\tvoid* target = POP_ADDRESS();\n")
  345. file.write("\t\t\tuint64_t C = POP_VALUE();\n")
  346. file.write("\t\t\t{0} = A ^ C;\n".format(writeC(symbol, type)))
  347. file.write("\t\t\tgoto *target;\n")
  348. file.write("\t\t}}\n\t\t{0} = A; }}\n".format(writeC(symbol, type)))
  349. opcodeMap = { }
  350. def buildOpcodeMap():
  351. functions = globals()
  352. totalWeight = 0;
  353. for instruction, weight in INSTRUCTION_WEIGHTS:
  354. func = functions['write_' + instruction]
  355. for i in range(weight):
  356. opcodeMap[totalWeight] = func
  357. totalWeight = totalWeight + 1
  358. assert totalWeight == 256
  359. def writeCode(file, i, symbol):
  360. opcodeMap.get(symbol.opcode)(file, i, symbol)
  361. def writeMain(file):
  362. file.write(('__attribute__((optimize("Os"))) int main() {\n'
  363. " register uint64_t r0, r1, r2, r3, r4, r5, r6, r7;\n"
  364. " register double f0, f1, f2, f3, f4, f5, f6, f7;\n"
  365. " register uint64_t ic, sp;\n"
  366. " stack_t stack[STACK_LENGTH];\n"
  367. " convertible_t scratchpad[SCRATCHPAD_LENGTH] __attribute__ ((aligned (16)));\n"
  368. " mmu_t mmu;\n"
  369. " uint32_t mxcsr;\n"
  370. ))
  371. def writeProlog(file):
  372. file.write(("#include <stdint.h>\n"
  373. "#include <time.h>\n"
  374. "#include <stdio.h>\n"
  375. "#include <x86intrin.h>\n"
  376. "#include <emmintrin.h>\n"
  377. "#include <wmmintrin.h>\n"
  378. "#include <math.h>\n"
  379. "#include <inttypes.h>\n"
  380. "typedef uint32_t addr_t;\n"
  381. "typedef unsigned __int128 uint128_t;\n"
  382. "typedef __int128 int128_t;\n"
  383. "typedef unsigned char byte;\n"
  384. "typedef union {\n"
  385. " double f64;\n"
  386. " int64_t i64;\n"
  387. " uint64_t u64;\n"
  388. " int32_t i32;\n"
  389. " uint32_t u32;\n"
  390. "} convertible_t;\n"
  391. "typedef union {\n"
  392. " uint64_t value;\n"
  393. " void* address;\n"
  394. "} stack_t;\n"
  395. "typedef struct {\n"
  396. " addr_t m0;\n"
  397. " addr_t mx;\n"
  398. "#ifdef RAM\n"
  399. " const char* buffer;\n"
  400. "#endif\n"
  401. "} mmu_t;\n"
  402. "#define DRAM_SIZE (1ULL << 32)\n"
  403. "#define SCRATCHPAD_SIZE (256 * 1024)\n"
  404. "#define SCRATCHPAD_LENGTH (SCRATCHPAD_SIZE / sizeof(convertible_t))\n"
  405. "#define SCRATCHPAD_MASK14 (16 * 1024 / sizeof(convertible_t) - 1)\n"
  406. "#define SCRATCHPAD_MASK18 (SCRATCHPAD_LENGTH - 1)\n"
  407. "#define SCRATCHPAD_16K(x) scratchpad[(x) & SCRATCHPAD_MASK14]\n"
  408. "#define SCRATCHPAD_256K(x) scratchpad[(x) & SCRATCHPAD_MASK18]\n"
  409. "#define STACK_LENGTH (128 * 1024)\n"
  410. "#ifdef RAM\n"
  411. "#define DRAM_READ(mmu) (convertible_t)*(uint64_t*)((mmu)->buffer + (mmu)->m0)\n"
  412. "#define PREFETCH(mmu) _mm_prefetch(((mmu)->buffer + (mmu)->m0), _MM_HINT_T0)\n"
  413. "#else\n"
  414. "#define DRAM_READ(mmu) (convertible_t)(uint64_t)__rolq(6364136223846793005ULL*((mmu)->m0)+1442695040888963407ULL,32)\n"
  415. "#define PREFETCH(mmu)\n"
  416. "#endif\n"
  417. "#define PUSH_VALUE(x) stack[sp++].value = x\n"
  418. "#define PUSH_ADDRESS(x) stack[sp++].address = x\n"
  419. "#define STACK_IS_EMPTY() (sp == 0)\n"
  420. "#define POP_VALUE() stack[--sp].value\n"
  421. "#define POP_ADDRESS() stack[--sp].address\n"
  422. "static convertible_t readDram(mmu_t* mmu, addr_t addr) {\n"
  423. " convertible_t data;\n"
  424. " data = DRAM_READ(mmu);\n"
  425. " mmu->m0 += 8;\n"
  426. " mmu->mx ^= addr;\n"
  427. " if((mmu->mx & 0xFFFF) == 0) {\n"
  428. " mmu->m0 = mmu->mx;\n"
  429. "#if defined(PREF)\n"
  430. " PREFETCH(mmu);\n"
  431. "#endif\n"
  432. " }\n"
  433. " return data;\n"
  434. "}\n"
  435. "static inline __m128i sl_xor(__m128i tmp1) {\n"
  436. " __m128i tmp4;\n"
  437. " tmp4 = _mm_slli_si128(tmp1, 0x04);\n"
  438. " tmp1 = _mm_xor_si128(tmp1, tmp4);\n"
  439. " tmp4 = _mm_slli_si128(tmp4, 0x04);\n"
  440. " tmp1 = _mm_xor_si128(tmp1, tmp4);\n"
  441. " tmp4 = _mm_slli_si128(tmp4, 0x04);\n"
  442. " tmp1 = _mm_xor_si128(tmp1, tmp4);\n"
  443. " return tmp1;\n"
  444. "}\n"
  445. "#define AES_GENKEY_SUB(rcon) do { \\\n"
  446. " __m128i xout1 = _mm_aeskeygenassist_si128(xout2, rcon); \\\n"
  447. " xout1 = _mm_shuffle_epi32(xout1, 0xFF); \\\n"
  448. " xout0 = sl_xor(xout0); \\\n"
  449. " xout0 = _mm_xor_si128(xout0, xout1); \\\n"
  450. " xout1 = _mm_aeskeygenassist_si128(xout0, 0x00); \\\n"
  451. " xout1 = _mm_shuffle_epi32(xout1, 0xAA); \\\n"
  452. " xout2 = sl_xor(xout2); \\\n"
  453. " xout2 = _mm_xor_si128(xout2, xout1); } while(0)\n"
  454. "static inline void aes_genkey(const __m128i* memory, __m128i* k0, __m128i* k1, __m128i* k2, __m128i* k3, __m128i* k4, __m128i* k5, __m128i* k6, __m128i* k7, __m128i* k8, __m128i* k9) {\n"
  455. " __m128i xout0, xout2;\n"
  456. " xout0 = _mm_load_si128(memory);\n"
  457. " xout2 = _mm_load_si128(memory+1);\n"
  458. " *k0 = xout0;\n"
  459. " *k1 = xout2;\n"
  460. " AES_GENKEY_SUB(0x01);\n"
  461. " *k2 = xout0;\n"
  462. " *k3 = xout2;\n"
  463. " AES_GENKEY_SUB(0x02);\n"
  464. " *k4 = xout0;\n"
  465. " *k5 = xout2;\n"
  466. " AES_GENKEY_SUB(0x04);\n"
  467. " *k6 = xout0;\n"
  468. " *k7 = xout2;\n"
  469. " AES_GENKEY_SUB(0x08);\n"
  470. " *k8 = xout0;\n"
  471. " *k9 = xout2;\n"
  472. "}\n"
  473. "static inline void aes_round(__m128i key, __m128i* x0, __m128i* x1, __m128i* x2, __m128i* x3, __m128i* x4, __m128i* x5, __m128i* x6, __m128i* x7) {\n"
  474. " *x0 = _mm_aesenc_si128(*x0, key);\n"
  475. " *x1 = _mm_aesenc_si128(*x1, key);\n"
  476. " *x2 = _mm_aesenc_si128(*x2, key);\n"
  477. " *x3 = _mm_aesenc_si128(*x3, key);\n"
  478. " *x4 = _mm_aesenc_si128(*x4, key);\n"
  479. " *x5 = _mm_aesenc_si128(*x5, key);\n"
  480. " *x6 = _mm_aesenc_si128(*x6, key);\n"
  481. " *x7 = _mm_aesenc_si128(*x7, key);\n"
  482. "}\n"
  483. "static void aesInitialize(__m128i* key, __m128i* seed, __m128i* output, size_t count) {\n"
  484. " \n"
  485. " __m128i xin0, xin1, xin2, xin3, xin4, xin5, xin6, xin7;\n"
  486. " __m128i k0, k1, k2, k3, k4, k5, k6, k7, k8, k9;\n"
  487. " \n"
  488. " aes_genkey(key, &k0, &k1, &k2, &k3, &k4, &k5, &k6, &k7, &k8, &k9);\n"
  489. " \n"
  490. " xin0 = _mm_load_si128(seed + 0);\n"
  491. " xin1 = _mm_load_si128(seed + 1);\n"
  492. " xin2 = _mm_load_si128(seed + 2);\n"
  493. " xin3 = _mm_load_si128(seed + 3);\n"
  494. " xin4 = _mm_load_si128(seed + 4);\n"
  495. " xin5 = _mm_load_si128(seed + 5);\n"
  496. " xin6 = _mm_load_si128(seed + 6);\n"
  497. " xin7 = _mm_load_si128(seed + 7);\n"
  498. " \n"
  499. " for (size_t i = 0; i < count / sizeof(__m128i); i += 8)\n"
  500. " {\n"
  501. " aes_round(k0, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  502. " aes_round(k1, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  503. " aes_round(k2, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  504. " aes_round(k3, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  505. " aes_round(k4, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  506. " aes_round(k5, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  507. " aes_round(k6, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  508. " aes_round(k7, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  509. " aes_round(k8, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  510. " aes_round(k9, &xin0, &xin1, &xin2, &xin3, &xin4, &xin5, &xin6, &xin7);\n"
  511. " \n"
  512. " _mm_store_si128(output + i + 0, xin0);\n"
  513. " _mm_store_si128(output + i + 1, xin1);\n"
  514. " _mm_store_si128(output + i + 2, xin2);\n"
  515. " _mm_store_si128(output + i + 3, xin3);\n"
  516. " _mm_store_si128(output + i + 4, xin4);\n"
  517. " _mm_store_si128(output + i + 5, xin5);\n"
  518. " _mm_store_si128(output + i + 6, xin6);\n"
  519. " _mm_store_si128(output + i + 7, xin7);\n"
  520. " }\n"
  521. "}\n"))
  522. with sys.stdout as file:
  523. buildOpcodeMap()
  524. writeProlog(file)
  525. file.write("const byte aesKey[32] = {{ {0} }};\n".format(genBytes(32)))
  526. file.write("const byte aesSeed[128] = {{ {0} }};\n".format(genBytes(128)))
  527. writeMain(file)
  528. writeInitialValues(file)
  529. for i in range(PROGRAM_SIZE):
  530. writeCode(file, i, CodeSymbol(random.getrandbits(128)))
  531. if PROGRAM_SIZE > 0:
  532. file.write("\t\tgoto i_0;\n")
  533. writeEpilog(file)