aasmcpu.pas 85 KB

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