Assembly.h 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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 given value to the given part of the target
  689. * register and stores the result in the given part of the target
  690. * register. single operation.
  691. *
  692. * \param target the target register
  693. * \param value the value to add
  694. * \param part the target register part
  695. */
  696. DLLEXPORT void addAddition(
  697. GPRegister target, char value, GPRegisterPart part = LOWER8);
  698. /**
  699. * adds the given value to the given part of the target
  700. * register and stores the result in the given part of the target
  701. * register. single operation.
  702. *
  703. * \param target the target register
  704. * \param value the value to add
  705. * \param part the target register part (only LOWER32 or FULL64 are
  706. * valid)
  707. */
  708. DLLEXPORT void addAddition(
  709. GPRegister target, int value, GPRegisterPart part = LOWER32);
  710. /**
  711. * adds the value of the source register to the value of the target
  712. * register and stores the result in the target register. single
  713. * operation.
  714. *
  715. * \param target the target register
  716. * \param source the source register
  717. * \param type the data type stored in the registers
  718. * \param part the register part
  719. */
  720. DLLEXPORT void addAddition(FPRegister target,
  721. FPRegister source,
  722. FPDataType type,
  723. FPRegisterPart part = Y);
  724. /**
  725. * subtracts the value of the target register from the value of the
  726. * source register and stores the result in the target register.
  727. * single operation.
  728. *
  729. * \param target the target register
  730. * \param source the source register
  731. * \param part the register part
  732. */
  733. DLLEXPORT void addSubtraction(GPRegister target,
  734. GPRegister source,
  735. GPRegisterPart part = FULL64);
  736. /**
  737. * subtracts the value of the target register from the value of the
  738. * source register and stores the result in the target register.
  739. * single operation.
  740. *
  741. * \param target the target register
  742. * \param source the source register
  743. * \param type the data type stored in the registers
  744. * \param part the register part
  745. */
  746. DLLEXPORT void addSubtraction(FPRegister target,
  747. FPRegister source,
  748. FPDataType type,
  749. FPRegisterPart part = Y);
  750. /**
  751. * multiplies the value of the source register to the value of the
  752. * target register and stores the result in the target register.
  753. * single operation.
  754. *
  755. * \param target the target register
  756. * \param source the source register
  757. * \param part the register part
  758. */
  759. DLLEXPORT void addMultiplication(GPRegister target,
  760. GPRegister source,
  761. GPRegisterPart part = FULL64);
  762. /**
  763. * multiplies the value of the source register to the value of the
  764. * target register and stores the result in the target register.
  765. * single operation.
  766. *
  767. * \param target the target register
  768. * \param source the source register
  769. * \param type the data type stored in the registers
  770. * \param part the register part
  771. */
  772. DLLEXPORT void addMultiplication(FPRegister target,
  773. FPRegister source,
  774. FPDataType type,
  775. FPRegisterPart part = Y);
  776. /**
  777. * devides the value of the target register by the value of the
  778. * source register and stores the result in the target register.
  779. * single operation.
  780. *
  781. * \param target the target register
  782. * \param source the source register
  783. * \param part the register part
  784. */
  785. DLLEXPORT void addDivision(GPRegister target,
  786. GPRegister source,
  787. GPRegisterPart part = FULL64);
  788. /**
  789. * devides the value of the target register by the value of the
  790. * source register and stores the result in the target register.
  791. * single operation.
  792. *
  793. * \param target the target register
  794. * \param source the source register
  795. * \param type the data type stored in the registers
  796. * \param part the register part
  797. */
  798. DLLEXPORT void addDivision(FPRegister target,
  799. FPRegister source,
  800. FPDataType type,
  801. FPRegisterPart part = Y);
  802. /**
  803. * performes bitwise AND without storing the result. only
  804. * SpecialRegister.FLAGS (SF, ZF, PF) is affected. Single operation.
  805. *
  806. * \param r1 the first value
  807. * \param r2 the second value
  808. * \param part the part of the values to test
  809. */
  810. DLLEXPORT void addTest(
  811. GPRegister r1, GPRegister r2, GPRegisterPart part = FULL64);
  812. /**
  813. * subtracts the value of r2 from r1 and sets FLAGS (CF, OF, SF, ZF,
  814. * AF, PF) according to the result. The result is discarded.
  815. *
  816. * \param r1 the first value
  817. * \param r2 the second value
  818. * \param part the part of the values to compare
  819. */
  820. DLLEXPORT void addCompare(
  821. GPRegister r1, GPRegister r2, GPRegisterPart part = FULL64);
  822. /**
  823. * subtracts the value from r1 and sets FLAGS (CF, OF, SF, ZF,
  824. * AF, PF) according to the result. The result is discarded.
  825. *
  826. * \param r1 the first value
  827. * \param value the value to subtract
  828. */
  829. DLLEXPORT void addCompare(GPRegister r1, char value);
  830. /**
  831. * subtracts the value from r1 and sets FLAGS (CF, OF, SF, ZF,
  832. * AF, PF) according to the result. The result is discarded.
  833. *
  834. * \param r1 the first value
  835. * \param value the value to subtract
  836. */
  837. DLLEXPORT void addCompare(GPRegister r1, short value);
  838. /**
  839. * subtracts the value from r1 and sets FLAGS (CF, OF, SF, ZF,
  840. * AF, PF) according to the result. The result is discarded.
  841. *
  842. * \param r1 the first value
  843. * \param value the value to subtract
  844. */
  845. DLLEXPORT void addCompare(GPRegister r1, int value);
  846. /**
  847. * subtracts the value of r2 from r1 and sets FLAGS (ZF, PF, CF).
  848. * the result is discarded
  849. *
  850. * UNORDERED: ZF,PF,CF := 111;
  851. * GREATER_THAN: ZF,PF,CF := 000;
  852. * LESS_THAN: ZF,PF,CF := 001;
  853. * EUAL: ZF,PF,CF := 100;
  854. *
  855. * \param r1 the first value
  856. * \param r2 the second value
  857. * \param type the datatype to compare. only SINGLE types are valid
  858. */
  859. DLLEXPORT void addCompare(
  860. FPRegister r1, FPRegister r2, FPDataType type);
  861. /**
  862. * computes bitwise AND for the values in source and target
  863. * register and stores the result in the target register. single
  864. * operation
  865. *
  866. * \param target the target register
  867. * \param source the source register
  868. * \param part the register part
  869. */
  870. DLLEXPORT void addAnd(GPRegister target,
  871. GPRegister source,
  872. GPRegisterPart part = FULL64);
  873. /**
  874. * computes bitwise AND for the values in source and target register
  875. * and stores the result in the target register. single operation
  876. *
  877. * \param target the target register
  878. * \param source the source register
  879. * \param part the register part
  880. */
  881. DLLEXPORT void addOr(GPRegister target,
  882. GPRegister source,
  883. GPRegisterPart part = FULL64);
  884. /**
  885. * writes the specified valueAddress pointer into a register.
  886. *
  887. * \param valueAddress the pointer to the value witch address should
  888. * be stored
  889. * \param target the register where the address should be stored
  890. */
  891. DLLEXPORT void addLoadValue(char* valueAddress, GPRegister target);
  892. /**
  893. * writes the specified valueAddress pointer into a register.
  894. *
  895. * \param valueAddress the pointer to the value witch address should
  896. * be stored
  897. * \param target the register where the address should be stored
  898. */
  899. DLLEXPORT void addLoadValue(short* valueAddress, GPRegister target);
  900. /**
  901. * writes the specified valueAddress pointer into a register.
  902. *
  903. * \param valueAddress the pointer to the value witch address should
  904. * be stored
  905. * \param target the register where the address should be stored
  906. */
  907. DLLEXPORT void addLoadValue(int* valueAddress, GPRegister target);
  908. /**
  909. * writes the specified valueAddress pointer into a register.
  910. *
  911. * \param valueAddress the pointer to the value witch address should
  912. * be stored
  913. * \param target the register where the address should be stored
  914. */
  915. DLLEXPORT void addLoadValue(
  916. __int64* valueAddress, GPRegister target);
  917. /**
  918. * loads the value at the address stored in addressRegister into the
  919. * target register. Single operation.
  920. *
  921. * \param addressRegister a register that contains a memory address
  922. * \param target the target register to store the value into
  923. * \param targetPart the part of the target register that will be
  924. * written to
  925. */
  926. DLLEXPORT void addLoadValue(GPRegister addressRegister,
  927. GPRegister target,
  928. GPRegisterPart targetPart,
  929. int offset = 0);
  930. /**
  931. * writes the specified valueAddress pointer into a register.
  932. *
  933. * \param valueAddress the pointer to the value witch address should
  934. * be stored
  935. * \param target the register where the address should be stored
  936. */
  937. DLLEXPORT void addLoadValue(
  938. float* valueAddress, FPRegister target, GPRegister temp = RAX);
  939. /**
  940. * writes the specified valueAddress pointer into a register.
  941. *
  942. * \param valueAddress the pointer to the value witch address should
  943. * be stored
  944. * \param target the register where the address should be stored
  945. */
  946. DLLEXPORT void addLoadValue(
  947. double* valueAddress, FPRegister target, GPRegister temp = RAX);
  948. /**
  949. * calls a function at a specified memory address.
  950. *
  951. * \param functionAddress pointet to the address of the function to
  952. * call
  953. */
  954. template<typename T>
  955. DLLEXPORT void addLoadAddress(T* addr, GPRegister temp = RAX)
  956. {
  957. instructions.add(new Instruction(MOV,
  958. {new GPRegisterArgument(temp),
  959. new ConstantArgument(
  960. reinterpret_cast<__int64>(addr))}));
  961. }
  962. /**
  963. * moves the given value to LOWER8 bits of target register.
  964. *
  965. * \param target the register to move the value to
  966. * \param value the value to move
  967. */
  968. DLLEXPORT void addMoveValue(GPRegister target, char value);
  969. /**
  970. * moves the given value to LOWER16 bits of target register.
  971. *
  972. * \param target the register to move the value to
  973. * \param value the value to move
  974. */
  975. DLLEXPORT void addMoveValue(GPRegister target, short value);
  976. /**
  977. * moves the given value to LOWER32 bits of target register.
  978. *
  979. * \param target the register to move the value to
  980. * \param value the value to move
  981. */
  982. DLLEXPORT void addMoveValue(GPRegister target, int value);
  983. /**
  984. * moves the given value to LOWER64 bits of target register.
  985. *
  986. * \param target the register to move the value to
  987. * \param value the value to move
  988. */
  989. DLLEXPORT void addMoveValue(GPRegister target, __int64 value);
  990. /**
  991. * moves the given value to LOWER32 bits of target register.
  992. * The value needs to be temporarily pushed to the stack, so
  993. * addLoadValue should be used if performance is critical.
  994. *
  995. * \param target the register to move the value to
  996. * \param value the value to move
  997. * \param temp temporary register that can be used us store the
  998. * value
  999. */
  1000. DLLEXPORT void addMoveValue(
  1001. FPRegister target, float value, GPRegister temp = RAX);
  1002. /**
  1003. * moves the given value to LOWER64 bits of target register.
  1004. * The value needs to be temporarily pushed to the stack, so
  1005. * addLoadValue should be used if performance is critical.
  1006. *
  1007. * \param target the register to move the value to
  1008. * \param value the value to move
  1009. * \param temp temporary register that can be used us store the
  1010. * value
  1011. */
  1012. DLLEXPORT void addMoveValue(
  1013. FPRegister target, double value, GPRegister temp = RAX);
  1014. /**
  1015. * moves the value from source register to target register.
  1016. *
  1017. * \param target the register to move the value to
  1018. * \param source the register to move the value from
  1019. * \param part the part of the register to move
  1020. */
  1021. DLLEXPORT void addMoveValue(GPRegister target,
  1022. GPRegister source,
  1023. GPRegisterPart part = FULL64);
  1024. /**
  1025. * moves the value from source register to target register.
  1026. *
  1027. * \param target the register to move the value to
  1028. * \param source the register to move the value from
  1029. * \param part the part of the register to move
  1030. */
  1031. DLLEXPORT void addMoveValue(FPRegister target,
  1032. FPRegister source,
  1033. FPDataType type,
  1034. FPRegisterPart part = Y);
  1035. /**
  1036. * converts a floating point value from source register into a
  1037. * signed integer value and writes it into the target register.
  1038. * Single operation.
  1039. *
  1040. * \param target the target register
  1041. * \param source the source register
  1042. * \param type the datatype of the source register. Only SINGLE
  1043. * types are valid.
  1044. * \param targetPart the register part to write the result into.
  1045. * Only LOWER32 or FULL64 are valid.
  1046. * \param round if true the value is rounded acourding to bits 13
  1047. * and 14 of the MXCSR register value
  1048. */
  1049. DLLEXPORT void addConversion(GPRegister target,
  1050. FPRegister source,
  1051. FPDataType type,
  1052. GPRegisterPart targetPart,
  1053. bool round = 0);
  1054. /**
  1055. * converts a signed integer from source register into a floating
  1056. * point value and writes it into the target register. Single
  1057. * operation
  1058. *
  1059. * \param target the target register
  1060. * \param source the source register
  1061. * \param targetType the data type after the conversion. Only SINGLE
  1062. * types are valid.
  1063. * \param sourcePart the register part that contains the signed
  1064. * integer. Only LOWER32 and FULL64 are valid.
  1065. */
  1066. DLLEXPORT void addConversion(FPRegister target,
  1067. GPRegister source,
  1068. FPDataType targetType,
  1069. GPRegisterPart sourcePart);
  1070. /**
  1071. * calls a function at a specified memory address.
  1072. *
  1073. * \param functionAddress pointet to the address of the function to
  1074. * call
  1075. * \param gpParams must be the the every n-th entry of the register
  1076. * list (RCX, RDX, R8, R9) for each n-th function parameter that is
  1077. * of type integer
  1078. * \param fpParams must be the the every n-th entry of the register
  1079. * list (MM0, MM1, MM2, MM3) for each n-th function parameter that
  1080. * is of type float/double
  1081. * \param temp temporary register to store the address of the
  1082. * function
  1083. * \param bpTemp temporary register to store the stack pointer
  1084. * before ligning the stack pointer to a 16 byte boundry. The stack
  1085. * pointer will be automatically restored after the function call
  1086. * using the value of this register. A non-volatile register must be
  1087. * used.
  1088. */
  1089. DLLEXPORT void addCall(void* functionAddress,
  1090. FuncReturnType returnType = NO_VALUE,
  1091. std::initializer_list<GPRegister> gpParams = {},
  1092. std::initializer_list<FPRegister> fpParams = {},
  1093. GPRegister temp = RAX,
  1094. GPRegister bpTemp = RBP);
  1095. /**
  1096. * calls a function at a specified memory address.
  1097. *
  1098. * \param functionAddress pointet to the address of the function to
  1099. * call
  1100. * \param gpParams must be the the every n-th entry of the register
  1101. * list (RCX, RDX, R8, R9) for each n-th function parameter that is
  1102. * of type integer
  1103. * \param fpParams must be the the every n-th entry of the register
  1104. * list (MM0, MM1, MM2, MM3) for each n-th function parameter that
  1105. * is of type float/double
  1106. * \param temp temporary register to store the address of the
  1107. * function
  1108. * \param bpTemp temporary register to store the stack pointer
  1109. * before ligning the stack pointer to a 16 byte boundry. The stack
  1110. * pointer will be automatically restored after the function call
  1111. * using the value of this register. A non-volatile register must be
  1112. * used.
  1113. */
  1114. template<typename T>
  1115. DLLEXPORT void addMemberCall(T&& functionAddress,
  1116. FuncReturnType returnType = NO_VALUE,
  1117. std::initializer_list<GPRegister> gpParams = {},
  1118. std::initializer_list<FPRegister> fpParams = {},
  1119. GPRegister temp = RAX,
  1120. GPRegister bpTemp = RBP)
  1121. {
  1122. addCall((void*&)functionAddress,
  1123. returnType,
  1124. gpParams,
  1125. fpParams,
  1126. temp,
  1127. bpTemp);
  1128. }
  1129. /**
  1130. * returns from executing the compiled assembly function.
  1131. */
  1132. DLLEXPORT void addReturn();
  1133. /**
  1134. * pushes a register into the stack.
  1135. *
  1136. * \param reg the register to push
  1137. * \param part the part of the register to push
  1138. */
  1139. DLLEXPORT void addPush(
  1140. GPRegister reg, GPRegisterPart part = FULL64);
  1141. /**
  1142. * pops a value from the sack into a specified register.
  1143. *
  1144. * \param reg the register to store the popped value
  1145. * \param part the part of the register to store the popped value
  1146. */
  1147. DLLEXPORT void addPop(GPRegister reg, GPRegisterPart part = FULL64);
  1148. /**
  1149. * pushes a register into the stack.
  1150. *
  1151. * \param reg the register to push
  1152. * \param type the datatype of the register
  1153. * \param part the part of the register
  1154. */
  1155. DLLEXPORT void addPush(
  1156. FPRegister reg, FPDataType type, FPRegisterPart part = X);
  1157. /**
  1158. * pops a value from the sack into a specified register.
  1159. *
  1160. * \param reg the register to store the popped value
  1161. * \param type the datatype of the register
  1162. * \param part the part of the register
  1163. */
  1164. DLLEXPORT void addPop(
  1165. FPRegister reg, FPDataType type, FPRegisterPart part = X);
  1166. private:
  1167. DLLEXPORT void addPop(RCArray<Instruction>& instructionList,
  1168. FPRegister reg,
  1169. FPDataType type,
  1170. FPRegisterPart part = X);
  1171. public:
  1172. /**
  1173. * copies the assembly code from a given block of assembly
  1174. * instructions.
  1175. *
  1176. * \param block the block to inline
  1177. * \param preservedGPRegisters the GP registers that should be
  1178. * preserved during the inlined block. if a preserved register is
  1179. * used in the block, it will be replaced by another register or
  1180. * pushed to the stack if no free register is available.
  1181. * \param preservedFPRegisters same as preservedGPRegisters but for
  1182. * FP registers
  1183. * \param preservedFPDataTypes data types of the preserved FP
  1184. * registers used to push them to the stack if needed
  1185. * \param blockResultGpReg if the register this pointer points to
  1186. * should be preserved this pointer will be set to the register that
  1187. * was selected to replace the register during execution of the
  1188. * block. Ignored if 0.
  1189. * \param blockResultFpReg same as blockResultGpReg but for FP
  1190. * Registers
  1191. */
  1192. DLLEXPORT void addBlock(AssemblyBlock* block,
  1193. std::initializer_list<GPRegister> preservedGPRegisters,
  1194. std::initializer_list<FPRegister> preservedFPRegisters,
  1195. std::initializer_list<FPDataType> preservedFPDataTypes,
  1196. GPRegister* blockResultGpReg,
  1197. FPRegister* blockResultFpReg);
  1198. /**
  1199. * checks if this block writes to a specified register.
  1200. *
  1201. * \param reg the register to check
  1202. * \return true if the block writes to the register
  1203. */
  1204. DLLEXPORT bool writesToRegister(GPRegister reg) const;
  1205. /**
  1206. * checks if this block writes to a specified register.
  1207. *
  1208. * \param reg the register to check
  1209. * \return true if the block writes to the register
  1210. */
  1211. DLLEXPORT bool writesToRegister(FPRegister reg) const;
  1212. /**
  1213. * checks if this block reads from a specified register.
  1214. *
  1215. * \param reg the register to check
  1216. * \return true if the block reads from the register
  1217. */
  1218. DLLEXPORT bool readsFromRegister(GPRegister reg) const;
  1219. /**
  1220. * checks if this block reads from a specified register.
  1221. *
  1222. * \param reg the register to check
  1223. * \return true if the block reads from the register
  1224. */
  1225. DLLEXPORT bool readsFromRegister(FPRegister reg) const;
  1226. /**
  1227. * checks if a register can be replaced by another register.
  1228. *
  1229. * \param oldReg the register to replace
  1230. * \param newReg the register to use instead
  1231. * \return true if the replacement is possible
  1232. */
  1233. DLLEXPORT bool isReplacementPossible(
  1234. GPRegister oldReg, GPRegister newReg) const;
  1235. /**
  1236. * checks if a register can be replaced by another register.
  1237. *
  1238. * \param oldReg the register to replace
  1239. * \param newReg the register to use instead
  1240. * \return true if the replacement is possible
  1241. */
  1242. DLLEXPORT bool isReplacementPossible(
  1243. FPRegister oldReg, FPRegister newReg) const;
  1244. /**
  1245. * replaces a register with another register.
  1246. *
  1247. * \param oldReg the register to replace
  1248. * \param newReg the register to use instead
  1249. */
  1250. DLLEXPORT void replaceRegister(
  1251. GPRegister oldReg, GPRegister newReg);
  1252. /**
  1253. * replaces a register with another register.
  1254. *
  1255. * \param oldReg the register to replace
  1256. * \param newReg the register to use instead
  1257. */
  1258. DLLEXPORT void replaceRegister(
  1259. FPRegister oldReg, FPRegister newReg);
  1260. /**
  1261. * adds a prefix to all jump labels in this block to avoid conflicts
  1262. * if this block is inlined into another block.
  1263. *
  1264. * \param labelPrefix the label prefix to add
  1265. */
  1266. DLLEXPORT void addJumpLabelPrefix(Text labelPrefix);
  1267. /**
  1268. * \return the instructions of this block
  1269. */
  1270. DLLEXPORT const RCArray<Instruction>& getInstructions() const;
  1271. /**
  1272. * performs optimisations to shorten the needed instructions.
  1273. *
  1274. */
  1275. DLLEXPORT void optimize();
  1276. /**
  1277. * \return a pointer to a function that contains the compiled byte
  1278. * code of this assembly block. and can be called directly with
  1279. * reinterpret_cast<returnType(*)(parameterTypes...)>(compile())(parameters...)
  1280. */
  1281. DLLEXPORT void* compile();
  1282. /**
  1283. * \return a pointer to a function that contains the compiled byte
  1284. * code of this assembly block. and can be called directly with
  1285. */
  1286. template<typename T> DLLEXPORT T compileToFunction()
  1287. {
  1288. return reinterpret_cast<T>(compile());
  1289. }
  1290. };
  1291. } // namespace Assembly
  1292. } // namespace Framework