aasmcpu.pas 84 KB

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