aasmcpu.pas 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the abstract assembler implementation for the i386
  4. * Portions of this code was inspired by the NASM sources
  5. The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
  6. Julian Hall. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit aasmcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,verbose,
  25. cpubase,
  26. cgbase,cgutils,
  27. symtype,
  28. aasmbase,aasmtai,aasmdata,aasmsym,
  29. ogbase;
  30. const
  31. { "mov reg,reg" source operand number }
  32. O_MOV_SOURCE = 0;
  33. { "mov reg,reg" destination operand number }
  34. O_MOV_DEST = 1;
  35. { Operand types }
  36. OT_NONE = $00000000;
  37. { Bits 0..7: sizes }
  38. OT_BITS8 = $00000001;
  39. OT_BITS16 = $00000002;
  40. OT_BITS32 = $00000004;
  41. OT_BITS64 = $00000008; { x86_64 and FPU }
  42. OT_BITS80 = $00000010; { FPU only }
  43. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  44. OT_NEAR = $00000040;
  45. OT_SHORT = $00000080;
  46. { TODO: FAR/NEAR/SHORT are sizes too, they should be included into size mask,
  47. but this requires adjusting the opcode table }
  48. OT_SIZE_MASK = $0000001F; { all the size attributes }
  49. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  50. { Bits 8..11: modifiers }
  51. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  52. OT_TO = $00000200; { reverse effect in FADD, FSUB &c }
  53. OT_COLON = $00000400; { operand is followed by a colon }
  54. OT_MODIFIER_MASK = $00000F00;
  55. { Bits 12..15: type of operand }
  56. OT_REGISTER = $00001000;
  57. OT_IMMEDIATE = $00002000;
  58. OT_MEMORY = $0000C000; { always includes 'OT_REGMEM' bit as well }
  59. OT_REGMEM = $00008000; { for r/m, ie EA, operands }
  60. OT_TYPE_MASK = OT_REGISTER or OT_IMMEDIATE or OT_MEMORY or OT_REGMEM;
  61. OT_REGNORM = OT_REGISTER or OT_REGMEM; { 'normal' reg, qualifies as EA }
  62. { Bits 20..22, 24..26: register classes
  63. otf_* consts are not used alone, only to build other constants. }
  64. otf_reg_cdt = $00100000;
  65. otf_reg_gpr = $00200000;
  66. otf_reg_sreg = $00400000;
  67. otf_reg_fpu = $01000000;
  68. otf_reg_mmx = $02000000;
  69. otf_reg_xmm = $04000000;
  70. { Bits 16..19: subclasses, meaning depends on classes field }
  71. otf_sub0 = $00010000;
  72. otf_sub1 = $00020000;
  73. otf_sub2 = $00040000;
  74. otf_sub3 = $00080000;
  75. OT_REG_SMASK = otf_sub0 or otf_sub1 or otf_sub2 or otf_sub3;
  76. { register class 0: CRx, DRx and TRx }
  77. {$ifdef x86_64}
  78. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS64;
  79. {$else x86_64}
  80. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS32;
  81. {$endif x86_64}
  82. OT_REG_CREG = OT_REG_CDT or otf_sub0; { CRn }
  83. OT_REG_DREG = OT_REG_CDT or otf_sub1; { DRn }
  84. OT_REG_TREG = OT_REG_CDT or otf_sub2; { TRn }
  85. OT_REG_CR4 = OT_REG_CDT or otf_sub3; { CR4 (Pentium only) }
  86. { register class 1: general-purpose registers }
  87. OT_REG_GPR = OT_REGNORM or otf_reg_gpr;
  88. OT_RM_GPR = OT_REGMEM or otf_reg_gpr;
  89. OT_REG8 = OT_REG_GPR or OT_BITS8; { 8-bit GPR }
  90. OT_REG16 = OT_REG_GPR or OT_BITS16;
  91. OT_REG32 = OT_REG_GPR or OT_BITS32;
  92. OT_REG64 = OT_REG_GPR or OT_BITS64;
  93. { GPR subclass 0: accumulator: AL, AX, EAX or RAX }
  94. OT_REG_ACCUM = OT_REG_GPR or otf_sub0;
  95. OT_REG_AL = OT_REG_ACCUM or OT_BITS8;
  96. OT_REG_AX = OT_REG_ACCUM or OT_BITS16;
  97. OT_REG_EAX = OT_REG_ACCUM or OT_BITS32;
  98. {$ifdef x86_64}
  99. OT_REG_RAX = OT_REG_ACCUM or OT_BITS64;
  100. {$endif x86_64}
  101. { GPR subclass 1: counter: CL, CX, ECX or RCX }
  102. OT_REG_COUNT = OT_REG_GPR or otf_sub1;
  103. OT_REG_CL = OT_REG_COUNT or OT_BITS8;
  104. OT_REG_CX = OT_REG_COUNT or OT_BITS16;
  105. OT_REG_ECX = OT_REG_COUNT or OT_BITS32;
  106. {$ifdef x86_64}
  107. OT_REG_RCX = OT_REG_COUNT or OT_BITS64;
  108. {$endif x86_64}
  109. { GPR subclass 2: data register: DL, DX, EDX or RDX }
  110. OT_REG_DX = OT_REG_GPR or otf_sub2 or OT_BITS16;
  111. OT_REG_EDX = OT_REG_GPR or otf_sub2 or OT_BITS32;
  112. { register class 2: Segment registers }
  113. OT_REG_SREG = OT_REGISTER or otf_reg_sreg or OT_BITS16;
  114. OT_REG_CS = OT_REG_SREG or otf_sub0; { CS }
  115. OT_REG_DESS = OT_REG_SREG or otf_sub1; { DS, ES, SS (non-CS 86 registers) }
  116. OT_REG_FSGS = OT_REG_SREG or otf_sub2; { FS, GS (386 extended registers) }
  117. { register class 3: FPU registers }
  118. OT_FPUREG = OT_REGISTER or otf_reg_fpu;
  119. OT_FPU0 = OT_FPUREG or otf_sub0; { FPU stack register zero }
  120. { register class 4: MMX (both reg and r/m) }
  121. OT_MMXREG = OT_REGNORM or otf_reg_mmx;
  122. OT_MMXRM = OT_REGMEM or otf_reg_mmx;
  123. { register class 5: XMM (both reg and r/m) }
  124. OT_XMMREG = OT_REGNORM or otf_reg_xmm;
  125. OT_XMMRM = OT_REGMEM or otf_reg_xmm;
  126. { Memory operands }
  127. OT_MEM8 = OT_MEMORY or OT_BITS8;
  128. OT_MEM16 = OT_MEMORY or OT_BITS16;
  129. OT_MEM32 = OT_MEMORY or OT_BITS32;
  130. OT_MEM64 = OT_MEMORY or OT_BITS64;
  131. OT_MEM80 = OT_MEMORY or OT_BITS80;
  132. OT_MEM_OFFS = OT_MEMORY or otf_sub0; { special type of EA }
  133. { simple [address] offset }
  134. { Matches any type of r/m operand }
  135. OT_MEMORY_ANY = OT_MEMORY or OT_RM_GPR or OT_XMMRM or OT_MMXRM;
  136. { Immediate operands }
  137. OT_IMM8 = OT_IMMEDIATE or OT_BITS8;
  138. OT_IMM16 = OT_IMMEDIATE or OT_BITS16;
  139. OT_IMM32 = OT_IMMEDIATE or OT_BITS32;
  140. OT_IMM64 = OT_IMMEDIATE or OT_BITS64;
  141. OT_ONENESS = otf_sub0; { special type of immediate operand }
  142. OT_UNITY = OT_IMMEDIATE or OT_ONENESS; { for shift/rotate instructions }
  143. { Size of the instruction table converted by nasmconv.pas }
  144. {$ifdef x86_64}
  145. instabentries = {$i x8664nop.inc}
  146. {$else x86_64}
  147. instabentries = {$i i386nop.inc}
  148. {$endif x86_64}
  149. maxinfolen = 8;
  150. MaxInsChanges = 3; { Max things a instruction can change }
  151. type
  152. { What an instruction can change. Needed for optimizer and spilling code.
  153. Note: The order of this enumeration is should not be changed! }
  154. TInsChange = (Ch_None,
  155. {Read from a register}
  156. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  157. {write from a register}
  158. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  159. {read and write from/to a register}
  160. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  161. {modify the contents of a register with the purpose of using
  162. this changed content afterwards (add/sub/..., but e.g. not rep
  163. or movsd)}
  164. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  165. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  166. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  167. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  168. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  169. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  170. Ch_WMemEDI,
  171. Ch_All,
  172. { x86_64 registers }
  173. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  174. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  175. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  176. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  177. );
  178. TInsProp = packed record
  179. Ch : Array[1..MaxInsChanges] of TInsChange;
  180. end;
  181. const
  182. InsProp : array[tasmop] of TInsProp =
  183. {$ifdef x86_64}
  184. {$i x8664pro.inc}
  185. {$else x86_64}
  186. {$i i386prop.inc}
  187. {$endif x86_64}
  188. type
  189. TOperandOrder = (op_intel,op_att);
  190. tinsentry=packed record
  191. opcode : tasmop;
  192. ops : byte;
  193. optypes : array[0..max_operands-1] of longint;
  194. code : array[0..maxinfolen] of char;
  195. flags : cardinal;
  196. end;
  197. pinsentry=^tinsentry;
  198. { alignment for operator }
  199. tai_align = class(tai_align_abstract)
  200. reg : tregister;
  201. constructor create(b:byte);override;
  202. constructor create_op(b: byte; _op: byte);override;
  203. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;override;
  204. end;
  205. taicpu = class(tai_cpu_abstract_sym)
  206. opsize : topsize;
  207. constructor op_none(op : tasmop);
  208. constructor op_none(op : tasmop;_size : topsize);
  209. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  210. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  211. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  212. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  213. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  214. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  215. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  216. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  217. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  218. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  219. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  220. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  221. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  222. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  223. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  224. { this is for Jmp instructions }
  225. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  226. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  227. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  228. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  229. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  230. procedure changeopsize(siz:topsize);
  231. function GetString:string;
  232. procedure CheckNonCommutativeOpcodes;
  233. private
  234. FOperandOrder : TOperandOrder;
  235. procedure init(_size : topsize); { this need to be called by all constructor }
  236. public
  237. { the next will reset all instructions that can change in pass 2 }
  238. procedure ResetPass1;override;
  239. procedure ResetPass2;override;
  240. function CheckIfValid:boolean;
  241. function Pass1(objdata:TObjData):longint;override;
  242. procedure Pass2(objdata:TObjData);override;
  243. procedure SetOperandOrder(order:TOperandOrder);
  244. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  245. { register spilling code }
  246. function spilling_get_operation_type(opnr: longint): topertype;override;
  247. private
  248. { next fields are filled in pass1, so pass2 is faster }
  249. insentry : PInsEntry;
  250. insoffset : longint;
  251. LastInsOffset : longint; { need to be public to be reset }
  252. inssize : shortint;
  253. {$ifdef x86_64}
  254. rex : byte;
  255. {$endif x86_64}
  256. function InsEnd:longint;
  257. procedure create_ot(objdata:TObjData);
  258. function Matches(p:PInsEntry):boolean;
  259. function calcsize(p:PInsEntry):shortint;
  260. procedure gencode(objdata:TObjData);
  261. function NeedAddrPrefix(opidx:byte):boolean;
  262. procedure Swapoperands;
  263. function FindInsentry(objdata:TObjData):boolean;
  264. end;
  265. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  266. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  267. procedure InitAsm;
  268. procedure DoneAsm;
  269. implementation
  270. uses
  271. cutils,
  272. globals,
  273. systems,
  274. procinfo,
  275. itcpugas,
  276. symsym;
  277. {*****************************************************************************
  278. Instruction table
  279. *****************************************************************************}
  280. const
  281. {Instruction flags }
  282. IF_NONE = $00000000;
  283. IF_SM = $00000001; { size match first two operands }
  284. IF_SM2 = $00000002;
  285. IF_SB = $00000004; { unsized operands can't be non-byte }
  286. IF_SW = $00000008; { unsized operands can't be non-word }
  287. IF_SD = $00000010; { unsized operands can't be nondword }
  288. IF_SMASK = $0000001f;
  289. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  290. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  291. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  292. IF_ARMASK = $00000060; { mask for unsized argument spec }
  293. IF_ARSHIFT = 5; { LSB of IF_ARMASK }
  294. IF_PRIV = $00000100; { it's a privileged instruction }
  295. IF_SMM = $00000200; { it's only valid in SMM }
  296. IF_PROT = $00000400; { it's protected mode only }
  297. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  298. IF_UNDOC = $00001000; { it's an undocumented instruction }
  299. IF_FPU = $00002000; { it's an FPU instruction }
  300. IF_MMX = $00004000; { it's an MMX instruction }
  301. { it's a 3DNow! instruction }
  302. IF_3DNOW = $00008000;
  303. { it's a SSE (KNI, MMX2) instruction }
  304. IF_SSE = $00010000;
  305. { SSE2 instructions }
  306. IF_SSE2 = $00020000;
  307. { SSE3 instructions }
  308. IF_SSE3 = $00040000;
  309. { SSE64 instructions }
  310. IF_SSE64 = $00080000;
  311. { the mask for processor types }
  312. {IF_PMASK = longint($FF000000);}
  313. { the mask for disassembly "prefer" }
  314. {IF_PFMASK = longint($F001FF00);}
  315. { SVM instructions }
  316. IF_SVM = $00100000;
  317. { SSE4 instructions }
  318. IF_SSE4 = $00200000;
  319. { TODO: These flags were added to make x86ins.dat more readable.
  320. Values must be reassigned to make any other use of them. }
  321. IF_SSSE3 = $00200000;
  322. IF_SSE41 = $00200000;
  323. IF_SSE42 = $00200000;
  324. IF_8086 = $00000000; { 8086 instruction }
  325. IF_186 = $01000000; { 186+ instruction }
  326. IF_286 = $02000000; { 286+ instruction }
  327. IF_386 = $03000000; { 386+ instruction }
  328. IF_486 = $04000000; { 486+ instruction }
  329. IF_PENT = $05000000; { Pentium instruction }
  330. IF_P6 = $06000000; { P6 instruction }
  331. IF_KATMAI = $07000000; { Katmai instructions }
  332. { Willamette instructions }
  333. IF_WILLAMETTE = $08000000;
  334. { Prescott instructions }
  335. IF_PRESCOTT = $09000000;
  336. IF_X86_64 = $0a000000;
  337. IF_CYRIX = $0b000000; { Cyrix-specific instruction }
  338. IF_AMD = $0c000000; { AMD-specific instruction }
  339. IF_CENTAUR = $0d000000; { centaur-specific instruction }
  340. { added flags }
  341. IF_PRE = $40000000; { it's a prefix instruction }
  342. IF_PASS2 = $80000000; { if the instruction can change in a second pass }
  343. type
  344. TInsTabCache=array[TasmOp] of longint;
  345. PInsTabCache=^TInsTabCache;
  346. const
  347. {$ifdef x86_64}
  348. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  349. {$else x86_64}
  350. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  351. {$endif x86_64}
  352. var
  353. InsTabCache : PInsTabCache;
  354. const
  355. {$ifdef x86_64}
  356. { Intel style operands ! }
  357. opsize_2_type:array[0..2,topsize] of longint=(
  358. (OT_NONE,
  359. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  360. OT_BITS16,OT_BITS32,OT_BITS64,
  361. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  362. OT_BITS64,
  363. OT_NEAR,OT_FAR,OT_SHORT,
  364. OT_NONE,
  365. OT_NONE
  366. ),
  367. (OT_NONE,
  368. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  369. OT_BITS16,OT_BITS32,OT_BITS64,
  370. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  371. OT_BITS64,
  372. OT_NEAR,OT_FAR,OT_SHORT,
  373. OT_NONE,
  374. OT_NONE
  375. ),
  376. (OT_NONE,
  377. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  378. OT_BITS16,OT_BITS32,OT_BITS64,
  379. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  380. OT_BITS64,
  381. OT_NEAR,OT_FAR,OT_SHORT,
  382. OT_NONE,
  383. OT_NONE
  384. )
  385. );
  386. reg_ot_table : array[tregisterindex] of longint = (
  387. {$i r8664ot.inc}
  388. );
  389. {$else x86_64}
  390. { Intel style operands ! }
  391. opsize_2_type:array[0..2,topsize] of longint=(
  392. (OT_NONE,
  393. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  394. OT_BITS16,OT_BITS32,OT_BITS64,
  395. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  396. OT_BITS64,
  397. OT_NEAR,OT_FAR,OT_SHORT,
  398. OT_NONE,
  399. OT_NONE
  400. ),
  401. (OT_NONE,
  402. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  403. OT_BITS16,OT_BITS32,OT_BITS64,
  404. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  405. OT_BITS64,
  406. OT_NEAR,OT_FAR,OT_SHORT,
  407. OT_NONE,
  408. OT_NONE
  409. ),
  410. (OT_NONE,
  411. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  412. OT_BITS16,OT_BITS32,OT_BITS64,
  413. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  414. OT_BITS64,
  415. OT_NEAR,OT_FAR,OT_SHORT,
  416. OT_NONE,
  417. OT_NONE
  418. )
  419. );
  420. reg_ot_table : array[tregisterindex] of longint = (
  421. {$i r386ot.inc}
  422. );
  423. {$endif x86_64}
  424. { Operation type for spilling code }
  425. type
  426. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  427. var
  428. operation_type_table : ^toperation_type_table;
  429. {****************************************************************************
  430. TAI_ALIGN
  431. ****************************************************************************}
  432. constructor tai_align.create(b: byte);
  433. begin
  434. inherited create(b);
  435. reg:=NR_ECX;
  436. end;
  437. constructor tai_align.create_op(b: byte; _op: byte);
  438. begin
  439. inherited create_op(b,_op);
  440. reg:=NR_NO;
  441. end;
  442. function tai_align.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  443. const
  444. {$ifdef x86_64}
  445. alignarray:array[0..3] of string[4]=(
  446. #$66#$66#$66#$90,
  447. #$66#$66#$90,
  448. #$66#$90,
  449. #$90
  450. );
  451. {$else x86_64}
  452. alignarray:array[0..5] of string[8]=(
  453. #$8D#$B4#$26#$00#$00#$00#$00,
  454. #$8D#$B6#$00#$00#$00#$00,
  455. #$8D#$74#$26#$00,
  456. #$8D#$76#$00,
  457. #$89#$F6,
  458. #$90);
  459. {$endif x86_64}
  460. var
  461. bufptr : pchar;
  462. j : longint;
  463. localsize: byte;
  464. begin
  465. inherited calculatefillbuf(buf,executable);
  466. if not(use_op) and executable then
  467. begin
  468. bufptr:=pchar(@buf);
  469. { fillsize may still be used afterwards, so don't modify }
  470. { e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
  471. localsize:=fillsize;
  472. while (localsize>0) do
  473. begin
  474. for j:=low(alignarray) to high(alignarray) do
  475. if (localsize>=length(alignarray[j])) then
  476. break;
  477. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  478. inc(bufptr,length(alignarray[j]));
  479. dec(localsize,length(alignarray[j]));
  480. end;
  481. end;
  482. calculatefillbuf:=pchar(@buf);
  483. end;
  484. {*****************************************************************************
  485. Taicpu Constructors
  486. *****************************************************************************}
  487. procedure taicpu.changeopsize(siz:topsize);
  488. begin
  489. opsize:=siz;
  490. end;
  491. procedure taicpu.init(_size : topsize);
  492. begin
  493. { default order is att }
  494. FOperandOrder:=op_att;
  495. segprefix:=NR_NO;
  496. opsize:=_size;
  497. insentry:=nil;
  498. LastInsOffset:=-1;
  499. InsOffset:=0;
  500. InsSize:=0;
  501. end;
  502. constructor taicpu.op_none(op : tasmop);
  503. begin
  504. inherited create(op);
  505. init(S_NO);
  506. end;
  507. constructor taicpu.op_none(op : tasmop;_size : topsize);
  508. begin
  509. inherited create(op);
  510. init(_size);
  511. end;
  512. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  513. begin
  514. inherited create(op);
  515. init(_size);
  516. ops:=1;
  517. loadreg(0,_op1);
  518. end;
  519. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  520. begin
  521. inherited create(op);
  522. init(_size);
  523. ops:=1;
  524. loadconst(0,_op1);
  525. end;
  526. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  527. begin
  528. inherited create(op);
  529. init(_size);
  530. ops:=1;
  531. loadref(0,_op1);
  532. end;
  533. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  534. begin
  535. inherited create(op);
  536. init(_size);
  537. ops:=2;
  538. loadreg(0,_op1);
  539. loadreg(1,_op2);
  540. end;
  541. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  542. begin
  543. inherited create(op);
  544. init(_size);
  545. ops:=2;
  546. loadreg(0,_op1);
  547. loadconst(1,_op2);
  548. end;
  549. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  550. begin
  551. inherited create(op);
  552. init(_size);
  553. ops:=2;
  554. loadreg(0,_op1);
  555. loadref(1,_op2);
  556. end;
  557. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  558. begin
  559. inherited create(op);
  560. init(_size);
  561. ops:=2;
  562. loadconst(0,_op1);
  563. loadreg(1,_op2);
  564. end;
  565. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  566. begin
  567. inherited create(op);
  568. init(_size);
  569. ops:=2;
  570. loadconst(0,_op1);
  571. loadconst(1,_op2);
  572. end;
  573. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  574. begin
  575. inherited create(op);
  576. init(_size);
  577. ops:=2;
  578. loadconst(0,_op1);
  579. loadref(1,_op2);
  580. end;
  581. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  582. begin
  583. inherited create(op);
  584. init(_size);
  585. ops:=2;
  586. loadref(0,_op1);
  587. loadreg(1,_op2);
  588. end;
  589. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  590. begin
  591. inherited create(op);
  592. init(_size);
  593. ops:=3;
  594. loadreg(0,_op1);
  595. loadreg(1,_op2);
  596. loadreg(2,_op3);
  597. end;
  598. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  599. begin
  600. inherited create(op);
  601. init(_size);
  602. ops:=3;
  603. loadconst(0,_op1);
  604. loadreg(1,_op2);
  605. loadreg(2,_op3);
  606. end;
  607. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  608. begin
  609. inherited create(op);
  610. init(_size);
  611. ops:=3;
  612. loadreg(0,_op1);
  613. loadreg(1,_op2);
  614. loadref(2,_op3);
  615. end;
  616. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  617. begin
  618. inherited create(op);
  619. init(_size);
  620. ops:=3;
  621. loadconst(0,_op1);
  622. loadref(1,_op2);
  623. loadreg(2,_op3);
  624. end;
  625. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  626. begin
  627. inherited create(op);
  628. init(_size);
  629. ops:=3;
  630. loadconst(0,_op1);
  631. loadreg(1,_op2);
  632. loadref(2,_op3);
  633. end;
  634. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  635. begin
  636. inherited create(op);
  637. init(_size);
  638. condition:=cond;
  639. ops:=1;
  640. loadsymbol(0,_op1,0);
  641. end;
  642. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  643. begin
  644. inherited create(op);
  645. init(_size);
  646. ops:=1;
  647. loadsymbol(0,_op1,0);
  648. end;
  649. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  650. begin
  651. inherited create(op);
  652. init(_size);
  653. ops:=1;
  654. loadsymbol(0,_op1,_op1ofs);
  655. end;
  656. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  657. begin
  658. inherited create(op);
  659. init(_size);
  660. ops:=2;
  661. loadsymbol(0,_op1,_op1ofs);
  662. loadreg(1,_op2);
  663. end;
  664. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  665. begin
  666. inherited create(op);
  667. init(_size);
  668. ops:=2;
  669. loadsymbol(0,_op1,_op1ofs);
  670. loadref(1,_op2);
  671. end;
  672. function taicpu.GetString:string;
  673. var
  674. i : longint;
  675. s : string;
  676. addsize : boolean;
  677. begin
  678. s:='['+std_op2str[opcode];
  679. for i:=0 to ops-1 do
  680. begin
  681. with oper[i]^ do
  682. begin
  683. if i=0 then
  684. s:=s+' '
  685. else
  686. s:=s+',';
  687. { type }
  688. addsize:=false;
  689. if (ot and OT_XMMREG)=OT_XMMREG then
  690. s:=s+'xmmreg'
  691. else
  692. if (ot and OT_MMXREG)=OT_MMXREG then
  693. s:=s+'mmxreg'
  694. else
  695. if (ot and OT_FPUREG)=OT_FPUREG then
  696. s:=s+'fpureg'
  697. else
  698. if (ot and OT_REGISTER)=OT_REGISTER then
  699. begin
  700. s:=s+'reg';
  701. addsize:=true;
  702. end
  703. else
  704. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  705. begin
  706. s:=s+'imm';
  707. addsize:=true;
  708. end
  709. else
  710. if (ot and OT_MEMORY)=OT_MEMORY then
  711. begin
  712. s:=s+'mem';
  713. addsize:=true;
  714. end
  715. else
  716. s:=s+'???';
  717. { size }
  718. if addsize then
  719. begin
  720. if (ot and OT_BITS8)<>0 then
  721. s:=s+'8'
  722. else
  723. if (ot and OT_BITS16)<>0 then
  724. s:=s+'16'
  725. else
  726. if (ot and OT_BITS32)<>0 then
  727. s:=s+'32'
  728. else
  729. if (ot and OT_BITS64)<>0 then
  730. s:=s+'64'
  731. else
  732. s:=s+'??';
  733. { signed }
  734. if (ot and OT_SIGNED)<>0 then
  735. s:=s+'s';
  736. end;
  737. end;
  738. end;
  739. GetString:=s+']';
  740. end;
  741. procedure taicpu.Swapoperands;
  742. var
  743. p : POper;
  744. begin
  745. { Fix the operands which are in AT&T style and we need them in Intel style }
  746. case ops of
  747. 0,1:
  748. ;
  749. 2 : begin
  750. { 0,1 -> 1,0 }
  751. p:=oper[0];
  752. oper[0]:=oper[1];
  753. oper[1]:=p;
  754. end;
  755. 3 : begin
  756. { 0,1,2 -> 2,1,0 }
  757. p:=oper[0];
  758. oper[0]:=oper[2];
  759. oper[2]:=p;
  760. end;
  761. 4 : begin
  762. { 0,1,2,3 -> 3,2,1,0 }
  763. p:=oper[0];
  764. oper[0]:=oper[3];
  765. oper[3]:=p;
  766. p:=oper[1];
  767. oper[1]:=oper[2];
  768. oper[2]:=p;
  769. end;
  770. else
  771. internalerror(201108141);
  772. end;
  773. end;
  774. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  775. begin
  776. if FOperandOrder<>order then
  777. begin
  778. Swapoperands;
  779. FOperandOrder:=order;
  780. end;
  781. end;
  782. procedure taicpu.CheckNonCommutativeOpcodes;
  783. begin
  784. { we need ATT order }
  785. SetOperandOrder(op_att);
  786. if (
  787. (ops=2) and
  788. (oper[0]^.typ=top_reg) and
  789. (oper[1]^.typ=top_reg) and
  790. { if the first is ST and the second is also a register
  791. it is necessarily ST1 .. ST7 }
  792. ((oper[0]^.reg=NR_ST) or
  793. (oper[0]^.reg=NR_ST0))
  794. ) or
  795. { ((ops=1) and
  796. (oper[0]^.typ=top_reg) and
  797. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  798. (ops=0) then
  799. begin
  800. if opcode=A_FSUBR then
  801. opcode:=A_FSUB
  802. else if opcode=A_FSUB then
  803. opcode:=A_FSUBR
  804. else if opcode=A_FDIVR then
  805. opcode:=A_FDIV
  806. else if opcode=A_FDIV then
  807. opcode:=A_FDIVR
  808. else if opcode=A_FSUBRP then
  809. opcode:=A_FSUBP
  810. else if opcode=A_FSUBP then
  811. opcode:=A_FSUBRP
  812. else if opcode=A_FDIVRP then
  813. opcode:=A_FDIVP
  814. else if opcode=A_FDIVP then
  815. opcode:=A_FDIVRP;
  816. end;
  817. if (
  818. (ops=1) and
  819. (oper[0]^.typ=top_reg) and
  820. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  821. (oper[0]^.reg<>NR_ST)
  822. ) then
  823. begin
  824. if opcode=A_FSUBRP then
  825. opcode:=A_FSUBP
  826. else if opcode=A_FSUBP then
  827. opcode:=A_FSUBRP
  828. else if opcode=A_FDIVRP then
  829. opcode:=A_FDIVP
  830. else if opcode=A_FDIVP then
  831. opcode:=A_FDIVRP;
  832. end;
  833. end;
  834. {*****************************************************************************
  835. Assembler
  836. *****************************************************************************}
  837. type
  838. ea = packed record
  839. sib_present : boolean;
  840. bytes : byte;
  841. size : byte;
  842. modrm : byte;
  843. sib : byte;
  844. {$ifdef x86_64}
  845. rex : byte;
  846. {$endif x86_64}
  847. end;
  848. procedure taicpu.create_ot(objdata:TObjData);
  849. {
  850. this function will also fix some other fields which only needs to be once
  851. }
  852. var
  853. i,l,relsize : longint;
  854. currsym : TObjSymbol;
  855. begin
  856. if ops=0 then
  857. exit;
  858. { update oper[].ot field }
  859. for i:=0 to ops-1 do
  860. with oper[i]^ do
  861. begin
  862. case typ of
  863. top_reg :
  864. begin
  865. ot:=reg_ot_table[findreg_by_number(reg)];
  866. end;
  867. top_ref :
  868. begin
  869. if (ref^.refaddr=addr_no)
  870. {$ifdef i386}
  871. or (
  872. (ref^.refaddr in [addr_pic]) and
  873. { allow any base for assembler blocks }
  874. ((assigned(current_procinfo) and
  875. (pi_has_assembler_block in current_procinfo.flags) and
  876. (ref^.base<>NR_NO)) or (ref^.base=NR_EBX))
  877. )
  878. {$endif i386}
  879. {$ifdef x86_64}
  880. or (
  881. (ref^.refaddr in [addr_pic,addr_pic_no_got]) and
  882. (ref^.base<>NR_NO)
  883. )
  884. {$endif x86_64}
  885. then
  886. begin
  887. { create ot field }
  888. if (ot and OT_SIZE_MASK)=0 then
  889. ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
  890. else
  891. ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
  892. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  893. ot:=ot or OT_MEM_OFFS;
  894. { fix scalefactor }
  895. if (ref^.index=NR_NO) then
  896. ref^.scalefactor:=0
  897. else
  898. if (ref^.scalefactor=0) then
  899. ref^.scalefactor:=1;
  900. end
  901. else
  902. begin
  903. { Jumps use a relative offset which can be 8bit,
  904. for other opcodes we always need to generate the full
  905. 32bit address }
  906. if assigned(objdata) and
  907. is_jmp then
  908. begin
  909. currsym:=objdata.symbolref(ref^.symbol);
  910. l:=ref^.offset;
  911. if assigned(currsym) then
  912. inc(l,currsym.address);
  913. { when it is a forward jump we need to compensate the
  914. offset of the instruction since the previous time,
  915. because the symbol address is then still using the
  916. 'old-style' addressing.
  917. For backwards jumps this is not required because the
  918. address of the symbol is already adjusted to the
  919. new offset }
  920. if (l>InsOffset) and (LastInsOffset<>-1) then
  921. inc(l,InsOffset-LastInsOffset);
  922. { instruction size will then always become 2 (PFV) }
  923. relsize:=(InsOffset+2)-l;
  924. if (relsize>=-128) and (relsize<=127) and
  925. (
  926. not assigned(currsym) or
  927. (currsym.objsection=objdata.currobjsec)
  928. ) then
  929. ot:=OT_IMM8 or OT_SHORT
  930. else
  931. ot:=OT_IMM32 or OT_NEAR;
  932. end
  933. else
  934. ot:=OT_IMM32 or OT_NEAR;
  935. end;
  936. end;
  937. top_local :
  938. begin
  939. if (ot and OT_SIZE_MASK)=0 then
  940. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  941. else
  942. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  943. end;
  944. top_const :
  945. begin
  946. { allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
  947. { further, allow AAD and AAM with imm. operand }
  948. if (opsize=S_NO) and not((i in [1,2,3]) or ((i=0) and (opcode in [A_AAD,A_AAM]))) then
  949. message(asmr_e_invalid_opcode_and_operand);
  950. if (opsize<>S_W) and (aint(val)>=-128) and (val<=127) then
  951. ot:=OT_IMM8 or OT_SIGNED
  952. else
  953. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  954. if (val=1) and (i=1) then
  955. ot := ot or OT_ONENESS;
  956. end;
  957. top_none :
  958. begin
  959. { generated when there was an error in the
  960. assembler reader. It never happends when generating
  961. assembler }
  962. end;
  963. else
  964. internalerror(200402261);
  965. end;
  966. end;
  967. end;
  968. function taicpu.InsEnd:longint;
  969. begin
  970. InsEnd:=InsOffset+InsSize;
  971. end;
  972. function taicpu.Matches(p:PInsEntry):boolean;
  973. { * IF_SM stands for Size Match: any operand whose size is not
  974. * explicitly specified by the template is `really' intended to be
  975. * the same size as the first size-specified operand.
  976. * Non-specification is tolerated in the input instruction, but
  977. * _wrong_ specification is not.
  978. *
  979. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  980. * three-operand instructions such as SHLD: it implies that the
  981. * first two operands must match in size, but that the third is
  982. * required to be _unspecified_.
  983. *
  984. * IF_SB invokes Size Byte: operands with unspecified size in the
  985. * template are really bytes, and so no non-byte specification in
  986. * the input instruction will be tolerated. IF_SW similarly invokes
  987. * Size Word, and IF_SD invokes Size Doubleword.
  988. *
  989. * (The default state if neither IF_SM nor IF_SM2 is specified is
  990. * that any operand with unspecified size in the template is
  991. * required to have unspecified size in the instruction too...)
  992. }
  993. var
  994. insot,
  995. currot,
  996. i,j,asize,oprs : longint;
  997. insflags:cardinal;
  998. siz : array[0..max_operands-1] of longint;
  999. begin
  1000. result:=false;
  1001. { Check the opcode and operands }
  1002. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1003. exit;
  1004. for i:=0 to p^.ops-1 do
  1005. begin
  1006. insot:=p^.optypes[i];
  1007. currot:=oper[i]^.ot;
  1008. { Check the operand flags }
  1009. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1010. exit;
  1011. { Check if the passed operand size matches with one of
  1012. the supported operand sizes }
  1013. if ((insot and OT_SIZE_MASK)<>0) and
  1014. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1015. exit;
  1016. end;
  1017. { Check operand sizes }
  1018. insflags:=p^.flags;
  1019. if insflags and IF_SMASK<>0 then
  1020. begin
  1021. { as default an untyped size can get all the sizes, this is different
  1022. from nasm, but else we need to do a lot checking which opcodes want
  1023. size or not with the automatic size generation }
  1024. asize:=-1;
  1025. if (insflags and IF_SB)<>0 then
  1026. asize:=OT_BITS8
  1027. else if (insflags and IF_SW)<>0 then
  1028. asize:=OT_BITS16
  1029. else if (insflags and IF_SD)<>0 then
  1030. asize:=OT_BITS32;
  1031. if (insflags and IF_ARMASK)<>0 then
  1032. begin
  1033. siz[0]:=-1;
  1034. siz[1]:=-1;
  1035. siz[2]:=-1;
  1036. siz[((insflags and IF_ARMASK) shr IF_ARSHIFT)-1]:=asize;
  1037. end
  1038. else
  1039. begin
  1040. siz[0]:=asize;
  1041. siz[1]:=asize;
  1042. siz[2]:=asize;
  1043. end;
  1044. if (insflags and (IF_SM or IF_SM2))<>0 then
  1045. begin
  1046. if (insflags and IF_SM2)<>0 then
  1047. oprs:=2
  1048. else
  1049. oprs:=p^.ops;
  1050. for i:=0 to oprs-1 do
  1051. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1052. begin
  1053. for j:=0 to oprs-1 do
  1054. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1055. break;
  1056. end;
  1057. end
  1058. else
  1059. oprs:=2;
  1060. { Check operand sizes }
  1061. for i:=0 to p^.ops-1 do
  1062. begin
  1063. insot:=p^.optypes[i];
  1064. currot:=oper[i]^.ot;
  1065. if ((insot and OT_SIZE_MASK)=0) and
  1066. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1067. { Immediates can always include smaller size }
  1068. ((currot and OT_IMMEDIATE)=0) and
  1069. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1070. exit;
  1071. end;
  1072. end;
  1073. result:=true;
  1074. end;
  1075. procedure taicpu.ResetPass1;
  1076. begin
  1077. { we need to reset everything here, because the choosen insentry
  1078. can be invalid for a new situation where the previously optimized
  1079. insentry is not correct }
  1080. InsEntry:=nil;
  1081. InsSize:=0;
  1082. LastInsOffset:=-1;
  1083. end;
  1084. procedure taicpu.ResetPass2;
  1085. begin
  1086. { we are here in a second pass, check if the instruction can be optimized }
  1087. if assigned(InsEntry) and
  1088. ((InsEntry^.flags and IF_PASS2)<>0) then
  1089. begin
  1090. InsEntry:=nil;
  1091. InsSize:=0;
  1092. end;
  1093. LastInsOffset:=-1;
  1094. end;
  1095. function taicpu.CheckIfValid:boolean;
  1096. begin
  1097. result:=FindInsEntry(nil);
  1098. end;
  1099. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1100. var
  1101. i : longint;
  1102. begin
  1103. result:=false;
  1104. { Things which may only be done once, not when a second pass is done to
  1105. optimize }
  1106. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1107. begin
  1108. current_filepos:=fileinfo;
  1109. { We need intel style operands }
  1110. SetOperandOrder(op_intel);
  1111. { create the .ot fields }
  1112. create_ot(objdata);
  1113. { set the file postion }
  1114. end
  1115. else
  1116. begin
  1117. { we've already an insentry so it's valid }
  1118. result:=true;
  1119. exit;
  1120. end;
  1121. { Lookup opcode in the table }
  1122. InsSize:=-1;
  1123. i:=instabcache^[opcode];
  1124. if i=-1 then
  1125. begin
  1126. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1127. exit;
  1128. end;
  1129. insentry:=@instab[i];
  1130. while (insentry^.opcode=opcode) do
  1131. begin
  1132. if matches(insentry) then
  1133. begin
  1134. result:=true;
  1135. exit;
  1136. end;
  1137. inc(insentry);
  1138. end;
  1139. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1140. { No instruction found, set insentry to nil and inssize to -1 }
  1141. insentry:=nil;
  1142. inssize:=-1;
  1143. end;
  1144. function taicpu.Pass1(objdata:TObjData):longint;
  1145. begin
  1146. Pass1:=0;
  1147. { Save the old offset and set the new offset }
  1148. InsOffset:=ObjData.CurrObjSec.Size;
  1149. { Error? }
  1150. if (Insentry=nil) and (InsSize=-1) then
  1151. exit;
  1152. { set the file postion }
  1153. current_filepos:=fileinfo;
  1154. { Get InsEntry }
  1155. if FindInsEntry(ObjData) then
  1156. begin
  1157. { Calculate instruction size }
  1158. InsSize:=calcsize(insentry);
  1159. if segprefix<>NR_NO then
  1160. inc(InsSize);
  1161. { Fix opsize if size if forced }
  1162. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1163. begin
  1164. if (insentry^.flags and IF_ARMASK)=0 then
  1165. begin
  1166. if (insentry^.flags and IF_SB)<>0 then
  1167. begin
  1168. if opsize=S_NO then
  1169. opsize:=S_B;
  1170. end
  1171. else if (insentry^.flags and IF_SW)<>0 then
  1172. begin
  1173. if opsize=S_NO then
  1174. opsize:=S_W;
  1175. end
  1176. else if (insentry^.flags and IF_SD)<>0 then
  1177. begin
  1178. if opsize=S_NO then
  1179. opsize:=S_L;
  1180. end;
  1181. end;
  1182. end;
  1183. LastInsOffset:=InsOffset;
  1184. Pass1:=InsSize;
  1185. exit;
  1186. end;
  1187. LastInsOffset:=-1;
  1188. end;
  1189. const
  1190. segprefixes: array[NR_CS..NR_GS] of Byte=(
  1191. //cs ds es ss fs gs
  1192. $2E, $3E, $26, $36, $64, $65
  1193. );
  1194. procedure taicpu.Pass2(objdata:TObjData);
  1195. begin
  1196. { error in pass1 ? }
  1197. if insentry=nil then
  1198. exit;
  1199. current_filepos:=fileinfo;
  1200. { Segment override }
  1201. if (segprefix>=NR_CS) and (segprefix<=NR_GS) then
  1202. begin
  1203. objdata.writebytes(segprefixes[segprefix],1);
  1204. { fix the offset for GenNode }
  1205. inc(InsOffset);
  1206. end
  1207. else if segprefix<>NR_NO then
  1208. InternalError(201001071);
  1209. { Generate the instruction }
  1210. GenCode(objdata);
  1211. end;
  1212. function taicpu.needaddrprefix(opidx:byte):boolean;
  1213. begin
  1214. result:=(oper[opidx]^.typ=top_ref) and
  1215. (oper[opidx]^.ref^.refaddr=addr_no) and
  1216. {$ifdef x86_64}
  1217. (oper[opidx]^.ref^.base<>NR_RIP) and
  1218. {$endif x86_64}
  1219. (
  1220. (
  1221. (oper[opidx]^.ref^.index<>NR_NO) and
  1222. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1223. ) or
  1224. (
  1225. (oper[opidx]^.ref^.base<>NR_NO) and
  1226. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1227. )
  1228. );
  1229. end;
  1230. function regval(r:Tregister):byte;
  1231. const
  1232. {$ifdef x86_64}
  1233. opcode_table:array[tregisterindex] of tregisterindex = (
  1234. {$i r8664op.inc}
  1235. );
  1236. {$else x86_64}
  1237. opcode_table:array[tregisterindex] of tregisterindex = (
  1238. {$i r386op.inc}
  1239. );
  1240. {$endif x86_64}
  1241. var
  1242. regidx : tregisterindex;
  1243. begin
  1244. regidx:=findreg_by_number(r);
  1245. if regidx<>0 then
  1246. result:=opcode_table[regidx]
  1247. else
  1248. begin
  1249. Message1(asmw_e_invalid_register,generic_regname(r));
  1250. result:=0;
  1251. end;
  1252. end;
  1253. {$ifdef x86_64}
  1254. function rexbits(r: tregister): byte;
  1255. begin
  1256. result:=0;
  1257. case getregtype(r) of
  1258. R_INTREGISTER:
  1259. if (getsupreg(r)>=RS_R8) then
  1260. { Either B,X or R bits can be set, depending on register role in instruction.
  1261. Set all three bits here, caller will discard unnecessary ones. }
  1262. result:=result or $47
  1263. else if (getsubreg(r)=R_SUBL) and
  1264. (getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1265. result:=result or $40
  1266. else if (getsubreg(r)=R_SUBH) then
  1267. { Not an actual REX bit, used to detect incompatible usage of
  1268. AH/BH/CH/DH }
  1269. result:=result or $80;
  1270. R_MMREGISTER:
  1271. if getsupreg(r)>=RS_XMM8 then
  1272. result:=result or $47;
  1273. end;
  1274. end;
  1275. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1276. var
  1277. sym : tasmsymbol;
  1278. md,s,rv : byte;
  1279. base,index,scalefactor,
  1280. o : longint;
  1281. ir,br : Tregister;
  1282. isub,bsub : tsubregister;
  1283. begin
  1284. process_ea:=false;
  1285. fillchar(output,sizeof(output),0);
  1286. {Register ?}
  1287. if (input.typ=top_reg) then
  1288. begin
  1289. rv:=regval(input.reg);
  1290. output.modrm:=$c0 or (rfield shl 3) or rv;
  1291. output.size:=1;
  1292. output.rex:=output.rex or (rexbits(input.reg) and $F1);
  1293. process_ea:=true;
  1294. exit;
  1295. end;
  1296. {No register, so memory reference.}
  1297. if input.typ<>top_ref then
  1298. internalerror(200409263);
  1299. ir:=input.ref^.index;
  1300. br:=input.ref^.base;
  1301. isub:=getsubreg(ir);
  1302. bsub:=getsubreg(br);
  1303. s:=input.ref^.scalefactor;
  1304. o:=input.ref^.offset;
  1305. sym:=input.ref^.symbol;
  1306. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1307. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1308. internalerror(200301081);
  1309. { it's direct address }
  1310. if (br=NR_NO) and (ir=NR_NO) then
  1311. begin
  1312. output.sib_present:=true;
  1313. output.bytes:=4;
  1314. output.modrm:=4 or (rfield shl 3);
  1315. output.sib:=$25;
  1316. end
  1317. else if (br=NR_RIP) and (ir=NR_NO) then
  1318. begin
  1319. { rip based }
  1320. output.sib_present:=false;
  1321. output.bytes:=4;
  1322. output.modrm:=5 or (rfield shl 3);
  1323. end
  1324. else
  1325. { it's an indirection }
  1326. begin
  1327. { 16 bit or 32 bit address? }
  1328. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1329. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1330. message(asmw_e_16bit_32bit_not_supported);
  1331. { wrong, for various reasons }
  1332. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1333. exit;
  1334. output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
  1335. process_ea:=true;
  1336. { base }
  1337. case br of
  1338. NR_R8,
  1339. NR_RAX : base:=0;
  1340. NR_R9,
  1341. NR_RCX : base:=1;
  1342. NR_R10,
  1343. NR_RDX : base:=2;
  1344. NR_R11,
  1345. NR_RBX : base:=3;
  1346. NR_R12,
  1347. NR_RSP : base:=4;
  1348. NR_R13,
  1349. NR_NO,
  1350. NR_RBP : base:=5;
  1351. NR_R14,
  1352. NR_RSI : base:=6;
  1353. NR_R15,
  1354. NR_RDI : base:=7;
  1355. else
  1356. exit;
  1357. end;
  1358. { index }
  1359. case ir of
  1360. NR_R8,
  1361. NR_RAX : index:=0;
  1362. NR_R9,
  1363. NR_RCX : index:=1;
  1364. NR_R10,
  1365. NR_RDX : index:=2;
  1366. NR_R11,
  1367. NR_RBX : index:=3;
  1368. NR_R12,
  1369. NR_NO : index:=4;
  1370. NR_R13,
  1371. NR_RBP : index:=5;
  1372. NR_R14,
  1373. NR_RSI : index:=6;
  1374. NR_R15,
  1375. NR_RDI : index:=7;
  1376. else
  1377. exit;
  1378. end;
  1379. case s of
  1380. 0,
  1381. 1 : scalefactor:=0;
  1382. 2 : scalefactor:=1;
  1383. 4 : scalefactor:=2;
  1384. 8 : scalefactor:=3;
  1385. else
  1386. exit;
  1387. end;
  1388. { If rbp or r13 is used we must always include an offset }
  1389. if (br=NR_NO) or
  1390. ((br<>NR_RBP) and (br<>NR_R13) and (o=0) and (sym=nil)) then
  1391. md:=0
  1392. else
  1393. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1394. md:=1
  1395. else
  1396. md:=2;
  1397. if (br=NR_NO) or (md=2) then
  1398. output.bytes:=4
  1399. else
  1400. output.bytes:=md;
  1401. { SIB needed ? }
  1402. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) then
  1403. begin
  1404. output.sib_present:=false;
  1405. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1406. end
  1407. else
  1408. begin
  1409. output.sib_present:=true;
  1410. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1411. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1412. end;
  1413. end;
  1414. output.size:=1+ord(output.sib_present)+output.bytes;
  1415. process_ea:=true;
  1416. end;
  1417. {$else x86_64}
  1418. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1419. var
  1420. sym : tasmsymbol;
  1421. md,s,rv : byte;
  1422. base,index,scalefactor,
  1423. o : longint;
  1424. ir,br : Tregister;
  1425. isub,bsub : tsubregister;
  1426. begin
  1427. process_ea:=false;
  1428. fillchar(output,sizeof(output),0);
  1429. {Register ?}
  1430. if (input.typ=top_reg) then
  1431. begin
  1432. rv:=regval(input.reg);
  1433. output.modrm:=$c0 or (rfield shl 3) or rv;
  1434. output.size:=1;
  1435. process_ea:=true;
  1436. exit;
  1437. end;
  1438. {No register, so memory reference.}
  1439. if (input.typ<>top_ref) then
  1440. internalerror(200409262);
  1441. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1442. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1443. internalerror(200301081);
  1444. ir:=input.ref^.index;
  1445. br:=input.ref^.base;
  1446. isub:=getsubreg(ir);
  1447. bsub:=getsubreg(br);
  1448. s:=input.ref^.scalefactor;
  1449. o:=input.ref^.offset;
  1450. sym:=input.ref^.symbol;
  1451. { it's direct address }
  1452. if (br=NR_NO) and (ir=NR_NO) then
  1453. begin
  1454. { it's a pure offset }
  1455. output.sib_present:=false;
  1456. output.bytes:=4;
  1457. output.modrm:=5 or (rfield shl 3);
  1458. end
  1459. else
  1460. { it's an indirection }
  1461. begin
  1462. { 16 bit address? }
  1463. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1464. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1465. message(asmw_e_16bit_not_supported);
  1466. {$ifdef OPTEA}
  1467. { make single reg base }
  1468. if (br=NR_NO) and (s=1) then
  1469. begin
  1470. br:=ir;
  1471. ir:=NR_NO;
  1472. end;
  1473. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1474. if (br=NR_NO) and
  1475. (((s=2) and (ir<>NR_ESP)) or
  1476. (s=3) or (s=5) or (s=9)) then
  1477. begin
  1478. br:=ir;
  1479. dec(s);
  1480. end;
  1481. { swap ESP into base if scalefactor is 1 }
  1482. if (s=1) and (ir=NR_ESP) then
  1483. begin
  1484. ir:=br;
  1485. br:=NR_ESP;
  1486. end;
  1487. {$endif OPTEA}
  1488. { wrong, for various reasons }
  1489. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1490. exit;
  1491. { base }
  1492. case br of
  1493. NR_EAX : base:=0;
  1494. NR_ECX : base:=1;
  1495. NR_EDX : base:=2;
  1496. NR_EBX : base:=3;
  1497. NR_ESP : base:=4;
  1498. NR_NO,
  1499. NR_EBP : base:=5;
  1500. NR_ESI : base:=6;
  1501. NR_EDI : base:=7;
  1502. else
  1503. exit;
  1504. end;
  1505. { index }
  1506. case ir of
  1507. NR_EAX : index:=0;
  1508. NR_ECX : index:=1;
  1509. NR_EDX : index:=2;
  1510. NR_EBX : index:=3;
  1511. NR_NO : index:=4;
  1512. NR_EBP : index:=5;
  1513. NR_ESI : index:=6;
  1514. NR_EDI : index:=7;
  1515. else
  1516. exit;
  1517. end;
  1518. case s of
  1519. 0,
  1520. 1 : scalefactor:=0;
  1521. 2 : scalefactor:=1;
  1522. 4 : scalefactor:=2;
  1523. 8 : scalefactor:=3;
  1524. else
  1525. exit;
  1526. end;
  1527. if (br=NR_NO) or
  1528. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1529. md:=0
  1530. else
  1531. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1532. md:=1
  1533. else
  1534. md:=2;
  1535. if (br=NR_NO) or (md=2) then
  1536. output.bytes:=4
  1537. else
  1538. output.bytes:=md;
  1539. { SIB needed ? }
  1540. if (ir=NR_NO) and (br<>NR_ESP) then
  1541. begin
  1542. output.sib_present:=false;
  1543. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  1544. end
  1545. else
  1546. begin
  1547. output.sib_present:=true;
  1548. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  1549. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1550. end;
  1551. end;
  1552. if output.sib_present then
  1553. output.size:=2+output.bytes
  1554. else
  1555. output.size:=1+output.bytes;
  1556. process_ea:=true;
  1557. end;
  1558. {$endif x86_64}
  1559. function taicpu.calcsize(p:PInsEntry):shortint;
  1560. var
  1561. codes : pchar;
  1562. c : byte;
  1563. len : shortint;
  1564. ea_data : ea;
  1565. {$ifdef x86_64}
  1566. omit_rexw : boolean;
  1567. {$endif x86_64}
  1568. begin
  1569. len:=0;
  1570. codes:=@p^.code[0];
  1571. {$ifdef x86_64}
  1572. rex:=0;
  1573. omit_rexw:=false;
  1574. {$endif x86_64}
  1575. repeat
  1576. c:=ord(codes^);
  1577. inc(codes);
  1578. case c of
  1579. 0 :
  1580. break;
  1581. 1,2,3 :
  1582. begin
  1583. inc(codes,c);
  1584. inc(len,c);
  1585. end;
  1586. 8,9,10 :
  1587. begin
  1588. {$ifdef x86_64}
  1589. rex:=rex or (rexbits(oper[c-8]^.reg) and $F1);
  1590. {$endif x86_64}
  1591. inc(codes);
  1592. inc(len);
  1593. end;
  1594. 11 :
  1595. begin
  1596. inc(codes);
  1597. inc(len);
  1598. end;
  1599. 4,5,6,7 :
  1600. begin
  1601. if opsize=S_W then
  1602. inc(len,2)
  1603. else
  1604. inc(len);
  1605. end;
  1606. 12,13,14,
  1607. 16,17,18,
  1608. 20,21,22,23,
  1609. 40,41,42 :
  1610. inc(len);
  1611. 24,25,26,
  1612. 31,
  1613. 48,49,50 :
  1614. inc(len,2);
  1615. 28,29,30:
  1616. begin
  1617. if opsize=S_Q then
  1618. inc(len,8)
  1619. else
  1620. inc(len,4);
  1621. end;
  1622. 36,37,38:
  1623. inc(len,sizeof(pint));
  1624. 44,45,46:
  1625. inc(len,8);
  1626. 32,33,34,
  1627. 52,53,54,
  1628. 56,57,58,
  1629. 172,173,174 :
  1630. inc(len,4);
  1631. 208,209,210 :
  1632. begin
  1633. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1634. OT_BITS16:
  1635. inc(len);
  1636. {$ifdef x86_64}
  1637. OT_BITS64:
  1638. begin
  1639. rex:=rex or $48;
  1640. end;
  1641. {$endif x86_64}
  1642. end;
  1643. end;
  1644. 200 :
  1645. {$ifndef x86_64}
  1646. inc(len);
  1647. {$else x86_64}
  1648. { every insentry with code 0310 must be marked with NOX86_64 }
  1649. InternalError(2011051301);
  1650. {$endif x86_64}
  1651. 201 :
  1652. {$ifdef x86_64}
  1653. inc(len)
  1654. {$endif x86_64}
  1655. ;
  1656. 212 :
  1657. inc(len);
  1658. 214 :
  1659. begin
  1660. {$ifdef x86_64}
  1661. rex:=rex or $48;
  1662. {$endif x86_64}
  1663. end;
  1664. 202,
  1665. 211,
  1666. 213,
  1667. 215,
  1668. 217,218: ;
  1669. 219,220,241 :
  1670. inc(len);
  1671. 221:
  1672. {$ifdef x86_64}
  1673. omit_rexw:=true
  1674. {$endif x86_64}
  1675. ;
  1676. 64..151 :
  1677. begin
  1678. {$ifdef x86_64}
  1679. if (c<127) then
  1680. begin
  1681. if (oper[c and 7]^.typ=top_reg) then
  1682. begin
  1683. rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
  1684. end;
  1685. end;
  1686. {$endif x86_64}
  1687. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1688. Message(asmw_e_invalid_effective_address)
  1689. else
  1690. inc(len,ea_data.size);
  1691. {$ifdef x86_64}
  1692. rex:=rex or ea_data.rex;
  1693. {$endif x86_64}
  1694. end;
  1695. else
  1696. InternalError(200603141);
  1697. end;
  1698. until false;
  1699. {$ifdef x86_64}
  1700. if ((rex and $80)<>0) and ((rex and $4F)<>0) then
  1701. Message(asmw_e_bad_reg_with_rex);
  1702. rex:=rex and $4F; { reset extra bits in upper nibble }
  1703. if omit_rexw then
  1704. begin
  1705. if rex=$48 then { remove rex entirely? }
  1706. rex:=0
  1707. else
  1708. rex:=rex and $F7;
  1709. end;
  1710. if rex<>0 then
  1711. Inc(len);
  1712. {$endif}
  1713. calcsize:=len;
  1714. end;
  1715. procedure taicpu.GenCode(objdata:TObjData);
  1716. {
  1717. * the actual codes (C syntax, i.e. octal):
  1718. * \0 - terminates the code. (Unless it's a literal of course.)
  1719. * \1, \2, \3 - that many literal bytes follow in the code stream
  1720. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1721. * (POP is never used for CS) depending on operand 0
  1722. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1723. * on operand 0
  1724. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1725. * to the register value of operand 0, 1 or 2
  1726. * \13 - a literal byte follows in the code stream, to be added
  1727. * to the condition code value of the instruction.
  1728. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1729. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1730. * \24, \25, \26, \27 - an unsigned byte immediate operand, from operand 0, 1, 2 or 3
  1731. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1732. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1733. * assembly mode or the address-size override on the operand
  1734. * \37 - a word constant, from the _segment_ part of operand 0
  1735. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1736. * \44, \45, \46 - select between \3[012], \4[012] or \5[456] depending
  1737. on the address size of instruction
  1738. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1739. * \54, \55, \56 - a qword immediate, from operand 0, 1 or 2
  1740. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1741. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1742. * assembly mode or the address-size override on the operand
  1743. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1744. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1745. * field the register value of operand b.
  1746. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1747. * field equal to digit b.
  1748. * \254,\255,\256 - a signed 32-bit immediate to be extended to 64 bits
  1749. * \300,\301,\302 - might be an 0x67, depending on the address size of
  1750. * the memory reference in operand x.
  1751. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1752. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1753. * \312 - (disassembler only) invalid with non-default address size.
  1754. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  1755. * size of operand x.
  1756. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1757. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1758. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1759. * \327 - indicates that this instruction is only valid when the
  1760. * operand size is the default (instruction to disassembler,
  1761. * generates no code in the assembler)
  1762. * \331 - instruction not valid with REP prefix. Hint for
  1763. * disassembler only; for SSE instructions.
  1764. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  1765. * \333 - 0xF3 prefix for SSE instructions
  1766. * \334 - 0xF2 prefix for SSE instructions
  1767. * \335 - Indicates 64-bit operand size with REX.W not necessary
  1768. * \361 - 0x66 prefix for SSE instructions
  1769. }
  1770. var
  1771. currval : aint;
  1772. currsym : tobjsymbol;
  1773. currrelreloc,
  1774. currabsreloc,
  1775. currabsreloc32 : TObjRelocationType;
  1776. {$ifdef x86_64}
  1777. rexwritten : boolean;
  1778. {$endif x86_64}
  1779. procedure getvalsym(opidx:longint);
  1780. begin
  1781. case oper[opidx]^.typ of
  1782. top_ref :
  1783. begin
  1784. currval:=oper[opidx]^.ref^.offset;
  1785. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  1786. {$ifdef i386}
  1787. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  1788. (tf_pic_uses_got in target_info.flags) then
  1789. begin
  1790. currrelreloc:=RELOC_PLT32;
  1791. currabsreloc:=RELOC_GOT32;
  1792. currabsreloc32:=RELOC_GOT32;
  1793. end
  1794. else
  1795. {$endif i386}
  1796. {$ifdef x86_64}
  1797. if oper[opidx]^.ref^.refaddr=addr_pic then
  1798. begin
  1799. currrelreloc:=RELOC_PLT32;
  1800. currabsreloc:=RELOC_GOTPCREL;
  1801. currabsreloc32:=RELOC_GOTPCREL;
  1802. end
  1803. else if oper[opidx]^.ref^.refaddr=addr_pic_no_got then
  1804. begin
  1805. currrelreloc:=RELOC_RELATIVE;
  1806. currabsreloc:=RELOC_RELATIVE;
  1807. currabsreloc32:=RELOC_RELATIVE;
  1808. end
  1809. else
  1810. {$endif x86_64}
  1811. begin
  1812. currrelreloc:=RELOC_RELATIVE;
  1813. currabsreloc:=RELOC_ABSOLUTE;
  1814. currabsreloc32:=RELOC_ABSOLUTE32;
  1815. end;
  1816. end;
  1817. top_const :
  1818. begin
  1819. currval:=aint(oper[opidx]^.val);
  1820. currsym:=nil;
  1821. currabsreloc:=RELOC_ABSOLUTE;
  1822. currabsreloc32:=RELOC_ABSOLUTE32;
  1823. end;
  1824. else
  1825. Message(asmw_e_immediate_or_reference_expected);
  1826. end;
  1827. end;
  1828. {$ifdef x86_64}
  1829. procedure maybewriterex;
  1830. begin
  1831. if (rex<>0) and not(rexwritten) then
  1832. begin
  1833. rexwritten:=true;
  1834. objdata.writebytes(rex,1);
  1835. end;
  1836. end;
  1837. {$endif x86_64}
  1838. procedure objdata_writereloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  1839. begin
  1840. {$ifdef i386}
  1841. { Special case of '_GLOBAL_OFFSET_TABLE_'
  1842. which needs a special relocation type R_386_GOTPC }
  1843. if assigned (p) and
  1844. (p.name='_GLOBAL_OFFSET_TABLE_') and
  1845. (tf_pic_uses_got in target_info.flags) then
  1846. begin
  1847. { nothing else than a 4 byte relocation should occur
  1848. for GOT }
  1849. if len<>4 then
  1850. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1851. Reloctype:=RELOC_GOTPC;
  1852. { We need to add the offset of the relocation
  1853. of _GLOBAL_OFFSET_TABLE symbol within
  1854. the current instruction }
  1855. inc(data,objdata.currobjsec.size-insoffset);
  1856. end;
  1857. {$endif i386}
  1858. objdata.writereloc(data,len,p,Reloctype);
  1859. end;
  1860. const
  1861. CondVal:array[TAsmCond] of byte=($0,
  1862. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1863. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1864. $0, $A, $A, $B, $8, $4);
  1865. var
  1866. c : byte;
  1867. pb : pbyte;
  1868. codes : pchar;
  1869. bytes : array[0..3] of byte;
  1870. rfield,
  1871. data,s,opidx : longint;
  1872. ea_data : ea;
  1873. relsym : TObjSymbol;
  1874. begin
  1875. { safety check }
  1876. if objdata.currobjsec.size<>longword(insoffset) then
  1877. internalerror(200130121);
  1878. { load data to write }
  1879. codes:=insentry^.code;
  1880. {$ifdef x86_64}
  1881. rexwritten:=false;
  1882. {$endif x86_64}
  1883. { Force word push/pop for registers }
  1884. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1885. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1886. begin
  1887. bytes[0]:=$66;
  1888. objdata.writebytes(bytes,1);
  1889. end;
  1890. repeat
  1891. c:=ord(codes^);
  1892. inc(codes);
  1893. case c of
  1894. 0 :
  1895. break;
  1896. 1,2,3 :
  1897. begin
  1898. {$ifdef x86_64}
  1899. maybewriterex;
  1900. {$endif x86_64}
  1901. objdata.writebytes(codes^,c);
  1902. inc(codes,c);
  1903. end;
  1904. 4,6 :
  1905. begin
  1906. case oper[0]^.reg of
  1907. NR_CS:
  1908. bytes[0]:=$e;
  1909. NR_NO,
  1910. NR_DS:
  1911. bytes[0]:=$1e;
  1912. NR_ES:
  1913. bytes[0]:=$6;
  1914. NR_SS:
  1915. bytes[0]:=$16;
  1916. else
  1917. internalerror(777004);
  1918. end;
  1919. if c=4 then
  1920. inc(bytes[0]);
  1921. objdata.writebytes(bytes,1);
  1922. end;
  1923. 5,7 :
  1924. begin
  1925. case oper[0]^.reg of
  1926. NR_FS:
  1927. bytes[0]:=$a0;
  1928. NR_GS:
  1929. bytes[0]:=$a8;
  1930. else
  1931. internalerror(777005);
  1932. end;
  1933. if c=5 then
  1934. inc(bytes[0]);
  1935. objdata.writebytes(bytes,1);
  1936. end;
  1937. 8,9,10 :
  1938. begin
  1939. {$ifdef x86_64}
  1940. maybewriterex;
  1941. {$endif x86_64}
  1942. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1943. inc(codes);
  1944. objdata.writebytes(bytes,1);
  1945. end;
  1946. 11 :
  1947. begin
  1948. bytes[0]:=ord(codes^)+condval[condition];
  1949. inc(codes);
  1950. objdata.writebytes(bytes,1);
  1951. end;
  1952. 12,13,14 :
  1953. begin
  1954. getvalsym(c-12);
  1955. if (currval<-128) or (currval>127) then
  1956. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1957. if assigned(currsym) then
  1958. objdata_writereloc(currval,1,currsym,currabsreloc)
  1959. else
  1960. objdata.writebytes(currval,1);
  1961. end;
  1962. 16,17,18 :
  1963. begin
  1964. getvalsym(c-16);
  1965. if (currval<-256) or (currval>255) then
  1966. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1967. if assigned(currsym) then
  1968. objdata_writereloc(currval,1,currsym,currabsreloc)
  1969. else
  1970. objdata.writebytes(currval,1);
  1971. end;
  1972. 20,21,22,23 :
  1973. begin
  1974. getvalsym(c-20);
  1975. if (currval<0) or (currval>255) then
  1976. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1977. if assigned(currsym) then
  1978. objdata_writereloc(currval,1,currsym,currabsreloc)
  1979. else
  1980. objdata.writebytes(currval,1);
  1981. end;
  1982. 24,25,26 : // 030..032
  1983. begin
  1984. getvalsym(c-24);
  1985. if (currval<-65536) or (currval>65535) then
  1986. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1987. if assigned(currsym) then
  1988. objdata_writereloc(currval,2,currsym,currabsreloc)
  1989. else
  1990. objdata.writebytes(currval,2);
  1991. end;
  1992. 28,29,30 : // 034..036
  1993. { !!! These are intended (and used in opcode table) to select depending
  1994. on address size, *not* operand size. Works by coincidence only. }
  1995. begin
  1996. getvalsym(c-28);
  1997. if opsize=S_Q then
  1998. begin
  1999. if assigned(currsym) then
  2000. objdata_writereloc(currval,8,currsym,currabsreloc)
  2001. else
  2002. objdata.writebytes(currval,8);
  2003. end
  2004. else
  2005. begin
  2006. if assigned(currsym) then
  2007. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2008. else
  2009. objdata.writebytes(currval,4);
  2010. end
  2011. end;
  2012. 32,33,34 : // 040..042
  2013. begin
  2014. getvalsym(c-32);
  2015. if assigned(currsym) then
  2016. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2017. else
  2018. objdata.writebytes(currval,4);
  2019. end;
  2020. 36,37,38 : // 044..046 - select between word/dword/qword depending on
  2021. begin // address size (we support only default address sizes).
  2022. getvalsym(c-36);
  2023. {$ifdef x86_64}
  2024. if assigned(currsym) then
  2025. objdata_writereloc(currval,8,currsym,currabsreloc)
  2026. else
  2027. objdata.writebytes(currval,8);
  2028. {$else x86_64}
  2029. if assigned(currsym) then
  2030. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2031. else
  2032. objdata.writebytes(currval,4);
  2033. {$endif x86_64}
  2034. end;
  2035. 40,41,42 : // 050..052 - byte relative operand
  2036. begin
  2037. getvalsym(c-40);
  2038. data:=currval-insend;
  2039. if assigned(currsym) then
  2040. inc(data,currsym.address);
  2041. if (data>127) or (data<-128) then
  2042. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  2043. objdata.writebytes(data,1);
  2044. end;
  2045. 44,45,46: // 054..056 - qword immediate operand
  2046. begin
  2047. getvalsym(c-44);
  2048. if assigned(currsym) then
  2049. objdata_writereloc(currval,8,currsym,currabsreloc)
  2050. else
  2051. objdata.writebytes(currval,8);
  2052. end;
  2053. 52,53,54 : // 064..066 - select between 16/32 address mode, but we support only 32
  2054. begin
  2055. getvalsym(c-52);
  2056. if assigned(currsym) then
  2057. objdata_writereloc(currval,4,currsym,currrelreloc)
  2058. else
  2059. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2060. end;
  2061. 56,57,58 : // 070..072 - long relative operand
  2062. begin
  2063. getvalsym(c-56);
  2064. if assigned(currsym) then
  2065. objdata_writereloc(currval,4,currsym,currrelreloc)
  2066. else
  2067. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2068. end;
  2069. 172,173,174 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
  2070. begin
  2071. getvalsym(c-172);
  2072. {$ifdef x86_64}
  2073. { for i386 as aint type is longint the
  2074. following test is useless }
  2075. if (currval<low(longint)) or (currval>high(longint)) then
  2076. Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
  2077. {$endif x86_64}
  2078. if assigned(currsym) then
  2079. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2080. else
  2081. objdata.writebytes(currval,4);
  2082. end;
  2083. 200 : { fixed 16-bit addr }
  2084. {$ifndef x86_64}
  2085. begin
  2086. bytes[0]:=$67;
  2087. objdata.writebytes(bytes,1);
  2088. end;
  2089. {$else x86_64}
  2090. { every insentry having code 0310 must be marked with NOX86_64 }
  2091. InternalError(2011051302);
  2092. {$endif}
  2093. 201 : { fixed 32-bit addr }
  2094. {$ifdef x86_64}
  2095. begin
  2096. bytes[0]:=$67;
  2097. objdata.writebytes(bytes,1);
  2098. end
  2099. {$endif x86_64}
  2100. ;
  2101. 208,209,210 :
  2102. begin
  2103. case oper[c-208]^.ot and OT_SIZE_MASK of
  2104. OT_BITS16 :
  2105. begin
  2106. bytes[0]:=$66;
  2107. objdata.writebytes(bytes,1);
  2108. end;
  2109. {$ifndef x86_64}
  2110. OT_BITS64 :
  2111. Message(asmw_e_64bit_not_supported);
  2112. {$endif x86_64}
  2113. end;
  2114. end;
  2115. 211,
  2116. 213 : {no action needed};
  2117. 212, 241 :
  2118. begin
  2119. bytes[0]:=$66;
  2120. objdata.writebytes(bytes,1);
  2121. end;
  2122. 214 :
  2123. begin
  2124. {$ifndef x86_64}
  2125. Message(asmw_e_64bit_not_supported);
  2126. {$endif x86_64}
  2127. end;
  2128. 219 :
  2129. begin
  2130. bytes[0]:=$f3;
  2131. objdata.writebytes(bytes,1);
  2132. end;
  2133. 220 :
  2134. begin
  2135. bytes[0]:=$f2;
  2136. objdata.writebytes(bytes,1);
  2137. end;
  2138. 221:
  2139. ;
  2140. 202,
  2141. 215,
  2142. 217,218 :
  2143. begin
  2144. { these are dissambler hints or 32 bit prefixes which
  2145. are not needed }
  2146. end;
  2147. 31,
  2148. 48,49,50 :
  2149. begin
  2150. InternalError(777006);
  2151. end
  2152. else
  2153. begin
  2154. { rex should be written at this point }
  2155. {$ifdef x86_64}
  2156. if (rex<>0) and not(rexwritten) then
  2157. internalerror(200603191);
  2158. {$endif x86_64}
  2159. if (c>=64) and (c<=151) then // 0100..0227
  2160. begin
  2161. if (c<127) then // 0177
  2162. begin
  2163. if (oper[c and 7]^.typ=top_reg) then
  2164. rfield:=regval(oper[c and 7]^.reg)
  2165. else
  2166. rfield:=regval(oper[c and 7]^.ref^.base);
  2167. end
  2168. else
  2169. rfield:=c and 7;
  2170. opidx:=(c shr 3) and 7;
  2171. if not process_ea(oper[opidx]^,ea_data,rfield) then
  2172. Message(asmw_e_invalid_effective_address);
  2173. pb:=@bytes[0];
  2174. pb^:=ea_data.modrm;
  2175. inc(pb);
  2176. if ea_data.sib_present then
  2177. begin
  2178. pb^:=ea_data.sib;
  2179. inc(pb);
  2180. end;
  2181. s:=pb-@bytes[0];
  2182. objdata.writebytes(bytes,s);
  2183. case ea_data.bytes of
  2184. 0 : ;
  2185. 1 :
  2186. begin
  2187. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  2188. begin
  2189. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2190. {$ifdef i386}
  2191. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2192. (tf_pic_uses_got in target_info.flags) then
  2193. currabsreloc:=RELOC_GOT32
  2194. else
  2195. {$endif i386}
  2196. {$ifdef x86_64}
  2197. if oper[opidx]^.ref^.refaddr=addr_pic then
  2198. currabsreloc:=RELOC_GOTPCREL
  2199. else
  2200. {$endif x86_64}
  2201. currabsreloc:=RELOC_ABSOLUTE;
  2202. objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  2203. end
  2204. else
  2205. begin
  2206. bytes[0]:=oper[opidx]^.ref^.offset;
  2207. objdata.writebytes(bytes,1);
  2208. end;
  2209. inc(s);
  2210. end;
  2211. 2,4 :
  2212. begin
  2213. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2214. currval:=oper[opidx]^.ref^.offset;
  2215. {$ifdef x86_64}
  2216. if oper[opidx]^.ref^.refaddr=addr_pic then
  2217. currabsreloc:=RELOC_GOTPCREL
  2218. else
  2219. if oper[opidx]^.ref^.base=NR_RIP then
  2220. begin
  2221. currabsreloc:=RELOC_RELATIVE;
  2222. { Adjust reloc value by number of bytes following the displacement,
  2223. but not if displacement is specified by literal constant }
  2224. if Assigned(currsym) then
  2225. Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
  2226. end
  2227. else
  2228. {$endif x86_64}
  2229. {$ifdef i386}
  2230. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2231. (tf_pic_uses_got in target_info.flags) then
  2232. currabsreloc:=RELOC_GOT32
  2233. else
  2234. {$endif i386}
  2235. currabsreloc:=RELOC_ABSOLUTE32;
  2236. if (currabsreloc=RELOC_ABSOLUTE32) and
  2237. (Assigned(oper[opidx]^.ref^.relsymbol)) then
  2238. begin
  2239. relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
  2240. currabsreloc:=RELOC_PIC_PAIR;
  2241. currval:=relsym.offset;
  2242. end;
  2243. objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  2244. inc(s,ea_data.bytes);
  2245. end;
  2246. end;
  2247. end
  2248. else
  2249. InternalError(777007);
  2250. end;
  2251. end;
  2252. until false;
  2253. end;
  2254. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  2255. begin
  2256. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  2257. (regtype = R_INTREGISTER) and
  2258. (ops=2) and
  2259. (oper[0]^.typ=top_reg) and
  2260. (oper[1]^.typ=top_reg) and
  2261. (oper[0]^.reg=oper[1]^.reg)
  2262. ) or
  2263. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
  2264. (opcode=A_MOVAPS) or (OPCODE=A_MOVAPD)) and
  2265. (regtype = R_MMREGISTER) and
  2266. (ops=2) and
  2267. (oper[0]^.typ=top_reg) and
  2268. (oper[1]^.typ=top_reg) and
  2269. (oper[0]^.reg=oper[1]^.reg)
  2270. );
  2271. end;
  2272. procedure build_spilling_operation_type_table;
  2273. var
  2274. opcode : tasmop;
  2275. i : integer;
  2276. begin
  2277. new(operation_type_table);
  2278. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2279. for opcode:=low(tasmop) to high(tasmop) do
  2280. begin
  2281. for i:=1 to MaxInsChanges do
  2282. begin
  2283. case InsProp[opcode].Ch[i] of
  2284. Ch_Rop1 :
  2285. operation_type_table^[opcode,0]:=operand_read;
  2286. Ch_Wop1 :
  2287. operation_type_table^[opcode,0]:=operand_write;
  2288. Ch_RWop1,
  2289. Ch_Mop1 :
  2290. operation_type_table^[opcode,0]:=operand_readwrite;
  2291. Ch_Rop2 :
  2292. operation_type_table^[opcode,1]:=operand_read;
  2293. Ch_Wop2 :
  2294. operation_type_table^[opcode,1]:=operand_write;
  2295. Ch_RWop2,
  2296. Ch_Mop2 :
  2297. operation_type_table^[opcode,1]:=operand_readwrite;
  2298. Ch_Rop3 :
  2299. operation_type_table^[opcode,2]:=operand_read;
  2300. Ch_Wop3 :
  2301. operation_type_table^[opcode,2]:=operand_write;
  2302. Ch_RWop3,
  2303. Ch_Mop3 :
  2304. operation_type_table^[opcode,2]:=operand_readwrite;
  2305. end;
  2306. end;
  2307. end;
  2308. { Special cases that can't be decoded from the InsChanges flags }
  2309. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  2310. end;
  2311. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2312. begin
  2313. { the information in the instruction table is made for the string copy
  2314. operation MOVSD so hack here (FK)
  2315. }
  2316. if (opcode=A_MOVSD) and (ops=2) then
  2317. begin
  2318. case opnr of
  2319. 0:
  2320. result:=operand_read;
  2321. 1:
  2322. result:=operand_write;
  2323. else
  2324. internalerror(200506055);
  2325. end
  2326. end
  2327. else
  2328. result:=operation_type_table^[opcode,opnr];
  2329. end;
  2330. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  2331. begin
  2332. case getregtype(r) of
  2333. R_INTREGISTER :
  2334. { we don't need special code here for 32 bit loads on x86_64, since
  2335. those will automatically zero-extend the upper 32 bits. }
  2336. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
  2337. R_MMREGISTER :
  2338. case getsubreg(r) of
  2339. R_SUBMMD:
  2340. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  2341. R_SUBMMS:
  2342. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  2343. R_SUBMMWHOLE:
  2344. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,ref,r);
  2345. else
  2346. internalerror(200506043);
  2347. end;
  2348. else
  2349. internalerror(200401041);
  2350. end;
  2351. end;
  2352. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  2353. var
  2354. size: topsize;
  2355. begin
  2356. case getregtype(r) of
  2357. R_INTREGISTER :
  2358. begin
  2359. size:=reg2opsize(r);
  2360. {$ifdef x86_64}
  2361. { even if it's a 32 bit reg, we still have to spill 64 bits
  2362. because we often perform 64 bit operations on them }
  2363. if (size=S_L) then
  2364. begin
  2365. size:=S_Q;
  2366. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  2367. end;
  2368. {$endif x86_64}
  2369. result:=taicpu.op_reg_ref(A_MOV,size,r,ref);
  2370. end;
  2371. R_MMREGISTER :
  2372. case getsubreg(r) of
  2373. R_SUBMMD:
  2374. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  2375. R_SUBMMS:
  2376. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  2377. R_SUBMMWHOLE:
  2378. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,ref);
  2379. else
  2380. internalerror(200506042);
  2381. end;
  2382. else
  2383. internalerror(200401041);
  2384. end;
  2385. end;
  2386. {*****************************************************************************
  2387. Instruction table
  2388. *****************************************************************************}
  2389. procedure BuildInsTabCache;
  2390. var
  2391. i : longint;
  2392. begin
  2393. new(instabcache);
  2394. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2395. i:=0;
  2396. while (i<InsTabEntries) do
  2397. begin
  2398. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2399. InsTabCache^[InsTab[i].OPcode]:=i;
  2400. inc(i);
  2401. end;
  2402. end;
  2403. procedure InitAsm;
  2404. begin
  2405. build_spilling_operation_type_table;
  2406. if not assigned(instabcache) then
  2407. BuildInsTabCache;
  2408. end;
  2409. procedure DoneAsm;
  2410. begin
  2411. if assigned(operation_type_table) then
  2412. begin
  2413. dispose(operation_type_table);
  2414. operation_type_table:=nil;
  2415. end;
  2416. if assigned(instabcache) then
  2417. begin
  2418. dispose(instabcache);
  2419. instabcache:=nil;
  2420. end;
  2421. end;
  2422. begin
  2423. cai_align:=tai_align;
  2424. cai_cpu:=taicpu;
  2425. end.