Assembly.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. // temporary subtracts op2 from op without storing the result. only
  54. // SpecialRegister.FLAGS is affected
  55. CMPPD,
  56. // Compare Packed Single Precision Floating-Point Values
  57. // temporary subtracts op2 from op without storing the result. only
  58. // SpecialRegister.FLAGS is affected
  59. CMPPS,
  60. // Compare Scalar Double Precision Floating-Point Value
  61. // temporary subtracts op2 from op without storing the result. only
  62. // SpecialRegister.FLAGS is affected
  63. CMPSD,
  64. // Compare Scalar Single Precision Floating-Point Value
  65. // temporary subtracts op2 from op without storing the result. only
  66. // SpecialRegister.FLAGS is affected
  67. CMPSS,
  68. // Data Movement
  69. MOV, // Move data from source to destination
  70. // Move Aligned Packed Double-Precision Floating-Point Value
  71. MOVAPD,
  72. // Move Aligned Packed Single-Precision Floating-Point Value
  73. MOVAPS,
  74. MOVSD, // Move Scalar Double-Precision Floating-Point Value
  75. MOVSS, // Move Scalar Single-Precision Floating-Point Value
  76. LEA, // Load Effective Address
  77. // Control Flow
  78. JMP, // Unconditional Jump
  79. JZ, // Jump if Zero: SpecialRegister.FLAGS(ZF) = 1
  80. JNZ, // Jump if Not Zero: SpecialRegister.FLAGS(ZF) = 0
  81. // Jump if Greater: SpecialRegister.FLAGS(SF) =
  82. // SpecialRegister.FLAGS(OF) and SpecialRegister.FLAGS(ZF) = 0
  83. JG,
  84. // Jump if Greater or Equal: SpecialRegister.FLAGS(SF) =
  85. // SpecialRegister.FLAGS(OF)
  86. JGE,
  87. // Jump if Less: SpecialRegister.FLAGS(SF) !=
  88. // SpecialRegister.FLAGS(OF)
  89. JL,
  90. // Jump if Less or Equal: SpecialRegister.FLAGS(SF) !=
  91. // SpecialRegister.FLAGS(OF) or SpecialRegister.FLAGS(ZF) = 1
  92. JLE,
  93. // Jump if above: SpecialRegister.FLAGS(CF) = 0 and
  94. // SpecialRegister.FLAGS(ZF) = 0
  95. JA,
  96. // Jump if carry: SpecialRegister.FLAGS(CF) = 1
  97. JC,
  98. // Jump if not carry: SpecialRegister.FLAGS(CF) = 0
  99. JNC,
  100. // Jump if below or equal: SpecialRegister.FLAGS(CF) = 1 or
  101. // SpecialRegister.FLAGS(ZF) = 1
  102. JBE,
  103. JO, // Jump if overflow: SpecialRegister.FLAGS(OF) = 1
  104. JNO, // Jump if not overflow: SpecialRegister.FLAGS(OF) = 0
  105. JP, // Jump if parity even: SpecialRegister.FLAGS(PF) = 1
  106. JNP, // Jump if not parity odd: SpecialRegister.FLAGS(PF) = 0
  107. JS, // Jump if sign: SpecialRegister.FLAGS(SF) = 1
  108. JNS, // Jump if not sign: SpecialRegister.FLAGS(SF) = 0
  109. CALL, // Call subroutine
  110. RET, // Return from subroutine
  111. // Stack Operations
  112. PUSH, // Push onto stack
  113. POP // Pop from stack
  114. };
  115. enum CMP_IMM8
  116. {
  117. EQ_OQ = 0, // Equal (ordered, non-signaling)
  118. LT_OS = 1, // Less than (ordered, signaling)
  119. LE_OS = 2, // Less than or equal (ordered, signaling
  120. UNORD_Q = 3, // Unordered (non-signaling)
  121. NEQ_UQ = 4, // Not-equal (unordered, non-signaling)
  122. NLT_US = 5, // Not less than (unordered, signaling)
  123. NLE_US = 6, // Not-less-than-or-equal (unordered, signaling)
  124. ORD_Q = 7, // Ordered (non-signaling)
  125. EQ_US = 8, // Equal (unordered, signaling)
  126. NGE_US = 9, // Not-greater-than-or-equal (unordered, signaling)
  127. NGT_US = 10, // Not-greater-than (unordered, signaling)
  128. FALSE_OQ = 11, // False (ordered, non-signaling)
  129. NEQ_OQ = 12, // Not-equal (ordered, non-signaling)
  130. GE_OS = 13, // Greater-than-or-equal (ordered, signaling)
  131. GT_OS = 14, // Greater-than (ordered, signaling)
  132. TRUE_UQ = 15, // True (unordered, non-signaling)
  133. EQ_OS = 16, // Equal (ordered, signaling)
  134. LT_OQ = 17, // Less-than (ordered, non-signaling)
  135. LE_OQ = 18, // Less-than-or-equal (ordered, non
  136. UNORD_S = 19, // Unordered (signaling)
  137. NEQ_US = 20, // Not-equal (unordered, signaling)
  138. NLT_UQ = 21, // Not-less-than (unordered, non-sign
  139. NLE_UQ = 22, // Not-less-than-or-equal (unordered, non-signaling)
  140. ORD_S = 23, // Ordered (signaling)
  141. EQ_UQ = 24, // Equal (unordered, non-signaling)
  142. NGE_UQ = 25, // Not-greater-than-or-equal (
  143. NGT_UQ = 26, // Not-greater-than (unordered, non-signaling)
  144. FALSE_OS = 27, // False (ordered, signaling)
  145. NEQ_OS = 28, // Not-equal (ordered, signaling)
  146. GE_OQ = 29, // Greater-than-or-equal (ordered, non
  147. GT_OQ = 30, // Greater-than (ordered, non-signaling)
  148. TRUE_S = 31 // True (signaling)
  149. };
  150. // General Purpose Registers
  151. enum GPRegister
  152. {
  153. // volatile register (can be altered by a function call)
  154. RAX = 0b0000,
  155. // non-volatile register (mus be restored on return)
  156. RBX = 0b0011,
  157. // volatile register (can be altered by a function call)
  158. RCX = 0b0001,
  159. // volatile register (can be altered by a function call)
  160. RDX = 0b0010,
  161. // Stack pointer points to the position of the last item that was
  162. // pushed to the stack. The stack grows downwards so lower means
  163. // more elements in the stack. needs to be aligned to 16 bytes
  164. RSP = 0b0100,
  165. // non-volatile register (mus be restored on return)
  166. RBP = 0b0101,
  167. // non-volatile register (mus be restored on return)
  168. RSI = 0b0110,
  169. // non-volatile register (mus be restored on return)
  170. RDI = 0b0111,
  171. // volatile register (can be altered by a function call)
  172. R8 = 0b1000,
  173. // volatile register (can be altered by a function call)
  174. R9 = 0b1001,
  175. // volatile register (can be altered by a function call)
  176. R10 = 0b1010,
  177. // volatile register (can be altered by a function call)
  178. R11 = 0b1011,
  179. // non-volatile register (mus be restored on return)
  180. R12 = 0b1100,
  181. // non-volatile register (mus be restored on return)
  182. R13 = 0b1101,
  183. // non-volatile register (mus be restored on return)
  184. R14 = 0b1110,
  185. // non-volatile register (mus be restored on return)
  186. R15 = 0b1111
  187. };
  188. enum SpecialRegister
  189. {
  190. // Instruction pointer points to the instruction that is executed
  191. // next
  192. RIP,
  193. /**
  194. only lower 16 bits are used:
  195. - CF: Carry Flag
  196. - PF: Parity Flag (1 if an even number of bits was set to 1 in
  197. the result of the last operation)
  198. - AF: Adjust Flag
  199. - ZF: Zero Flag (1 if the result of the last operation was zero)
  200. - SF: Sign Flag (1 if the result of the last operation was a
  201. positive number)
  202. - TF: Trap Flag
  203. - IF: Interrupt Flag
  204. - DF: Direction Flag
  205. - OF: Overflow Flag
  206. */
  207. FLAGS,
  208. };
  209. /**
  210. floating point registers
  211. */
  212. enum FPRegister
  213. {
  214. MM0,
  215. MM1,
  216. MM2,
  217. MM3,
  218. MM4,
  219. MM5,
  220. MM6,
  221. MM7,
  222. MM8,
  223. MM9,
  224. MM10,
  225. MM11,
  226. MM12,
  227. MM13,
  228. MM14,
  229. MM15,
  230. __FP_REGISTER_COUNT
  231. };
  232. /**
  233. describes the bits of the specified register that should be used.
  234. */
  235. enum GPRegisterPart
  236. {
  237. LOWER8,
  238. HIGHER8,
  239. LOWER16,
  240. LOWER32,
  241. FULL64
  242. };
  243. /**
  244. describes the bits of the specified register that should be used.
  245. */
  246. enum FPRegisterPart
  247. {
  248. X,
  249. Y,
  250. // Z
  251. };
  252. class AssemblyBlock;
  253. class GPRegisterArgument;
  254. class FPRegisterArgument;
  255. class MemoryAccessArgument;
  256. class ConstantArgument;
  257. class JumpTargetArgument;
  258. /**
  259. An argument for an Instruction.
  260. */
  261. class OperationArgument
  262. {
  263. private:
  264. public:
  265. /**
  266. * checks if a register is used in this argument.
  267. *
  268. * \param reg the register to check
  269. * \return true if the register is used
  270. */
  271. DLLEXPORT virtual bool usesRegister(GPRegister reg) const;
  272. /**
  273. * checks if a register is used in this argument.
  274. *
  275. * \param reg the register to check
  276. * \return true if the register is used
  277. */
  278. DLLEXPORT virtual bool usesRegister(FPRegister reg) const;
  279. /**
  280. * replaces a register with another register.
  281. *
  282. * \param oldReg the register to replace
  283. * \param newReg the register to use instead
  284. */
  285. DLLEXPORT virtual void replaceRegister(
  286. GPRegister oldReg, GPRegister newReg);
  287. /**
  288. * replaces a register with another register.
  289. *
  290. * \param oldReg the register to replace
  291. * \param newReg the register to use instead
  292. */
  293. DLLEXPORT virtual void replaceRegister(
  294. FPRegister oldReg, FPRegister newReg);
  295. /**
  296. * adds a prefix to all jump labels in this argument to avoid
  297. * conflicts if the assembly block that contains this argument is
  298. * inlined into another block.
  299. *
  300. * \param labelPrefix the label prefix to add
  301. */
  302. DLLEXPORT virtual void addJumpLabelPrefix(Text labelPrefix);
  303. /**
  304. * \return the GPRegisterArgument or 0 if it is not a
  305. * GPRegisterArgument
  306. */
  307. DLLEXPORT const GPRegisterArgument* asGPRegisterArgument() const;
  308. /**
  309. * \return the GPRegisterArgument or 0 if it is not a
  310. * GPRegisterArgument
  311. */
  312. DLLEXPORT const MemoryAccessArgument*
  313. asMemoryAccessArgument() const;
  314. /**
  315. * \return the ConstantArgument or 0 if it is not a
  316. * ConstantArgument
  317. */
  318. DLLEXPORT const ConstantArgument* asConstantArgument() const;
  319. /**
  320. * \return the FPRegisterArgument or 0 if it is not a
  321. * FPRegisterArgument
  322. */
  323. DLLEXPORT const FPRegisterArgument* asFPRegisterArgument() const;
  324. /**
  325. * \return the JumpTargetArgument or 0 if it is not a
  326. * JumpTargetArgument
  327. */
  328. DLLEXPORT const JumpTargetArgument* asJumpTargetArgument() const;
  329. };
  330. /**
  331. Represents the usage of a GPRegister as an Instruction Argument.
  332. */
  333. class GPRegisterArgument : public OperationArgument
  334. {
  335. private:
  336. GPRegister reg;
  337. GPRegisterPart part;
  338. public:
  339. DLLEXPORT GPRegisterArgument(
  340. GPRegister reg, GPRegisterPart part = GPRegisterPart::FULL64);
  341. DLLEXPORT bool usesRegister(GPRegister reg) const override;
  342. DLLEXPORT void replaceRegister(
  343. GPRegister oldReg, GPRegister newReg) override;
  344. DLLEXPORT GPRegister getRegister() const;
  345. DLLEXPORT GPRegisterPart getPart() const;
  346. };
  347. /**
  348. Represents the usage of a FPRegister as an Instruction Argument.
  349. */
  350. class FPRegisterArgument : public OperationArgument
  351. {
  352. private:
  353. FPRegister reg;
  354. FPRegisterPart part;
  355. public:
  356. DLLEXPORT FPRegisterArgument(FPRegister reg, FPRegisterPart = X);
  357. DLLEXPORT bool usesRegister(FPRegister reg) const override;
  358. DLLEXPORT void replaceRegister(
  359. FPRegister oldReg, FPRegister newReg) override;
  360. DLLEXPORT FPRegister getRegister() const;
  361. DLLEXPORT FPRegisterPart getPart() const;
  362. };
  363. enum class MemoryBlockSize
  364. {
  365. // 8 bist
  366. BYTE = 1,
  367. // 16 bits
  368. WORD = 2,
  369. // 32 bits
  370. DWORD = 4,
  371. // 64 bits
  372. QWORD = 8,
  373. // 128 bits
  374. M128 = 16,
  375. // 256 bits
  376. M256 = 32,
  377. // 512 bits
  378. // M512 = 64
  379. };
  380. /**
  381. Represents the usage of a Memory Read Request as an Instruction
  382. Argument.
  383. */
  384. class MemoryAccessArgument : public OperationArgument
  385. {
  386. private:
  387. bool useAddressReg;
  388. GPRegister address;
  389. int offset; // offset from the address in the register
  390. GPRegister offsetReg;
  391. bool useOffsetReg;
  392. MemoryBlockSize blockSize; // size of the block to access (1,2,4,8)
  393. public:
  394. DLLEXPORT MemoryAccessArgument(MemoryBlockSize blockSize,
  395. GPRegister address,
  396. bool useAddressReg = true,
  397. int offset = 0,
  398. bool useOffsetReg = false,
  399. GPRegister offsetReg = RAX);
  400. DLLEXPORT bool usesRegister(GPRegister reg) const override;
  401. DLLEXPORT void replaceRegister(
  402. GPRegister oldReg, GPRegister newReg) override;
  403. DLLEXPORT bool isUsingAddressRegister() const;
  404. DLLEXPORT GPRegister getAddressRegister() const;
  405. DLLEXPORT int getOffset() const;
  406. DLLEXPORT bool isUsingOffsetRegister() const;
  407. DLLEXPORT GPRegister getOffsetRegister() const;
  408. DLLEXPORT MemoryBlockSize getBlockSize() const;
  409. };
  410. /**
  411. Represents the usage of a const value as an Instruction Argument.
  412. */
  413. class ConstantArgument : public OperationArgument
  414. {
  415. private:
  416. __int64 value;
  417. MemoryBlockSize size; // size in byte
  418. public:
  419. DLLEXPORT ConstantArgument(
  420. __int64 value, MemoryBlockSize size = MemoryBlockSize::QWORD);
  421. DLLEXPORT ConstantArgument(
  422. int value, MemoryBlockSize size = MemoryBlockSize::DWORD);
  423. DLLEXPORT ConstantArgument(
  424. short value, MemoryBlockSize size = MemoryBlockSize::WORD);
  425. DLLEXPORT ConstantArgument(
  426. char value, MemoryBlockSize size = MemoryBlockSize::BYTE);
  427. DLLEXPORT __int64 getValue() const;
  428. DLLEXPORT MemoryBlockSize getSize() const;
  429. };
  430. /**
  431. Represents the usage of a jump label as an Instruction Argument.
  432. */
  433. class JumpTargetArgument : public OperationArgument
  434. {
  435. private:
  436. Text name;
  437. public:
  438. DLLEXPORT JumpTargetArgument(Text name);
  439. DLLEXPORT void addJumpLabelPrefix(Text labelPrefix) override;
  440. const Text& getLabel() const;
  441. };
  442. /**
  443. Represents a single assembly instruction with its arguments.
  444. */
  445. class Instruction : public Framework::ReferenceCounter
  446. {
  447. private:
  448. Operation op;
  449. std::vector<OperationArgument*> args;
  450. public:
  451. DLLEXPORT Instruction(
  452. Operation op, std::initializer_list<OperationArgument*> params);
  453. DLLEXPORT ~Instruction();
  454. /**
  455. * checks if this instruction reads from a specified register.
  456. *
  457. * \param reg the register to check
  458. * \return true if the instruction reads from the register
  459. */
  460. DLLEXPORT bool writesToRegister(
  461. GPRegister reg, const AssemblyBlock* block) const;
  462. /**
  463. * checks if this instruction reads from a specified register.
  464. *
  465. * \param reg the register to check
  466. * \return true if the instruction reads from the register
  467. */
  468. DLLEXPORT bool writesToRegister(
  469. FPRegister reg, const AssemblyBlock* block) const;
  470. /**
  471. * checks if this instruction reads from a specified register.
  472. *
  473. * \param reg the register to check
  474. * \return true if the instruction reads from the register
  475. */
  476. DLLEXPORT bool readsFromRegister(
  477. GPRegister reg, const AssemblyBlock* block) const;
  478. /**
  479. * checks if this instruction reads from a specified register.
  480. *
  481. * \param reg the register to check
  482. * \return true if the instruction reads from the register
  483. */
  484. DLLEXPORT bool readsFromRegister(
  485. FPRegister reg, const AssemblyBlock* block) const;
  486. /**
  487. * checks if a register can be replaced by another register.
  488. *
  489. * \param oldReg the register to replace
  490. * \param newReg the register to use instead
  491. * \return true if the replacement is possible
  492. */
  493. DLLEXPORT bool isReplacementPossible(GPRegister oldReg,
  494. GPRegister newReg,
  495. const AssemblyBlock* block) const;
  496. /**
  497. * checks if a register can be replaced by another register.
  498. *
  499. * \param oldReg the register to replace
  500. * \param newReg the register to use instead
  501. * \return true if the replacement is possible
  502. */
  503. DLLEXPORT bool isReplacementPossible(FPRegister oldReg,
  504. FPRegister newReg,
  505. const AssemblyBlock* block) const;
  506. /**
  507. * replaces a register with another register.
  508. *
  509. * \param oldReg the register to replace
  510. * \param newReg the register to use instead
  511. */
  512. DLLEXPORT void replaceRegister(
  513. GPRegister oldReg, GPRegister newReg);
  514. /**
  515. * replaces a register with another register.
  516. *
  517. * \param oldReg the register to replace
  518. * \param newReg the register to use instead
  519. */
  520. DLLEXPORT void replaceRegister(
  521. FPRegister oldReg, FPRegister newReg);
  522. /**
  523. * adds a prefix to all jump labels in this instruction to avoid
  524. * conflicts if the assembly block that contains this instruction is
  525. * inlined into another block.
  526. *
  527. * \param labelPrefix the label prefix to add
  528. */
  529. DLLEXPORT void addJumpLabelPrefix(Text labelPrefix);
  530. /**
  531. * compiles this Instruction to macine code.
  532. *
  533. * \param machineCodeWriter the machine code will be written to this
  534. * writer
  535. * \param block the block that contains this instruction. needed to
  536. * resolve jump labels
  537. */
  538. DLLEXPORT void compile(StreamWriter* machineCodeWriter,
  539. const AssemblyBlock* block) const;
  540. /**
  541. * calculates the bytes needed for this instruction in machine code.
  542. *
  543. * \return the bytes needed
  544. */
  545. DLLEXPORT int compiledSize(const AssemblyBlock* block) const;
  546. /**
  547. * \return the op code of this instruction
  548. */
  549. DLLEXPORT Operation getOperation() const;
  550. /**
  551. * \return true if the given label is defined by this operation
  552. */
  553. DLLEXPORT bool definesLabel(Text label) const;
  554. };
  555. /**
  556. Represents a block of assembly instructions that can be compiled to
  557. machine code or inlined into another AssemblyBlock with addBlock(...).
  558. */
  559. class AssemblyBlock
  560. {
  561. private:
  562. RCArray<Instruction> instructions;
  563. int inlineIndex;
  564. void* compiledCode;
  565. public:
  566. DLLEXPORT AssemblyBlock();
  567. DLLEXPORT ~AssemblyBlock();
  568. /**
  569. * adds an instruction.
  570. *
  571. * \param instr the instruction to add.
  572. */
  573. DLLEXPORT void addInstruction(Instruction* instr);
  574. /**
  575. * defines a new jump target label.
  576. *
  577. * \param name the name of the label.
  578. */
  579. DLLEXPORT void defineJumpTarget(Text name);
  580. /**
  581. * adds a jump instruction that jumps to the next Instaruction after
  582. * the definition of the given label.
  583. *
  584. * \param jumpOp the op code of the jump iperation.
  585. * \param targetName the label to jump to.
  586. */
  587. DLLEXPORT void addJump(Operation jumpOp, Text targetName);
  588. /**
  589. * writes the specified valueAddress pointer into a register.
  590. *
  591. * \param valueAddress the pointer to the value witch address should
  592. * be stored
  593. * \param target the register where the address should be stored
  594. */
  595. DLLEXPORT void addLoadValue(char* valueAddress, GPRegister target);
  596. /**
  597. * writes the specified valueAddress pointer into a register.
  598. *
  599. * \param valueAddress the pointer to the value witch address should
  600. * be stored
  601. * \param target the register where the address should be stored
  602. */
  603. DLLEXPORT void addLoadValue(short* valueAddress, GPRegister target);
  604. /**
  605. * writes the specified valueAddress pointer into a register.
  606. *
  607. * \param valueAddress the pointer to the value witch address should
  608. * be stored
  609. * \param target the register where the address should be stored
  610. */
  611. DLLEXPORT void addLoadValue(int* valueAddress, GPRegister target);
  612. /**
  613. * writes the specified valueAddress pointer into a register.
  614. *
  615. * \param valueAddress the pointer to the value witch address should
  616. * be stored
  617. * \param target the register where the address should be stored
  618. */
  619. DLLEXPORT void addLoadValue(
  620. __int64* valueAddress, GPRegister target);
  621. /**
  622. * writes the specified valueAddress pointer into a register.
  623. *
  624. * \param valueAddress the pointer to the value witch address should
  625. * be stored
  626. * \param target the register where the address should be stored
  627. */
  628. DLLEXPORT void addLoadValue(
  629. float* valueAddress, FPRegister target, GPRegister temp = RAX);
  630. /**
  631. * writes the specified valueAddress pointer into a register.
  632. *
  633. * \param valueAddress the pointer to the value witch address should
  634. * be stored
  635. * \param target the register where the address should be stored
  636. */
  637. DLLEXPORT void addLoadValue(
  638. double* valueAddress, FPRegister target, GPRegister temp = RAX);
  639. /**
  640. * calls a function at a specified memory address.
  641. *
  642. * \param functionAddress pointet to the address of the function to
  643. * call
  644. */
  645. DLLEXPORT void addCall(
  646. void* functionAddress, GPRegister temp = RAX);
  647. /**
  648. * returns from executing the compiled assembly function.
  649. */
  650. DLLEXPORT void addReturn();
  651. /**
  652. * pushes a register into the stack.
  653. *
  654. * \param reg the register to push
  655. * \param part the part of the register to push
  656. */
  657. DLLEXPORT void addPush(
  658. GPRegister reg, GPRegisterPart part = FULL64);
  659. /**
  660. * pops a value from the sack into a specified register.
  661. *
  662. * \param reg the register to store the popped value
  663. * \param part the part of the register to store the popped value
  664. */
  665. DLLEXPORT void addPop(GPRegister reg, GPRegisterPart part = FULL64);
  666. /**
  667. * pushes a register into the stack.
  668. *
  669. * \param reg the register to push
  670. */
  671. DLLEXPORT void addPush(FPRegister reg, FPRegisterPart part = X);
  672. /**
  673. * pops a value from the sack into a specified register.
  674. *
  675. * \param reg the register to store the popped value
  676. */
  677. DLLEXPORT void addPop(FPRegister reg, FPRegisterPart part = X);
  678. /**
  679. * copies the assembly code from a given block of assembly
  680. * instructions.
  681. *
  682. * \param block the block to inline
  683. * \param preservedGPRegisters the GP registers that should be
  684. * preserved during the inlined block. if a preserved register is
  685. * used in the block, it will be replaced by another register or
  686. * pushed to the stack if no free register is available.
  687. * \param preservedFPRegisters same as preservedGPRegisters but for
  688. * FP registers
  689. * \param blockResultGpReg if the register RAX should be preserved
  690. * this pointer will be set to the register that was selected to
  691. * replace RAX during execution of the block.
  692. * \param blockResultFpReg same as blockResultGpReg but for XMM0
  693. */
  694. DLLEXPORT void addBlock(AssemblyBlock* block,
  695. std::initializer_list<GPRegister> preservedGPRegisters,
  696. std::initializer_list<FPRegister> preservedFPRegisters,
  697. GPRegister* blockResultGpReg,
  698. FPRegister* blockResultFpReg);
  699. /**
  700. * checks if this block writes to a specified register.
  701. *
  702. * \param reg the register to check
  703. * \return true if the block writes to the register
  704. */
  705. DLLEXPORT bool writesToRegister(GPRegister reg) const;
  706. /**
  707. * checks if this block writes to a specified register.
  708. *
  709. * \param reg the register to check
  710. * \return true if the block writes to the register
  711. */
  712. DLLEXPORT bool writesToRegister(FPRegister reg) const;
  713. /**
  714. * checks if this block reads from a specified register.
  715. *
  716. * \param reg the register to check
  717. * \return true if the block reads from the register
  718. */
  719. DLLEXPORT bool readsFromRegister(GPRegister reg) const;
  720. /**
  721. * checks if this block reads from a specified register.
  722. *
  723. * \param reg the register to check
  724. * \return true if the block reads from the register
  725. */
  726. DLLEXPORT bool readsFromRegister(FPRegister reg) const;
  727. /**
  728. * checks if a register can be replaced by another register.
  729. *
  730. * \param oldReg the register to replace
  731. * \param newReg the register to use instead
  732. * \return true if the replacement is possible
  733. */
  734. DLLEXPORT bool isReplacementPossible(
  735. GPRegister oldReg, GPRegister newReg) const;
  736. /**
  737. * checks if a register can be replaced by another register.
  738. *
  739. * \param oldReg the register to replace
  740. * \param newReg the register to use instead
  741. * \return true if the replacement is possible
  742. */
  743. DLLEXPORT bool isReplacementPossible(
  744. FPRegister oldReg, FPRegister newReg) const;
  745. /**
  746. * replaces a register with another register.
  747. *
  748. * \param oldReg the register to replace
  749. * \param newReg the register to use instead
  750. */
  751. DLLEXPORT void replaceRegister(
  752. GPRegister oldReg, GPRegister newReg);
  753. /**
  754. * replaces a register with another register.
  755. *
  756. * \param oldReg the register to replace
  757. * \param newReg the register to use instead
  758. */
  759. DLLEXPORT void replaceRegister(
  760. FPRegister oldReg, FPRegister newReg);
  761. /**
  762. * adds a prefix to all jump labels in this block to avoid conflicts
  763. * if this block is inlined into another block.
  764. *
  765. * \param labelPrefix the label prefix to add
  766. */
  767. DLLEXPORT void addJumpLabelPrefix(Text labelPrefix);
  768. /**
  769. * \return the instructions of this block
  770. */
  771. DLLEXPORT const RCArray<Instruction>& getInstructions() const;
  772. /**
  773. * \return a pointer to a function that contains the compiled byte
  774. * code of this assembly block. and can be called directly with
  775. * reinterpret_cast<returnType(*)(parameterTypes...)>(compile())(parameters...)
  776. */
  777. DLLEXPORT void* compile();
  778. };
  779. } // namespace Assembly
  780. } // namespace Framework