Assembly.h 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. #pragma once
  2. #include <vector>
  3. #include "Array.h"
  4. #include "Text.h"
  5. #include "Writer.h"
  6. namespace Framework
  7. {
  8. namespace Assembly
  9. {
  10. enum Operation
  11. {
  12. // no real operation, used for labels
  13. NOP,
  14. // Arithmetic Operations
  15. ADD, // Addition
  16. ADDPD, // Add Packed Double-Precision Floating-Point Values
  17. ADDPS, // Add Packed Single-Precision Floating-Point Values
  18. ADDSD, // Add Scalar Double-Precision Floating-Point Values
  19. ADDSS, // Add Scalar Single-Precision Floating-Point Values
  20. SUB, // Subtraction
  21. SUBPD, // Subtract Packed Double-Precision Floating-Point Values
  22. SUBPS, // Subtract Packed Single-Precision Floating-Point Values
  23. SUBSD, // Subtract Scalar Double-Precision Floating-Point Values
  24. SUBSS, // Subtract Scalar Single-Precision Floating-Point Values
  25. MUL, // Multiply unsigned
  26. IMUL, // Multiply signed
  27. MULPD, // Multiply Packed Double Precision Floating-Point Values
  28. MULPS, // Multiply Packed Single Precision Floating-Point Values
  29. MULSD, // Multiply Scalar Double Precision Floating-Point Value
  30. MULSS, // Multiply Scalar Single Precision Floating-Point Values
  31. DIV, // Division unsigned
  32. IDIV, // Division signed
  33. DIVPD, // Divide Packed Double-Precision Floating-Point Values
  34. DIVPS, // Divide Packed Single-Precision Floating-Point Values
  35. DIVSD, // Divide Scalar Double-Precision Floating-Point Values
  36. DIVSS, // Divide Scalar Single-Precision Floating-Point Values
  37. NEG, // Negation
  38. INC, // Increment
  39. // Logical Operations
  40. AND, // Bitwise AND
  41. OR, // Bitwise OR
  42. XOR, // Bitwise XOR
  43. NOT, // Bitwise NOT
  44. // Comparison Operations
  45. // Bitwise AND without storing the result. only
  46. // SpecialRegister.FLAGS (SF, ZF, PF) is affected
  47. TEST,
  48. // Compare
  49. // temporary subtracts op2 from op without storing the result. only
  50. // SpecialRegister.FLAGS is affected
  51. CMP,
  52. // Compare Packed Double Precision Floating-Point Values
  53. CMPPD,
  54. // Compare Packed Single Precision Floating-Point Values
  55. CMPPS,
  56. // Compare Scalar Double Precision Floating-Point Value
  57. CMPSD,
  58. // Compare Scalar Single Precision Floating-Point Value
  59. CMPSS,
  60. // Compare Scalar Ordered Double Precision Floating-Point Values
  61. // writes to SpecialRegister.FLAGS (ZF, PF, CF)
  62. // UNORDERED (one of the values was NAN) = 0b111
  63. // GREATER_THAN = 0b000
  64. // LESS_THAN = 0b001
  65. // EQUAL = 0b100
  66. COMISD,
  67. // Compare Scalar Ordered Single Precision Floating-Point Values
  68. // writes to SpecialRegister.FLAGS (ZF, PF, CF)
  69. // UNORDERED (one of the values was NAN) = 0b111
  70. // GREATER_THAN = 0b000
  71. // LESS_THAN = 0b001
  72. // EQUAL = 0b100
  73. COMISS,
  74. // Data Movement
  75. MOV, // Move data from source to destination
  76. // Move Unaligned Packed Double-Precision Floating-Point Value
  77. MOVUPD,
  78. // Move Unaligned Packed Single-Precision Floating-Point Value
  79. MOVUPS,
  80. MOVSD, // Move Scalar Double-Precision Floating-Point Value
  81. MOVSS, // Move Scalar Single-Precision Floating-Point Value
  82. LEA, // Load Effective Address
  83. // DATA Conversion
  84. // Convert Integer to Scalar Double Precision Floating-Point Value
  85. CVTSI2SD,
  86. // Convert Integer to Scalar Single Precision Floating-Point Value
  87. CVTSI2SS,
  88. // Convert With Truncation Scalar Double Precision Floating-Point
  89. // Value to Signed Integer
  90. CVTTSD2SI,
  91. // Convert With Truncation Scalar Single Precision Floating-Point
  92. // Value to Signed Integer
  93. CVTTSS2SI,
  94. // Convert Scalar Double Precision Floating-Point Value to Integer
  95. // with rounding according to the bits 13 an 14 of the MXCSR
  96. // register value:
  97. //
  98. // 0b00: (default) round to nearest integer value
  99. // 0b01: round to the next lower integer value
  100. // 0b10: round to the next heigher integer value
  101. // 0b11: truncate (like CVTTSS2SI)
  102. CVTSD2SI,
  103. // Convert Scalar Single Precision Floating-Point Value to Integer
  104. // with rounding according to the bits 13 an 14 of the MXCSR
  105. // register value:
  106. //
  107. // 0b00: (default) round to nearest integer value
  108. // 0b01: round to the next lower integer value
  109. // 0b10: round to the next heigher integer value
  110. // 0b11: truncate (like CVTTSS2SI)
  111. CVTSS2SI,
  112. // Control Flow
  113. // Unconditional Jump
  114. JMP,
  115. // Jump if Zero: SpecialRegister.FLAGS(ZF) = 1
  116. JZ,
  117. // Jump if equal: SpecialRegister.FLAGS(ZF) = 1
  118. JE = JZ,
  119. // Jump if Not Zero: SpecialRegister.FLAGS(ZF) = 0
  120. JNZ,
  121. // Jump if Not Equal: SpecialRegister.FLAGS(ZF) = 0
  122. JNE = JNZ,
  123. // Jump if Greater: SpecialRegister.FLAGS(SF) =
  124. // SpecialRegister.FLAGS(OF) and SpecialRegister.FLAGS(ZF) = 0
  125. JG,
  126. // Jump if Greater or Equal: SpecialRegister.FLAGS(SF) =
  127. // SpecialRegister.FLAGS(OF)
  128. JGE,
  129. // Jump if Less: SpecialRegister.FLAGS(SF) !=
  130. // SpecialRegister.FLAGS(OF)
  131. JL,
  132. // Jump if Less or Equal: SpecialRegister.FLAGS(SF) !=
  133. // SpecialRegister.FLAGS(OF) or SpecialRegister.FLAGS(ZF) = 1
  134. JLE,
  135. // Jump if above: SpecialRegister.FLAGS(CF) = 0 and
  136. // SpecialRegister.FLAGS(ZF) = 0
  137. JA,
  138. // Jump if carry: SpecialRegister.FLAGS(CF) = 1
  139. JC,
  140. // Jump if below: SpecialRegister.FLAGS(CF) = 1
  141. JB = JC,
  142. // Jump if not carry: SpecialRegister.FLAGS(CF) = 0
  143. JNC,
  144. // Jump if not below: SpecialRegister.FLAGS(CF) = 0
  145. JNB = JNC,
  146. // Jump if below or equal: SpecialRegister.FLAGS(CF) = 1 or
  147. // SpecialRegister.FLAGS(ZF) = 1
  148. JBE,
  149. JO, // Jump if overflow: SpecialRegister.FLAGS(OF) = 1
  150. JNO, // Jump if not overflow: SpecialRegister.FLAGS(OF) = 0
  151. JP, // Jump if parity even: SpecialRegister.FLAGS(PF) = 1
  152. JNP, // Jump if not parity odd: SpecialRegister.FLAGS(PF) = 0
  153. JS, // Jump if sign: SpecialRegister.FLAGS(SF) = 1
  154. JNS, // Jump if not sign: SpecialRegister.FLAGS(SF) = 0
  155. CALL, // Call subroutine
  156. RET, // Return from subroutine
  157. // Stack Operations
  158. PUSH, // Push onto stack
  159. POP // Pop from stack
  160. };
  161. enum CMP_IMM8
  162. {
  163. EQ_OQ = 0, // Equal (ordered, non-signaling)
  164. LT_OS = 1, // Less than (ordered, signaling)
  165. LE_OS = 2, // Less than or equal (ordered, signaling
  166. UNORD_Q = 3, // Unordered (non-signaling)
  167. NEQ_UQ = 4, // Not-equal (unordered, non-signaling)
  168. NLT_US = 5, // Not less than (unordered, signaling)
  169. NLE_US = 6, // Not-less-than-or-equal (unordered, signaling)
  170. ORD_Q = 7, // Ordered (non-signaling)
  171. EQ_US = 8, // Equal (unordered, signaling)
  172. NGE_US = 9, // Not-greater-than-or-equal (unordered, signaling)
  173. NGT_US = 10, // Not-greater-than (unordered, signaling)
  174. FALSE_OQ = 11, // False (ordered, non-signaling)
  175. NEQ_OQ = 12, // Not-equal (ordered, non-signaling)
  176. GE_OS = 13, // Greater-than-or-equal (ordered, signaling)
  177. GT_OS = 14, // Greater-than (ordered, signaling)
  178. TRUE_UQ = 15, // True (unordered, non-signaling)
  179. EQ_OS = 16, // Equal (ordered, signaling)
  180. LT_OQ = 17, // Less-than (ordered, non-signaling)
  181. LE_OQ = 18, // Less-than-or-equal (ordered, non
  182. UNORD_S = 19, // Unordered (signaling)
  183. NEQ_US = 20, // Not-equal (unordered, signaling)
  184. NLT_UQ = 21, // Not-less-than (unordered, non-sign
  185. NLE_UQ = 22, // Not-less-than-or-equal (unordered, non-signaling)
  186. ORD_S = 23, // Ordered (signaling)
  187. EQ_UQ = 24, // Equal (unordered, non-signaling)
  188. NGE_UQ = 25, // Not-greater-than-or-equal (
  189. NGT_UQ = 26, // Not-greater-than (unordered, non-signaling)
  190. FALSE_OS = 27, // False (ordered, signaling)
  191. NEQ_OS = 28, // Not-equal (ordered, signaling)
  192. GE_OQ = 29, // Greater-than-or-equal (ordered, non
  193. GT_OQ = 30, // Greater-than (ordered, non-signaling)
  194. TRUE_S = 31 // True (signaling)
  195. };
  196. enum FPDataType
  197. {
  198. SINGLE_FLOAT,
  199. SINGLE_DOUBLE,
  200. PACKED_FLOAT,
  201. PACKED_DOUBLE
  202. };
  203. enum FuncReturnType
  204. {
  205. NO_VALUE,
  206. INT_VALUE,
  207. FLOAT_VALUE
  208. };
  209. // General Purpose Registers
  210. enum GPRegister
  211. {
  212. // volatile register (can be altered by a function call)
  213. RAX = 0b0000,
  214. // non-volatile register (mus be restored on return)
  215. RBX = 0b0011,
  216. // volatile register (can be altered by a function call)
  217. RCX = 0b0001,
  218. // volatile register (can be altered by a function call)
  219. RDX = 0b0010,
  220. // Stack pointer points to the position of the last item that was
  221. // pushed to the stack. The stack grows downwards so lower means
  222. // more elements in the stack. needs to be aligned to 16 bytes
  223. // non-volatile register (mus be restored on return)
  224. RSP = 0b0100,
  225. // base pointer points to the base of the current stack frame
  226. // non-volatile register (mus be restored on return)
  227. RBP = 0b0101,
  228. // non-volatile register (mus be restored on return)
  229. RSI = 0b0110,
  230. // non-volatile register (mus be restored on return)
  231. RDI = 0b0111,
  232. // volatile register (can be altered by a function call)
  233. R8 = 0b1000,
  234. // volatile register (can be altered by a function call)
  235. R9 = 0b1001,
  236. // volatile register (can be altered by a function call)
  237. R10 = 0b1010,
  238. // volatile register (can be altered by a function call)
  239. R11 = 0b1011,
  240. // non-volatile register (mus be restored on return)
  241. R12 = 0b1100,
  242. // non-volatile register (mus be restored on return)
  243. R13 = 0b1101,
  244. // non-volatile register (mus be restored on return)
  245. R14 = 0b1110,
  246. // non-volatile register (mus be restored on return)
  247. R15 = 0b1111
  248. };
  249. enum SpecialRegister
  250. {
  251. // Instruction pointer points to the instruction that is executed
  252. // next
  253. RIP,
  254. /**
  255. only lower 16 bits are used:
  256. - CF: Carry Flag
  257. - PF: Parity Flag (1 if an even number of bits was set to 1 in
  258. the result of the last operation)
  259. - AF: Adjust Flag
  260. - ZF: Zero Flag (1 if the result of the last operation was zero)
  261. - SF: Sign Flag (1 if the result of the last operation was a
  262. positive number)
  263. - TF: Trap Flag
  264. - IF: Interrupt Flag
  265. - DF: Direction Flag
  266. - OF: Overflow Flag
  267. */
  268. FLAGS,
  269. /**
  270. * controls the behaviour of some operations.
  271. */
  272. MXCSR,
  273. };
  274. /**
  275. floating point registers
  276. */
  277. enum FPRegister
  278. {
  279. MM0, // volatile register (can be altered by a function call)
  280. MM1, // volatile register (can be altered by a function call)
  281. MM2, // volatile register (can be altered by a function call)
  282. MM3, // volatile register (can be altered by a function call)
  283. MM4, // volatile register (can be altered by a function call)
  284. MM5, // volatile register (can be altered by a function call)
  285. MM6, // non volatile register (mus be restored on return)
  286. MM7, // non volatile register (mus be restored on return)
  287. MM8, // non volatile register (mus be restored on return)
  288. MM9, // non volatile register (mus be restored on return)
  289. MM10, // non volatile register (mus be restored on return)
  290. MM11, // non volatile register (mus be restored on return)
  291. MM12, // non volatile register (mus be restored on return)
  292. MM13, // non volatile register (mus be restored on return)
  293. MM14, // non volatile register (mus be restored on return)
  294. MM15, // non volatile register (mus be restored on return)
  295. __FP_REGISTER_COUNT
  296. };
  297. /**
  298. describes the bits of the specified register that should be used.
  299. */
  300. enum GPRegisterPart
  301. {
  302. LOWER8,
  303. HIGHER8,
  304. LOWER16,
  305. LOWER32,
  306. FULL64
  307. };
  308. /**
  309. describes the bits of the specified register that should be used.
  310. */
  311. enum FPRegisterPart
  312. {
  313. X,
  314. Y,
  315. // Z
  316. };
  317. class AssemblyBlock;
  318. class GPRegisterArgument;
  319. class FPRegisterArgument;
  320. class MemoryAccessArgument;
  321. class ConstantArgument;
  322. class JumpTargetArgument;
  323. /**
  324. An argument for an Instruction.
  325. */
  326. class OperationArgument
  327. {
  328. private:
  329. public:
  330. /**
  331. * checks if a register is used in this argument.
  332. *
  333. * \param reg the register to check
  334. * \return true if the register is used
  335. */
  336. DLLEXPORT virtual bool usesRegister(GPRegister reg) const;
  337. /**
  338. * checks if a register is used in this argument.
  339. *
  340. * \param reg the register to check
  341. * \return true if the register is used
  342. */
  343. DLLEXPORT virtual bool usesRegister(FPRegister reg) const;
  344. /**
  345. * replaces a register with another register.
  346. *
  347. * \param oldReg the register to replace
  348. * \param newReg the register to use instead
  349. */
  350. DLLEXPORT virtual void replaceRegister(
  351. GPRegister oldReg, GPRegister newReg);
  352. /**
  353. * replaces a register with another register.
  354. *
  355. * \param oldReg the register to replace
  356. * \param newReg the register to use instead
  357. */
  358. DLLEXPORT virtual void replaceRegister(
  359. FPRegister oldReg, FPRegister newReg);
  360. /**
  361. * adds a prefix to all jump labels in this argument to avoid
  362. * conflicts if the assembly block that contains this argument is
  363. * inlined into another block.
  364. *
  365. * \param labelPrefix the label prefix to add
  366. */
  367. DLLEXPORT virtual void addJumpLabelPrefix(Text labelPrefix);
  368. /**
  369. * \return the GPRegisterArgument or 0 if it is not a
  370. * GPRegisterArgument
  371. */
  372. DLLEXPORT const GPRegisterArgument* asGPRegisterArgument() const;
  373. /**
  374. * \return the GPRegisterArgument or 0 if it is not a
  375. * GPRegisterArgument
  376. */
  377. DLLEXPORT const MemoryAccessArgument*
  378. asMemoryAccessArgument() const;
  379. /**
  380. * \return the ConstantArgument or 0 if it is not a
  381. * ConstantArgument
  382. */
  383. DLLEXPORT const ConstantArgument* asConstantArgument() const;
  384. /**
  385. * \return the FPRegisterArgument or 0 if it is not a
  386. * FPRegisterArgument
  387. */
  388. DLLEXPORT const FPRegisterArgument* asFPRegisterArgument() const;
  389. /**
  390. * \return the JumpTargetArgument or 0 if it is not a
  391. * JumpTargetArgument
  392. */
  393. DLLEXPORT const JumpTargetArgument* asJumpTargetArgument() const;
  394. };
  395. /**
  396. Represents the usage of a GPRegister as an Instruction Argument.
  397. */
  398. class GPRegisterArgument : public OperationArgument
  399. {
  400. private:
  401. GPRegister reg;
  402. GPRegisterPart part;
  403. public:
  404. DLLEXPORT GPRegisterArgument(
  405. GPRegister reg, GPRegisterPart part = GPRegisterPart::FULL64);
  406. DLLEXPORT bool usesRegister(GPRegister reg) const override;
  407. DLLEXPORT void replaceRegister(
  408. GPRegister oldReg, GPRegister newReg) override;
  409. DLLEXPORT GPRegister getRegister() const;
  410. DLLEXPORT GPRegisterPart getPart() const;
  411. };
  412. /**
  413. Represents the usage of a FPRegister as an Instruction Argument.
  414. */
  415. class FPRegisterArgument : public OperationArgument
  416. {
  417. private:
  418. FPRegister reg;
  419. FPRegisterPart part;
  420. public:
  421. DLLEXPORT FPRegisterArgument(FPRegister reg, FPRegisterPart = X);
  422. DLLEXPORT bool usesRegister(FPRegister reg) const override;
  423. DLLEXPORT void replaceRegister(
  424. FPRegister oldReg, FPRegister newReg) override;
  425. DLLEXPORT FPRegister getRegister() const;
  426. DLLEXPORT FPRegisterPart getPart() const;
  427. };
  428. enum class MemoryBlockSize
  429. {
  430. // 8 bist
  431. BYTE = 1,
  432. // 16 bits
  433. WORD = 2,
  434. // 32 bits
  435. DWORD = 4,
  436. // 64 bits
  437. QWORD = 8,
  438. // 128 bits
  439. M128 = 16,
  440. // 256 bits
  441. M256 = 32,
  442. // 512 bits
  443. // M512 = 64
  444. };
  445. /**
  446. Represents the usage of a Memory Read Request as an Instruction
  447. Argument.
  448. */
  449. class MemoryAccessArgument : public OperationArgument
  450. {
  451. private:
  452. MemoryBlockSize blockSize; // size of the block to access (1,2,4,8)
  453. bool useAddressReg;
  454. GPRegister address;
  455. int offset; // offset from the address in the register
  456. GPRegister offsetReg;
  457. bool useOffsetReg;
  458. public:
  459. DLLEXPORT MemoryAccessArgument(MemoryBlockSize blockSize,
  460. GPRegister address,
  461. bool useAddressReg = true,
  462. int offset = 0,
  463. bool useOffsetReg = false,
  464. GPRegister offsetReg = RAX);
  465. DLLEXPORT bool usesRegister(GPRegister reg) const override;
  466. DLLEXPORT void replaceRegister(
  467. GPRegister oldReg, GPRegister newReg) override;
  468. DLLEXPORT bool isUsingAddressRegister() const;
  469. DLLEXPORT GPRegister getAddressRegister() const;
  470. DLLEXPORT int getOffset() const;
  471. DLLEXPORT bool isUsingOffsetRegister() const;
  472. DLLEXPORT GPRegister getOffsetRegister() const;
  473. DLLEXPORT MemoryBlockSize getBlockSize() const;
  474. };
  475. /**
  476. Represents the usage of a const value as an Instruction Argument.
  477. */
  478. class ConstantArgument : public OperationArgument
  479. {
  480. private:
  481. __int64 value;
  482. MemoryBlockSize size; // size in byte
  483. public:
  484. DLLEXPORT ConstantArgument(
  485. __int64 value, MemoryBlockSize size = MemoryBlockSize::QWORD);
  486. DLLEXPORT ConstantArgument(
  487. int value, MemoryBlockSize size = MemoryBlockSize::DWORD);
  488. DLLEXPORT ConstantArgument(
  489. short value, MemoryBlockSize size = MemoryBlockSize::WORD);
  490. DLLEXPORT ConstantArgument(
  491. char value, MemoryBlockSize size = MemoryBlockSize::BYTE);
  492. DLLEXPORT __int64 getValue() const;
  493. DLLEXPORT MemoryBlockSize getSize() const;
  494. };
  495. /**
  496. Represents the usage of a jump label as an Instruction Argument.
  497. */
  498. class JumpTargetArgument : public OperationArgument
  499. {
  500. private:
  501. Text name;
  502. public:
  503. DLLEXPORT JumpTargetArgument(Text name);
  504. DLLEXPORT void addJumpLabelPrefix(Text labelPrefix) override;
  505. const Text& getLabel() const;
  506. };
  507. /**
  508. Represents a single assembly instruction with its arguments.
  509. */
  510. class Instruction : public Framework::ReferenceCounter
  511. {
  512. private:
  513. Operation op;
  514. std::vector<OperationArgument*> args;
  515. std::vector<GPRegister> implicitReadGPs;
  516. std::vector<FPRegister> implicitReadFPs;
  517. std::vector<GPRegister> implicitWriteGPs;
  518. std::vector<FPRegister> implicitWriteFPs;
  519. public:
  520. DLLEXPORT Instruction(
  521. Operation op, std::initializer_list<OperationArgument*> params);
  522. DLLEXPORT Instruction(Operation op,
  523. std::initializer_list<OperationArgument*> params,
  524. std::initializer_list<GPRegister> implicitReadGPs,
  525. std::initializer_list<FPRegister> implicitReadFPs,
  526. std::initializer_list<GPRegister> implicitWriteGPs,
  527. std::initializer_list<FPRegister> implicitWriteFPs);
  528. DLLEXPORT ~Instruction();
  529. /**
  530. * checks if this instruction reads from a specified register.
  531. *
  532. * \param reg the register to check
  533. * \return true if the instruction reads from the register
  534. */
  535. DLLEXPORT bool writesToRegister(
  536. GPRegister reg, const AssemblyBlock* block) const;
  537. /**
  538. * checks if this instruction reads from a specified register.
  539. *
  540. * \param reg the register to check
  541. * \return true if the instruction reads from the register
  542. */
  543. DLLEXPORT bool writesToRegister(
  544. FPRegister reg, const AssemblyBlock* block) const;
  545. /**
  546. * checks if this instruction reads from a specified register.
  547. *
  548. * \param reg the register to check
  549. * \return true if the instruction reads from the register
  550. */
  551. DLLEXPORT bool readsFromRegister(
  552. GPRegister reg, const AssemblyBlock* block) const;
  553. /**
  554. * checks if this instruction reads from a specified register.
  555. *
  556. * \param reg the register to check
  557. * \return true if the instruction reads from the register
  558. */
  559. DLLEXPORT bool readsFromRegister(
  560. FPRegister reg, const AssemblyBlock* block) const;
  561. /**
  562. * checks if a register can be replaced by another register.
  563. *
  564. * \param oldReg the register to replace
  565. * \param newReg the register to use instead
  566. * \return true if the replacement is possible
  567. */
  568. DLLEXPORT bool isReplacementPossible(GPRegister oldReg,
  569. GPRegister newReg,
  570. const AssemblyBlock* block) const;
  571. /**
  572. * checks if a register can be replaced by another register.
  573. *
  574. * \param oldReg the register to replace
  575. * \param newReg the register to use instead
  576. * \return true if the replacement is possible
  577. */
  578. DLLEXPORT bool isReplacementPossible(FPRegister oldReg,
  579. FPRegister newReg,
  580. const AssemblyBlock* block) const;
  581. /**
  582. * replaces a register with another register.
  583. *
  584. * \param oldReg the register to replace
  585. * \param newReg the register to use instead
  586. */
  587. DLLEXPORT void replaceRegister(
  588. GPRegister oldReg, GPRegister newReg);
  589. /**
  590. * replaces a register with another register.
  591. *
  592. * \param oldReg the register to replace
  593. * \param newReg the register to use instead
  594. */
  595. DLLEXPORT void replaceRegister(
  596. FPRegister oldReg, FPRegister newReg);
  597. /**
  598. * adds a prefix to all jump labels in this instruction to avoid
  599. * conflicts if the assembly block that contains this instruction is
  600. * inlined into another block.
  601. *
  602. * \param labelPrefix the label prefix to add
  603. */
  604. DLLEXPORT void addJumpLabelPrefix(Text labelPrefix);
  605. /**
  606. * compiles this Instruction to macine code.
  607. *
  608. * \param machineCodeWriter the machine code will be written to this
  609. * writer
  610. * \param block the block that contains this instruction. needed to
  611. * resolve jump labels
  612. */
  613. DLLEXPORT void compile(StreamWriter* machineCodeWriter,
  614. const AssemblyBlock* block) const;
  615. /**
  616. * test if this instruction is valid.
  617. *
  618. * \param block the code block that contains this instruction
  619. * \return true if the instruction can be compiled
  620. */
  621. DLLEXPORT bool isValid(const AssemblyBlock* block) const;
  622. /**
  623. * calculates the bytes needed for this instruction in machine code.
  624. *
  625. * \return the bytes needed
  626. */
  627. DLLEXPORT int compiledSize(const AssemblyBlock* block) const;
  628. /**
  629. * \return the op code of this instruction
  630. */
  631. DLLEXPORT Operation getOperation() const;
  632. /**
  633. * \return true if the given label is defined by this operation
  634. */
  635. DLLEXPORT bool definesLabel(Text label) const;
  636. /**
  637. * \return the arguments of this instruction
  638. */
  639. DLLEXPORT const std::vector<OperationArgument*>&
  640. getArguments() const;
  641. };
  642. /**
  643. Represents a block of assembly instructions that can be compiled to
  644. machine code or inlined into another AssemblyBlock with addBlock(...).
  645. */
  646. class AssemblyBlock
  647. {
  648. private:
  649. RCArray<Instruction> instructions;
  650. int inlineIndex;
  651. void* compiledCode;
  652. public:
  653. DLLEXPORT AssemblyBlock();
  654. DLLEXPORT ~AssemblyBlock();
  655. /**
  656. * adds an instruction.
  657. *
  658. * \param instr the instruction to add.
  659. */
  660. DLLEXPORT void addInstruction(Instruction* instr);
  661. /**
  662. * defines a new jump target label.
  663. *
  664. * \param name the name of the label.
  665. */
  666. DLLEXPORT void defineJumpTarget(Text name);
  667. /**
  668. * adds a jump instruction that jumps to the next Instaruction after
  669. * the definition of the given label.
  670. *
  671. * \param jumpOp the op code of the jump iperation.
  672. * \param targetName the label to jump to.
  673. */
  674. DLLEXPORT void addJump(Operation jumpOp, Text targetName);
  675. /**
  676. * adds the value of the source register to the value of the target
  677. * register and stores the result in the target register. single
  678. * operation.
  679. *
  680. * \param target the target register
  681. * \param source the source register
  682. * \param part the register part
  683. */
  684. DLLEXPORT void addAddition(GPRegister target,
  685. GPRegister source,
  686. GPRegisterPart part = FULL64);
  687. /**
  688. * adds the value of the source register to the value of the target
  689. * register and stores the result in the target register. single
  690. * operation.
  691. *
  692. * \param target the target register
  693. * \param source the source register
  694. * \param type the data type stored in the registers
  695. * \param part the register part
  696. */
  697. DLLEXPORT void addAddition(FPRegister target,
  698. FPRegister source,
  699. FPDataType type,
  700. FPRegisterPart part = Y);
  701. /**
  702. * subtracts the value of the target register from the value of the
  703. * source register and stores the result in the target register.
  704. * single operation.
  705. *
  706. * \param target the target register
  707. * \param source the source register
  708. * \param part the register part
  709. */
  710. DLLEXPORT void addSubtraction(GPRegister target,
  711. GPRegister source,
  712. GPRegisterPart part = FULL64);
  713. /**
  714. * subtracts the value of the target register from the value of the
  715. * source register and stores the result in the target register.
  716. * single operation.
  717. *
  718. * \param target the target register
  719. * \param source the source register
  720. * \param type the data type stored in the registers
  721. * \param part the register part
  722. */
  723. DLLEXPORT void addSubtraction(FPRegister target,
  724. FPRegister source,
  725. FPDataType type,
  726. FPRegisterPart part = Y);
  727. /**
  728. * multiplies the value of the source register to the value of the
  729. * target register and stores the result in the target register.
  730. * single operation.
  731. *
  732. * \param target the target register
  733. * \param source the source register
  734. * \param part the register part
  735. */
  736. DLLEXPORT void addMultiplication(GPRegister target,
  737. GPRegister source,
  738. GPRegisterPart part = FULL64);
  739. /**
  740. * multiplies the value of the source register to the value of the
  741. * target register and stores the result in the target register.
  742. * single operation.
  743. *
  744. * \param target the target register
  745. * \param source the source register
  746. * \param type the data type stored in the registers
  747. * \param part the register part
  748. */
  749. DLLEXPORT void addMultiplication(FPRegister target,
  750. FPRegister source,
  751. FPDataType type,
  752. FPRegisterPart part = Y);
  753. /**
  754. * devides the value of the target register by the value of the
  755. * source register and stores the result in the target register.
  756. * single operation.
  757. *
  758. * \param target the target register
  759. * \param source the source register
  760. * \param part the register part
  761. */
  762. DLLEXPORT void addDivision(GPRegister target,
  763. GPRegister source,
  764. GPRegisterPart part = FULL64);
  765. /**
  766. * devides the value of the target register by the value of the
  767. * source register and stores the result in the target register.
  768. * single operation.
  769. *
  770. * \param target the target register
  771. * \param source the source register
  772. * \param type the data type stored in the registers
  773. * \param part the register part
  774. */
  775. DLLEXPORT void addDivision(FPRegister target,
  776. FPRegister source,
  777. FPDataType type,
  778. FPRegisterPart part = Y);
  779. /**
  780. * performes bitwise AND without storing the result. only
  781. * SpecialRegister.FLAGS (SF, ZF, PF) is affected. Single operation.
  782. *
  783. * \param r1 the first value
  784. * \param r2 the second value
  785. * \param part the part of the values to test
  786. */
  787. DLLEXPORT void addTest(
  788. GPRegister r1, GPRegister r2, GPRegisterPart part = FULL64);
  789. /**
  790. * subtracts the value of r2 from r1 and sets FLAGS (CF, OF, SF, ZF,
  791. * AF, PF) according to the result. The result is discarded.
  792. *
  793. * \param r1 the first value
  794. * \param r2 the second value
  795. * \param part the part of the values to compare
  796. */
  797. DLLEXPORT void addCompare(
  798. GPRegister r1, GPRegister r2, GPRegisterPart part = FULL64);
  799. /**
  800. * subtracts the value from r1 and sets FLAGS (CF, OF, SF, ZF,
  801. * AF, PF) according to the result. The result is discarded.
  802. *
  803. * \param r1 the first value
  804. * \param value the value to subtract
  805. */
  806. DLLEXPORT void addCompare(GPRegister r1, char value);
  807. /**
  808. * subtracts the value from r1 and sets FLAGS (CF, OF, SF, ZF,
  809. * AF, PF) according to the result. The result is discarded.
  810. *
  811. * \param r1 the first value
  812. * \param value the value to subtract
  813. */
  814. DLLEXPORT void addCompare(GPRegister r1, short value);
  815. /**
  816. * subtracts the value from r1 and sets FLAGS (CF, OF, SF, ZF,
  817. * AF, PF) according to the result. The result is discarded.
  818. *
  819. * \param r1 the first value
  820. * \param value the value to subtract
  821. */
  822. DLLEXPORT void addCompare(GPRegister r1, int value);
  823. /**
  824. * subtracts the value of r2 from r1 and sets FLAGS (ZF, PF, CF).
  825. * the result is discarded
  826. *
  827. * UNORDERED: ZF,PF,CF := 111;
  828. * GREATER_THAN: ZF,PF,CF := 000;
  829. * LESS_THAN: ZF,PF,CF := 001;
  830. * EUAL: ZF,PF,CF := 100;
  831. *
  832. * \param r1 the first value
  833. * \param r2 the second value
  834. * \param type the datatype to compare. only SINGLE types are valid
  835. */
  836. DLLEXPORT void addCompare(
  837. FPRegister r1, FPRegister r2, FPDataType type);
  838. /**
  839. * computes bitwise AND for the values in source and target
  840. * register and stores the result in the target register. single
  841. * operation
  842. *
  843. * \param target the target register
  844. * \param source the source register
  845. * \param part the register part
  846. */
  847. DLLEXPORT void addAnd(GPRegister target,
  848. GPRegister source,
  849. GPRegisterPart part = FULL64);
  850. /**
  851. * computes bitwise AND for the values in source and target register
  852. * and stores the result in the target register. single operation
  853. *
  854. * \param target the target register
  855. * \param source the source register
  856. * \param part the register part
  857. */
  858. DLLEXPORT void addOr(GPRegister target,
  859. GPRegister source,
  860. GPRegisterPart part = FULL64);
  861. /**
  862. * writes the specified valueAddress pointer into a register.
  863. *
  864. * \param valueAddress the pointer to the value witch address should
  865. * be stored
  866. * \param target the register where the address should be stored
  867. */
  868. DLLEXPORT void addLoadValue(char* valueAddress, GPRegister target);
  869. /**
  870. * writes the specified valueAddress pointer into a register.
  871. *
  872. * \param valueAddress the pointer to the value witch address should
  873. * be stored
  874. * \param target the register where the address should be stored
  875. */
  876. DLLEXPORT void addLoadValue(short* valueAddress, GPRegister target);
  877. /**
  878. * writes the specified valueAddress pointer into a register.
  879. *
  880. * \param valueAddress the pointer to the value witch address should
  881. * be stored
  882. * \param target the register where the address should be stored
  883. */
  884. DLLEXPORT void addLoadValue(int* valueAddress, GPRegister target);
  885. /**
  886. * writes the specified valueAddress pointer into a register.
  887. *
  888. * \param valueAddress the pointer to the value witch address should
  889. * be stored
  890. * \param target the register where the address should be stored
  891. */
  892. DLLEXPORT void addLoadValue(
  893. __int64* valueAddress, GPRegister target);
  894. /**
  895. * loads the value at the address stored in addressRegister into the
  896. * target register. Single operation.
  897. *
  898. * \param addressRegister a register that contains a memory address
  899. * \param target the target register to store the value into
  900. * \param targetPart the part of the target register that will be
  901. * written to
  902. */
  903. DLLEXPORT void addLoadValue(GPRegister addressRegister,
  904. GPRegister target,
  905. GPRegisterPart targetPart,
  906. int offset = 0);
  907. /**
  908. * writes the specified valueAddress pointer into a register.
  909. *
  910. * \param valueAddress the pointer to the value witch address should
  911. * be stored
  912. * \param target the register where the address should be stored
  913. */
  914. DLLEXPORT void addLoadValue(
  915. float* valueAddress, FPRegister target, GPRegister temp = RAX);
  916. /**
  917. * writes the specified valueAddress pointer into a register.
  918. *
  919. * \param valueAddress the pointer to the value witch address should
  920. * be stored
  921. * \param target the register where the address should be stored
  922. */
  923. DLLEXPORT void addLoadValue(
  924. double* valueAddress, FPRegister target, GPRegister temp = RAX);
  925. /**
  926. * calls a function at a specified memory address.
  927. *
  928. * \param functionAddress pointet to the address of the function to
  929. * call
  930. */
  931. template<typename T>
  932. DLLEXPORT void addLoadAddress(T* addr, GPRegister temp = RAX)
  933. {
  934. instructions.add(new Instruction(MOV,
  935. {new GPRegisterArgument(temp),
  936. new ConstantArgument(
  937. reinterpret_cast<__int64>(addr))}));
  938. }
  939. /**
  940. * moves the given value to LOWER8 bits of target register.
  941. *
  942. * \param target the register to move the value to
  943. * \param value the value to move
  944. */
  945. DLLEXPORT void addMoveValue(GPRegister target, char value);
  946. /**
  947. * moves the given value to LOWER16 bits of target register.
  948. *
  949. * \param target the register to move the value to
  950. * \param value the value to move
  951. */
  952. DLLEXPORT void addMoveValue(GPRegister target, short value);
  953. /**
  954. * moves the given value to LOWER32 bits of target register.
  955. *
  956. * \param target the register to move the value to
  957. * \param value the value to move
  958. */
  959. DLLEXPORT void addMoveValue(GPRegister target, int value);
  960. /**
  961. * moves the given value to LOWER64 bits of target register.
  962. *
  963. * \param target the register to move the value to
  964. * \param value the value to move
  965. */
  966. DLLEXPORT void addMoveValue(GPRegister target, __int64 value);
  967. /**
  968. * moves the given value to LOWER32 bits of target register.
  969. * The value needs to be temporarily pushed to the stack, so
  970. * addLoadValue should be used if performance is critical.
  971. *
  972. * \param target the register to move the value to
  973. * \param value the value to move
  974. * \param temp temporary register that can be used us store the
  975. * value
  976. */
  977. DLLEXPORT void addMoveValue(
  978. FPRegister target, float value, GPRegister temp = RAX);
  979. /**
  980. * moves the given value to LOWER64 bits of target register.
  981. * The value needs to be temporarily pushed to the stack, so
  982. * addLoadValue should be used if performance is critical.
  983. *
  984. * \param target the register to move the value to
  985. * \param value the value to move
  986. * \param temp temporary register that can be used us store the
  987. * value
  988. */
  989. DLLEXPORT void addMoveValue(
  990. FPRegister target, double value, GPRegister temp = RAX);
  991. /**
  992. * moves the value from source register to target register.
  993. *
  994. * \param target the register to move the value to
  995. * \param source the register to move the value from
  996. * \param part the part of the register to move
  997. */
  998. DLLEXPORT void addMoveValue(GPRegister target,
  999. GPRegister source,
  1000. GPRegisterPart part = FULL64);
  1001. /**
  1002. * moves the value from source register to target register.
  1003. *
  1004. * \param target the register to move the value to
  1005. * \param source the register to move the value from
  1006. * \param part the part of the register to move
  1007. */
  1008. DLLEXPORT void addMoveValue(FPRegister target,
  1009. FPRegister source,
  1010. FPDataType type,
  1011. FPRegisterPart part = Y);
  1012. /**
  1013. * converts a floating point value from source register into a
  1014. * signed integer value and writes it into the target register.
  1015. * Single operation.
  1016. *
  1017. * \param target the target register
  1018. * \param source the source register
  1019. * \param type the datatype of the source register. Only SINGLE
  1020. * types are valid.
  1021. * \param targetPart the register part to write the result into.
  1022. * Only LOWER32 or FULL64 are valid.
  1023. * \param round if true the value is rounded acourding to bits 13
  1024. * and 14 of the MXCSR register value
  1025. */
  1026. DLLEXPORT void addConversion(GPRegister target,
  1027. FPRegister source,
  1028. FPDataType type,
  1029. GPRegisterPart targetPart,
  1030. bool round = 0);
  1031. /**
  1032. * converts a signed integer from source register into a floating
  1033. * point value and writes it into the target register. Single
  1034. * operation
  1035. *
  1036. * \param target the target register
  1037. * \param source the source register
  1038. * \param targetType the data type after the conversion. Only SINGLE
  1039. * types are valid.
  1040. * \param sourcePart the register part that contains the signed
  1041. * integer. Only LOWER32 and FULL64 are valid.
  1042. */
  1043. DLLEXPORT void addConversion(FPRegister target,
  1044. GPRegister source,
  1045. FPDataType targetType,
  1046. GPRegisterPart sourcePart);
  1047. /**
  1048. * calls a function at a specified memory address.
  1049. *
  1050. * \param functionAddress pointet to the address of the function to
  1051. * call
  1052. * \param gpParams must be the the every n-th entry of the register
  1053. * list (RCX, RDX, R8, R9) for each n-th function parameter that is
  1054. * of type integer
  1055. * \param fpParams must be the the every n-th entry of the register
  1056. * list (MM0, MM1, MM2, MM3) for each n-th function parameter that
  1057. * is of type float/double
  1058. * \param temp temporary register to store the address of the
  1059. * function
  1060. * \param bpTemp temporary register to store the stack pointer
  1061. * before ligning the stack pointer to a 16 byte boundry. The stack
  1062. * pointer will be automatically restored after the function call
  1063. * using the value of this register. A non-volatile register must be
  1064. * used.
  1065. */
  1066. DLLEXPORT void addCall(void* functionAddress,
  1067. FuncReturnType returnType = NO_VALUE,
  1068. std::initializer_list<GPRegister> gpParams = {},
  1069. std::initializer_list<FPRegister> fpParams = {},
  1070. GPRegister temp = RAX,
  1071. GPRegister bpTemp = RBP);
  1072. /**
  1073. * calls a function at a specified memory address.
  1074. *
  1075. * \param functionAddress pointet to the address of the function to
  1076. * call
  1077. * \param gpParams must be the the every n-th entry of the register
  1078. * list (RCX, RDX, R8, R9) for each n-th function parameter that is
  1079. * of type integer
  1080. * \param fpParams must be the the every n-th entry of the register
  1081. * list (MM0, MM1, MM2, MM3) for each n-th function parameter that
  1082. * is of type float/double
  1083. * \param temp temporary register to store the address of the
  1084. * function
  1085. * \param bpTemp temporary register to store the stack pointer
  1086. * before ligning the stack pointer to a 16 byte boundry. The stack
  1087. * pointer will be automatically restored after the function call
  1088. * using the value of this register. A non-volatile register must be
  1089. * used.
  1090. */
  1091. template<typename T>
  1092. DLLEXPORT void addMemberCall(T&& functionAddress,
  1093. FuncReturnType returnType = NO_VALUE,
  1094. std::initializer_list<GPRegister> gpParams = {},
  1095. std::initializer_list<FPRegister> fpParams = {},
  1096. GPRegister temp = RAX,
  1097. GPRegister bpTemp = RBP)
  1098. {
  1099. addCall((void*&)functionAddress,
  1100. returnType,
  1101. gpParams,
  1102. fpParams,
  1103. temp,
  1104. bpTemp);
  1105. }
  1106. /**
  1107. * returns from executing the compiled assembly function.
  1108. */
  1109. DLLEXPORT void addReturn();
  1110. /**
  1111. * pushes a register into the stack.
  1112. *
  1113. * \param reg the register to push
  1114. * \param part the part of the register to push
  1115. */
  1116. DLLEXPORT void addPush(
  1117. GPRegister reg, GPRegisterPart part = FULL64);
  1118. /**
  1119. * pops a value from the sack into a specified register.
  1120. *
  1121. * \param reg the register to store the popped value
  1122. * \param part the part of the register to store the popped value
  1123. */
  1124. DLLEXPORT void addPop(GPRegister reg, GPRegisterPart part = FULL64);
  1125. /**
  1126. * pushes a register into the stack.
  1127. *
  1128. * \param reg the register to push
  1129. * \param type the datatype of the register
  1130. * \param part the part of the register
  1131. */
  1132. DLLEXPORT void addPush(
  1133. FPRegister reg, FPDataType type, FPRegisterPart part = X);
  1134. /**
  1135. * pops a value from the sack into a specified register.
  1136. *
  1137. * \param reg the register to store the popped value
  1138. * \param type the datatype of the register
  1139. * \param part the part of the register
  1140. */
  1141. DLLEXPORT void addPop(
  1142. FPRegister reg, FPDataType type, FPRegisterPart part = X);
  1143. private:
  1144. DLLEXPORT void addPop(RCArray<Instruction>& instructionList,
  1145. FPRegister reg,
  1146. FPDataType type,
  1147. FPRegisterPart part = X);
  1148. public:
  1149. /**
  1150. * copies the assembly code from a given block of assembly
  1151. * instructions.
  1152. *
  1153. * \param block the block to inline
  1154. * \param preservedGPRegisters the GP registers that should be
  1155. * preserved during the inlined block. if a preserved register is
  1156. * used in the block, it will be replaced by another register or
  1157. * pushed to the stack if no free register is available.
  1158. * \param preservedFPRegisters same as preservedGPRegisters but for
  1159. * FP registers
  1160. * \param preservedFPDataTypes data types of the preserved FP
  1161. * registers used to push them to the stack if needed
  1162. * \param blockResultGpReg if the register this pointer points to
  1163. * should be preserved this pointer will be set to the register that
  1164. * was selected to replace the register during execution of the
  1165. * block. Ignored if 0.
  1166. * \param blockResultFpReg same as blockResultGpReg but for FP
  1167. * Registers
  1168. */
  1169. DLLEXPORT void addBlock(AssemblyBlock* block,
  1170. std::initializer_list<GPRegister> preservedGPRegisters,
  1171. std::initializer_list<FPRegister> preservedFPRegisters,
  1172. std::initializer_list<FPDataType> preservedFPDataTypes,
  1173. GPRegister* blockResultGpReg,
  1174. FPRegister* blockResultFpReg);
  1175. /**
  1176. * checks if this block writes to a specified register.
  1177. *
  1178. * \param reg the register to check
  1179. * \return true if the block writes to the register
  1180. */
  1181. DLLEXPORT bool writesToRegister(GPRegister reg) const;
  1182. /**
  1183. * checks if this block writes to a specified register.
  1184. *
  1185. * \param reg the register to check
  1186. * \return true if the block writes to the register
  1187. */
  1188. DLLEXPORT bool writesToRegister(FPRegister reg) const;
  1189. /**
  1190. * checks if this block reads from a specified register.
  1191. *
  1192. * \param reg the register to check
  1193. * \return true if the block reads from the register
  1194. */
  1195. DLLEXPORT bool readsFromRegister(GPRegister reg) const;
  1196. /**
  1197. * checks if this block reads from a specified register.
  1198. *
  1199. * \param reg the register to check
  1200. * \return true if the block reads from the register
  1201. */
  1202. DLLEXPORT bool readsFromRegister(FPRegister reg) const;
  1203. /**
  1204. * checks if a register can be replaced by another register.
  1205. *
  1206. * \param oldReg the register to replace
  1207. * \param newReg the register to use instead
  1208. * \return true if the replacement is possible
  1209. */
  1210. DLLEXPORT bool isReplacementPossible(
  1211. GPRegister oldReg, GPRegister newReg) const;
  1212. /**
  1213. * checks if a register can be replaced by another register.
  1214. *
  1215. * \param oldReg the register to replace
  1216. * \param newReg the register to use instead
  1217. * \return true if the replacement is possible
  1218. */
  1219. DLLEXPORT bool isReplacementPossible(
  1220. FPRegister oldReg, FPRegister newReg) const;
  1221. /**
  1222. * replaces a register with another register.
  1223. *
  1224. * \param oldReg the register to replace
  1225. * \param newReg the register to use instead
  1226. */
  1227. DLLEXPORT void replaceRegister(
  1228. GPRegister oldReg, GPRegister newReg);
  1229. /**
  1230. * replaces a register with another register.
  1231. *
  1232. * \param oldReg the register to replace
  1233. * \param newReg the register to use instead
  1234. */
  1235. DLLEXPORT void replaceRegister(
  1236. FPRegister oldReg, FPRegister newReg);
  1237. /**
  1238. * adds a prefix to all jump labels in this block to avoid conflicts
  1239. * if this block is inlined into another block.
  1240. *
  1241. * \param labelPrefix the label prefix to add
  1242. */
  1243. DLLEXPORT void addJumpLabelPrefix(Text labelPrefix);
  1244. /**
  1245. * \return the instructions of this block
  1246. */
  1247. DLLEXPORT const RCArray<Instruction>& getInstructions() const;
  1248. /**
  1249. * performs optimisations to shorten the needed instructions.
  1250. *
  1251. */
  1252. DLLEXPORT void optimize();
  1253. /**
  1254. * \return a pointer to a function that contains the compiled byte
  1255. * code of this assembly block. and can be called directly with
  1256. * reinterpret_cast<returnType(*)(parameterTypes...)>(compile())(parameters...)
  1257. */
  1258. DLLEXPORT void* compile();
  1259. /**
  1260. * \return a pointer to a function that contains the compiled byte
  1261. * code of this assembly block. and can be called directly with
  1262. */
  1263. template<typename T> DLLEXPORT T compileToFunction()
  1264. {
  1265. return reinterpret_cast<T>(compile());
  1266. }
  1267. };
  1268. } // namespace Assembly
  1269. } // namespace Framework