aasmcpu.pas 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512
  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,globals,verbose,
  25. cpubase,
  26. cgbase,cgutils,
  27. symtype,
  28. aasmbase,aasmtai,aasmdata,
  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 : longint;
  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):pchar;override;
  167. end;
  168. taicpu = class(tai_cpu_abstract)
  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. protected
  211. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  212. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  213. procedure ppubuildderefimploper(var o:toper);override;
  214. procedure ppuderefoper(var o:toper);override;
  215. private
  216. { next fields are filled in pass1, so pass2 is faster }
  217. insentry : PInsEntry;
  218. insoffset : longint;
  219. LastInsOffset : longint; { need to be public to be reset }
  220. inssize : shortint;
  221. {$ifdef x86_64}
  222. rex : byte;
  223. {$endif x86_64}
  224. function InsEnd:longint;
  225. procedure create_ot(objdata:TObjData);
  226. function Matches(p:PInsEntry):boolean;
  227. function calcsize(p:PInsEntry):shortint;
  228. procedure gencode(objdata:TObjData);
  229. function NeedAddrPrefix(opidx:byte):boolean;
  230. procedure Swapoperands;
  231. function FindInsentry(objdata:TObjData):boolean;
  232. end;
  233. function spilling_create_load(const ref:treference;r:tregister): tai;
  234. function spilling_create_store(r:tregister; const ref:treference): tai;
  235. procedure InitAsm;
  236. procedure DoneAsm;
  237. implementation
  238. uses
  239. cutils,
  240. itcpugas,
  241. symsym;
  242. {*****************************************************************************
  243. Instruction table
  244. *****************************************************************************}
  245. const
  246. {Instruction flags }
  247. IF_NONE = $00000000;
  248. IF_SM = $00000001; { size match first two operands }
  249. IF_SM2 = $00000002;
  250. IF_SB = $00000004; { unsized operands can't be non-byte }
  251. IF_SW = $00000008; { unsized operands can't be non-word }
  252. IF_SD = $00000010; { unsized operands can't be nondword }
  253. IF_SMASK = $0000001f;
  254. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  255. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  256. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  257. IF_ARMASK = $00000060; { mask for unsized argument spec }
  258. IF_PRIV = $00000100; { it's a privileged instruction }
  259. IF_SMM = $00000200; { it's only valid in SMM }
  260. IF_PROT = $00000400; { it's protected mode only }
  261. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  262. IF_UNDOC = $00001000; { it's an undocumented instruction }
  263. IF_FPU = $00002000; { it's an FPU instruction }
  264. IF_MMX = $00004000; { it's an MMX instruction }
  265. { it's a 3DNow! instruction }
  266. IF_3DNOW = $00008000;
  267. { it's a SSE (KNI, MMX2) instruction }
  268. IF_SSE = $00010000;
  269. { SSE2 instructions }
  270. IF_SSE2 = $00020000;
  271. { SSE3 instructions }
  272. IF_SSE3 = $00040000;
  273. { SSE64 instructions }
  274. IF_SSE64 = $00080000;
  275. { the mask for processor types }
  276. {IF_PMASK = longint($FF000000);}
  277. { the mask for disassembly "prefer" }
  278. {IF_PFMASK = longint($F001FF00);}
  279. { SVM instructions }
  280. IF_SVM = $00100000;
  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 = longint($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):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. begin
  421. inherited calculatefillbuf(buf);
  422. if not use_op then
  423. begin
  424. bufptr:=pchar(@buf);
  425. while (fillsize>0) do
  426. begin
  427. for j:=low(alignarray) to high(alignarray) do
  428. if (fillsize>=length(alignarray[j])) then
  429. break;
  430. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  431. inc(bufptr,length(alignarray[j]));
  432. dec(fillsize,length(alignarray[j]));
  433. end;
  434. end;
  435. calculatefillbuf:=pchar(@buf);
  436. end;
  437. {*****************************************************************************
  438. Taicpu Constructors
  439. *****************************************************************************}
  440. procedure taicpu.changeopsize(siz:topsize);
  441. begin
  442. opsize:=siz;
  443. end;
  444. procedure taicpu.init(_size : topsize);
  445. begin
  446. { default order is att }
  447. FOperandOrder:=op_att;
  448. segprefix:=NR_NO;
  449. opsize:=_size;
  450. insentry:=nil;
  451. LastInsOffset:=-1;
  452. InsOffset:=0;
  453. InsSize:=0;
  454. end;
  455. constructor taicpu.op_none(op : tasmop);
  456. begin
  457. inherited create(op);
  458. init(S_NO);
  459. end;
  460. constructor taicpu.op_none(op : tasmop;_size : topsize);
  461. begin
  462. inherited create(op);
  463. init(_size);
  464. end;
  465. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  466. begin
  467. inherited create(op);
  468. init(_size);
  469. ops:=1;
  470. loadreg(0,_op1);
  471. end;
  472. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  473. begin
  474. inherited create(op);
  475. init(_size);
  476. ops:=1;
  477. loadconst(0,_op1);
  478. end;
  479. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  480. begin
  481. inherited create(op);
  482. init(_size);
  483. ops:=1;
  484. loadref(0,_op1);
  485. end;
  486. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  487. begin
  488. inherited create(op);
  489. init(_size);
  490. ops:=2;
  491. loadreg(0,_op1);
  492. loadreg(1,_op2);
  493. end;
  494. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  495. begin
  496. inherited create(op);
  497. init(_size);
  498. ops:=2;
  499. loadreg(0,_op1);
  500. loadconst(1,_op2);
  501. end;
  502. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  503. begin
  504. inherited create(op);
  505. init(_size);
  506. ops:=2;
  507. loadreg(0,_op1);
  508. loadref(1,_op2);
  509. end;
  510. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  511. begin
  512. inherited create(op);
  513. init(_size);
  514. ops:=2;
  515. loadconst(0,_op1);
  516. loadreg(1,_op2);
  517. end;
  518. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  519. begin
  520. inherited create(op);
  521. init(_size);
  522. ops:=2;
  523. loadconst(0,_op1);
  524. loadconst(1,_op2);
  525. end;
  526. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  527. begin
  528. inherited create(op);
  529. init(_size);
  530. ops:=2;
  531. loadconst(0,_op1);
  532. loadref(1,_op2);
  533. end;
  534. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  535. begin
  536. inherited create(op);
  537. init(_size);
  538. ops:=2;
  539. loadref(0,_op1);
  540. loadreg(1,_op2);
  541. end;
  542. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  543. begin
  544. inherited create(op);
  545. init(_size);
  546. ops:=3;
  547. loadreg(0,_op1);
  548. loadreg(1,_op2);
  549. loadreg(2,_op3);
  550. end;
  551. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  552. begin
  553. inherited create(op);
  554. init(_size);
  555. ops:=3;
  556. loadconst(0,_op1);
  557. loadreg(1,_op2);
  558. loadreg(2,_op3);
  559. end;
  560. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  561. begin
  562. inherited create(op);
  563. init(_size);
  564. ops:=3;
  565. loadreg(0,_op1);
  566. loadreg(1,_op2);
  567. loadref(2,_op3);
  568. end;
  569. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  570. begin
  571. inherited create(op);
  572. init(_size);
  573. ops:=3;
  574. loadconst(0,_op1);
  575. loadref(1,_op2);
  576. loadreg(2,_op3);
  577. end;
  578. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  579. begin
  580. inherited create(op);
  581. init(_size);
  582. ops:=3;
  583. loadconst(0,_op1);
  584. loadreg(1,_op2);
  585. loadref(2,_op3);
  586. end;
  587. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  588. begin
  589. inherited create(op);
  590. init(_size);
  591. condition:=cond;
  592. ops:=1;
  593. loadsymbol(0,_op1,0);
  594. end;
  595. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  596. begin
  597. inherited create(op);
  598. init(_size);
  599. ops:=1;
  600. loadsymbol(0,_op1,0);
  601. end;
  602. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  603. begin
  604. inherited create(op);
  605. init(_size);
  606. ops:=1;
  607. loadsymbol(0,_op1,_op1ofs);
  608. end;
  609. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  610. begin
  611. inherited create(op);
  612. init(_size);
  613. ops:=2;
  614. loadsymbol(0,_op1,_op1ofs);
  615. loadreg(1,_op2);
  616. end;
  617. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  618. begin
  619. inherited create(op);
  620. init(_size);
  621. ops:=2;
  622. loadsymbol(0,_op1,_op1ofs);
  623. loadref(1,_op2);
  624. end;
  625. function taicpu.GetString:string;
  626. var
  627. i : longint;
  628. s : string;
  629. addsize : boolean;
  630. begin
  631. s:='['+std_op2str[opcode];
  632. for i:=0 to ops-1 do
  633. begin
  634. with oper[i]^ do
  635. begin
  636. if i=0 then
  637. s:=s+' '
  638. else
  639. s:=s+',';
  640. { type }
  641. addsize:=false;
  642. if (ot and OT_XMMREG)=OT_XMMREG then
  643. s:=s+'xmmreg'
  644. else
  645. if (ot and OT_MMXREG)=OT_MMXREG then
  646. s:=s+'mmxreg'
  647. else
  648. if (ot and OT_FPUREG)=OT_FPUREG then
  649. s:=s+'fpureg'
  650. else
  651. if (ot and OT_REGISTER)=OT_REGISTER then
  652. begin
  653. s:=s+'reg';
  654. addsize:=true;
  655. end
  656. else
  657. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  658. begin
  659. s:=s+'imm';
  660. addsize:=true;
  661. end
  662. else
  663. if (ot and OT_MEMORY)=OT_MEMORY then
  664. begin
  665. s:=s+'mem';
  666. addsize:=true;
  667. end
  668. else
  669. s:=s+'???';
  670. { size }
  671. if addsize then
  672. begin
  673. if (ot and OT_BITS8)<>0 then
  674. s:=s+'8'
  675. else
  676. if (ot and OT_BITS16)<>0 then
  677. s:=s+'16'
  678. else
  679. if (ot and OT_BITS32)<>0 then
  680. s:=s+'32'
  681. else
  682. if (ot and OT_BITS64)<>0 then
  683. s:=s+'64'
  684. else
  685. s:=s+'??';
  686. { signed }
  687. if (ot and OT_SIGNED)<>0 then
  688. s:=s+'s';
  689. end;
  690. end;
  691. end;
  692. GetString:=s+']';
  693. end;
  694. procedure taicpu.Swapoperands;
  695. var
  696. p : POper;
  697. begin
  698. { Fix the operands which are in AT&T style and we need them in Intel style }
  699. case ops of
  700. 2 : begin
  701. { 0,1 -> 1,0 }
  702. p:=oper[0];
  703. oper[0]:=oper[1];
  704. oper[1]:=p;
  705. end;
  706. 3 : begin
  707. { 0,1,2 -> 2,1,0 }
  708. p:=oper[0];
  709. oper[0]:=oper[2];
  710. oper[2]:=p;
  711. end;
  712. end;
  713. end;
  714. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  715. begin
  716. if FOperandOrder<>order then
  717. begin
  718. Swapoperands;
  719. FOperandOrder:=order;
  720. end;
  721. end;
  722. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  723. begin
  724. o.typ:=toptype(ppufile.getbyte);
  725. o.ot:=ppufile.getlongint;
  726. case o.typ of
  727. top_reg :
  728. ppufile.getdata(o.reg,sizeof(Tregister));
  729. top_ref :
  730. begin
  731. new(o.ref);
  732. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  733. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  734. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  735. o.ref^.scalefactor:=ppufile.getbyte;
  736. o.ref^.offset:=ppufile.getaint;
  737. o.ref^.symbol:=ppufile.getasmsymbol;
  738. o.ref^.relsymbol:=ppufile.getasmsymbol;
  739. end;
  740. top_const :
  741. o.val:=ppufile.getaint;
  742. top_local :
  743. begin
  744. new(o.localoper);
  745. with o.localoper^ do
  746. begin
  747. ppufile.getderef(localsymderef);
  748. localsymofs:=ppufile.getaint;
  749. localindexreg:=tregister(ppufile.getlongint);
  750. localscale:=ppufile.getbyte;
  751. localgetoffset:=(ppufile.getbyte<>0);
  752. end;
  753. end;
  754. end;
  755. end;
  756. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  757. begin
  758. ppufile.putbyte(byte(o.typ));
  759. ppufile.putlongint(o.ot);
  760. case o.typ of
  761. top_reg :
  762. ppufile.putdata(o.reg,sizeof(Tregister));
  763. top_ref :
  764. begin
  765. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  766. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  767. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  768. ppufile.putbyte(o.ref^.scalefactor);
  769. ppufile.putaint(o.ref^.offset);
  770. ppufile.putasmsymbol(o.ref^.symbol);
  771. ppufile.putasmsymbol(o.ref^.relsymbol);
  772. end;
  773. top_const :
  774. ppufile.putaint(o.val);
  775. top_local :
  776. begin
  777. with o.localoper^ do
  778. begin
  779. ppufile.putderef(localsymderef);
  780. ppufile.putaint(localsymofs);
  781. ppufile.putlongint(longint(localindexreg));
  782. ppufile.putbyte(localscale);
  783. ppufile.putbyte(byte(localgetoffset));
  784. end;
  785. end;
  786. end;
  787. end;
  788. procedure taicpu.ppubuildderefimploper(var o:toper);
  789. begin
  790. case o.typ of
  791. top_local :
  792. o.localoper^.localsymderef.build(tlocalvarsym(o.localoper^.localsym));
  793. end;
  794. end;
  795. procedure taicpu.ppuderefoper(var o:toper);
  796. begin
  797. case o.typ of
  798. top_ref :
  799. begin
  800. end;
  801. top_local :
  802. o.localoper^.localsym:=tlocalvarsym(o.localoper^.localsymderef.resolve);
  803. end;
  804. end;
  805. procedure taicpu.CheckNonCommutativeOpcodes;
  806. begin
  807. { we need ATT order }
  808. SetOperandOrder(op_att);
  809. if (
  810. (ops=2) and
  811. (oper[0]^.typ=top_reg) and
  812. (oper[1]^.typ=top_reg) and
  813. { if the first is ST and the second is also a register
  814. it is necessarily ST1 .. ST7 }
  815. ((oper[0]^.reg=NR_ST) or
  816. (oper[0]^.reg=NR_ST0))
  817. ) or
  818. { ((ops=1) and
  819. (oper[0]^.typ=top_reg) and
  820. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  821. (ops=0) then
  822. begin
  823. if opcode=A_FSUBR then
  824. opcode:=A_FSUB
  825. else if opcode=A_FSUB then
  826. opcode:=A_FSUBR
  827. else if opcode=A_FDIVR then
  828. opcode:=A_FDIV
  829. else if opcode=A_FDIV then
  830. opcode:=A_FDIVR
  831. else if opcode=A_FSUBRP then
  832. opcode:=A_FSUBP
  833. else if opcode=A_FSUBP then
  834. opcode:=A_FSUBRP
  835. else if opcode=A_FDIVRP then
  836. opcode:=A_FDIVP
  837. else if opcode=A_FDIVP then
  838. opcode:=A_FDIVRP;
  839. end;
  840. if (
  841. (ops=1) and
  842. (oper[0]^.typ=top_reg) and
  843. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  844. (oper[0]^.reg<>NR_ST)
  845. ) then
  846. begin
  847. if opcode=A_FSUBRP then
  848. opcode:=A_FSUBP
  849. else if opcode=A_FSUBP then
  850. opcode:=A_FSUBRP
  851. else if opcode=A_FDIVRP then
  852. opcode:=A_FDIVP
  853. else if opcode=A_FDIVP then
  854. opcode:=A_FDIVRP;
  855. end;
  856. end;
  857. {*****************************************************************************
  858. Assembler
  859. *****************************************************************************}
  860. type
  861. ea = packed record
  862. sib_present : boolean;
  863. bytes : byte;
  864. size : byte;
  865. modrm : byte;
  866. sib : byte;
  867. {$ifdef x86_64}
  868. rex_present : boolean;
  869. rex : byte;
  870. {$endif x86_64}
  871. end;
  872. procedure taicpu.create_ot(objdata:TObjData);
  873. {
  874. this function will also fix some other fields which only needs to be once
  875. }
  876. var
  877. i,l,relsize : longint;
  878. currsym : TObjSymbol;
  879. begin
  880. if ops=0 then
  881. exit;
  882. { update oper[].ot field }
  883. for i:=0 to ops-1 do
  884. with oper[i]^ do
  885. begin
  886. case typ of
  887. top_reg :
  888. begin
  889. ot:=reg_ot_table[findreg_by_number(reg)];
  890. end;
  891. top_ref :
  892. begin
  893. if ref^.refaddr=addr_no then
  894. begin
  895. { create ot field }
  896. if (ot and OT_SIZE_MASK)=0 then
  897. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  898. else
  899. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  900. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  901. ot:=ot or OT_MEM_OFFS;
  902. { fix scalefactor }
  903. if (ref^.index=NR_NO) then
  904. ref^.scalefactor:=0
  905. else
  906. if (ref^.scalefactor=0) then
  907. ref^.scalefactor:=1;
  908. end
  909. else
  910. begin
  911. if assigned(objdata) then
  912. begin
  913. currsym:=objdata.symbolref(ref^.symbol);
  914. l:=ref^.offset;
  915. if assigned(currsym) then
  916. inc(l,currsym.address);
  917. { when it is a forward jump we need to compensate the
  918. offset of the instruction since the previous time,
  919. because the symbol address is then still using the
  920. 'old-style' addressing.
  921. For backwards jumps this is not required because the
  922. address of the symbol is already adjusted to the
  923. new offset }
  924. if (l>InsOffset) and (LastInsOffset<>-1) then
  925. inc(l,InsOffset-LastInsOffset);
  926. { instruction size will then always become 2 (PFV) }
  927. relsize:=(InsOffset+2)-l;
  928. if (relsize>=-128) and (relsize<=127) and
  929. (
  930. not assigned(currsym) or
  931. (currsym.objsection=objdata.currobjsec)
  932. ) then
  933. ot:=OT_IMM8 or OT_SHORT
  934. else
  935. ot:=OT_IMM32 or OT_NEAR;
  936. end
  937. else
  938. ot:=OT_IMM32 or OT_NEAR;
  939. end;
  940. end;
  941. top_local :
  942. begin
  943. if (ot and OT_SIZE_MASK)=0 then
  944. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  945. else
  946. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  947. end;
  948. top_const :
  949. begin
  950. { allow 3rd operand being a constant and expect no size for shuf* etc. }
  951. if (opsize=S_NO) and (i<>2) then
  952. message(asmr_e_invalid_opcode_and_operand);
  953. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  954. ot:=OT_IMM8 or OT_SIGNED
  955. else
  956. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  957. end;
  958. top_none :
  959. begin
  960. { generated when there was an error in the
  961. assembler reader. It never happends when generating
  962. assembler }
  963. end;
  964. else
  965. internalerror(200402261);
  966. end;
  967. end;
  968. end;
  969. function taicpu.InsEnd:longint;
  970. begin
  971. InsEnd:=InsOffset+InsSize;
  972. end;
  973. function taicpu.Matches(p:PInsEntry):boolean;
  974. { * IF_SM stands for Size Match: any operand whose size is not
  975. * explicitly specified by the template is `really' intended to be
  976. * the same size as the first size-specified operand.
  977. * Non-specification is tolerated in the input instruction, but
  978. * _wrong_ specification is not.
  979. *
  980. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  981. * three-operand instructions such as SHLD: it implies that the
  982. * first two operands must match in size, but that the third is
  983. * required to be _unspecified_.
  984. *
  985. * IF_SB invokes Size Byte: operands with unspecified size in the
  986. * template are really bytes, and so no non-byte specification in
  987. * the input instruction will be tolerated. IF_SW similarly invokes
  988. * Size Word, and IF_SD invokes Size Doubleword.
  989. *
  990. * (The default state if neither IF_SM nor IF_SM2 is specified is
  991. * that any operand with unspecified size in the template is
  992. * required to have unspecified size in the instruction too...)
  993. }
  994. var
  995. insot,
  996. insflags,
  997. currot,
  998. i,j,asize,oprs : longint;
  999. siz : array[0..2] of longint;
  1000. begin
  1001. result:=false;
  1002. { Check the opcode and operands }
  1003. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1004. exit;
  1005. for i:=0 to p^.ops-1 do
  1006. begin
  1007. insot:=p^.optypes[i];
  1008. currot:=oper[i]^.ot;
  1009. { Check the operand flags }
  1010. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1011. exit;
  1012. { Check if the passed operand size matches with one of
  1013. the supported operand sizes }
  1014. if ((insot and OT_SIZE_MASK)<>0) and
  1015. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1016. exit;
  1017. end;
  1018. { Check operand sizes }
  1019. insflags:=p^.flags;
  1020. if insflags and IF_SMASK<>0 then
  1021. begin
  1022. { as default an untyped size can get all the sizes, this is different
  1023. from nasm, but else we need to do a lot checking which opcodes want
  1024. size or not with the automatic size generation }
  1025. asize:=-1;
  1026. if (insflags and IF_SB)<>0 then
  1027. asize:=OT_BITS8
  1028. else if (insflags and IF_SW)<>0 then
  1029. asize:=OT_BITS16
  1030. else if (insflags and IF_SD)<>0 then
  1031. asize:=OT_BITS32;
  1032. if (insflags and IF_ARMASK)<>0 then
  1033. begin
  1034. siz[0]:=0;
  1035. siz[1]:=0;
  1036. siz[2]:=0;
  1037. if (insflags and IF_AR0)<>0 then
  1038. siz[0]:=asize
  1039. else if (insflags and IF_AR1)<>0 then
  1040. siz[1]:=asize
  1041. else if (insflags and IF_AR2)<>0 then
  1042. siz[2]:=asize;
  1043. end
  1044. else
  1045. begin
  1046. siz[0]:=asize;
  1047. siz[1]:=asize;
  1048. siz[2]:=asize;
  1049. end;
  1050. if (insflags and (IF_SM or IF_SM2))<>0 then
  1051. begin
  1052. if (insflags and IF_SM2)<>0 then
  1053. oprs:=2
  1054. else
  1055. oprs:=p^.ops;
  1056. for i:=0 to oprs-1 do
  1057. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1058. begin
  1059. for j:=0 to oprs-1 do
  1060. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1061. break;
  1062. end;
  1063. end
  1064. else
  1065. oprs:=2;
  1066. { Check operand sizes }
  1067. for i:=0 to p^.ops-1 do
  1068. begin
  1069. insot:=p^.optypes[i];
  1070. currot:=oper[i]^.ot;
  1071. if ((insot and OT_SIZE_MASK)=0) and
  1072. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1073. { Immediates can always include smaller size }
  1074. ((currot and OT_IMMEDIATE)=0) and
  1075. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1076. exit;
  1077. end;
  1078. end;
  1079. result:=true;
  1080. end;
  1081. procedure taicpu.ResetPass1;
  1082. begin
  1083. { we need to reset everything here, because the choosen insentry
  1084. can be invalid for a new situation where the previously optimized
  1085. insentry is not correct }
  1086. InsEntry:=nil;
  1087. InsSize:=0;
  1088. LastInsOffset:=-1;
  1089. end;
  1090. procedure taicpu.ResetPass2;
  1091. begin
  1092. { we are here in a second pass, check if the instruction can be optimized }
  1093. if assigned(InsEntry) and
  1094. ((InsEntry^.flags and IF_PASS2)<>0) then
  1095. begin
  1096. InsEntry:=nil;
  1097. InsSize:=0;
  1098. end;
  1099. LastInsOffset:=-1;
  1100. end;
  1101. function taicpu.CheckIfValid:boolean;
  1102. begin
  1103. result:=FindInsEntry(nil);
  1104. end;
  1105. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1106. var
  1107. i : longint;
  1108. begin
  1109. result:=false;
  1110. { Things which may only be done once, not when a second pass is done to
  1111. optimize }
  1112. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1113. begin
  1114. { We need intel style operands }
  1115. SetOperandOrder(op_intel);
  1116. { create the .ot fields }
  1117. create_ot(objdata);
  1118. { set the file postion }
  1119. aktfilepos:=fileinfo;
  1120. end
  1121. else
  1122. begin
  1123. { we've already an insentry so it's valid }
  1124. result:=true;
  1125. exit;
  1126. end;
  1127. { Lookup opcode in the table }
  1128. InsSize:=-1;
  1129. i:=instabcache^[opcode];
  1130. if i=-1 then
  1131. begin
  1132. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1133. exit;
  1134. end;
  1135. insentry:=@instab[i];
  1136. while (insentry^.opcode=opcode) do
  1137. begin
  1138. if matches(insentry) then
  1139. begin
  1140. result:=true;
  1141. exit;
  1142. end;
  1143. inc(insentry);
  1144. end;
  1145. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1146. { No instruction found, set insentry to nil and inssize to -1 }
  1147. insentry:=nil;
  1148. inssize:=-1;
  1149. end;
  1150. function taicpu.Pass1(objdata:TObjData):longint;
  1151. begin
  1152. Pass1:=0;
  1153. { Save the old offset and set the new offset }
  1154. InsOffset:=ObjData.CurrObjSec.Size;
  1155. { Error? }
  1156. if (Insentry=nil) and (InsSize=-1) then
  1157. exit;
  1158. { set the file postion }
  1159. aktfilepos:=fileinfo;
  1160. { Get InsEntry }
  1161. if FindInsEntry(ObjData) then
  1162. begin
  1163. { Calculate instruction size }
  1164. InsSize:=calcsize(insentry);
  1165. if segprefix<>NR_NO then
  1166. inc(InsSize);
  1167. { Fix opsize if size if forced }
  1168. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1169. begin
  1170. if (insentry^.flags and IF_ARMASK)=0 then
  1171. begin
  1172. if (insentry^.flags and IF_SB)<>0 then
  1173. begin
  1174. if opsize=S_NO then
  1175. opsize:=S_B;
  1176. end
  1177. else if (insentry^.flags and IF_SW)<>0 then
  1178. begin
  1179. if opsize=S_NO then
  1180. opsize:=S_W;
  1181. end
  1182. else if (insentry^.flags and IF_SD)<>0 then
  1183. begin
  1184. if opsize=S_NO then
  1185. opsize:=S_L;
  1186. end;
  1187. end;
  1188. end;
  1189. LastInsOffset:=InsOffset;
  1190. Pass1:=InsSize;
  1191. exit;
  1192. end;
  1193. LastInsOffset:=-1;
  1194. end;
  1195. procedure taicpu.Pass2(objdata:TObjData);
  1196. var
  1197. c : longint;
  1198. begin
  1199. { error in pass1 ? }
  1200. if insentry=nil then
  1201. exit;
  1202. aktfilepos:=fileinfo;
  1203. { Segment override }
  1204. if (segprefix<>NR_NO) then
  1205. begin
  1206. case segprefix of
  1207. NR_CS : c:=$2e;
  1208. NR_DS : c:=$3e;
  1209. NR_ES : c:=$26;
  1210. NR_FS : c:=$64;
  1211. NR_GS : c:=$65;
  1212. NR_SS : c:=$36;
  1213. end;
  1214. objdata.writebytes(c,1);
  1215. { fix the offset for GenNode }
  1216. inc(InsOffset);
  1217. end;
  1218. { Generate the instruction }
  1219. GenCode(objdata);
  1220. end;
  1221. function taicpu.needaddrprefix(opidx:byte):boolean;
  1222. begin
  1223. result:=(oper[opidx]^.typ=top_ref) and
  1224. (oper[opidx]^.ref^.refaddr=addr_no) and
  1225. (
  1226. (
  1227. (oper[opidx]^.ref^.index<>NR_NO) and
  1228. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1229. ) or
  1230. (
  1231. (oper[opidx]^.ref^.base<>NR_NO) and
  1232. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1233. )
  1234. );
  1235. end;
  1236. function regval(r:Tregister):byte;
  1237. const
  1238. {$ifdef x86_64}
  1239. opcode_table:array[tregisterindex] of tregisterindex = (
  1240. {$i r8664op.inc}
  1241. );
  1242. {$else x86_64}
  1243. opcode_table:array[tregisterindex] of tregisterindex = (
  1244. {$i r386op.inc}
  1245. );
  1246. {$endif x86_64}
  1247. var
  1248. regidx : tregisterindex;
  1249. begin
  1250. regidx:=findreg_by_number(r);
  1251. if regidx<>0 then
  1252. result:=opcode_table[regidx]
  1253. else
  1254. begin
  1255. Message1(asmw_e_invalid_register,generic_regname(r));
  1256. result:=0;
  1257. end;
  1258. end;
  1259. {$ifdef x86_64}
  1260. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1261. var
  1262. sym : tasmsymbol;
  1263. md,s,rv : byte;
  1264. base,index,scalefactor,
  1265. o : longint;
  1266. ir,br : Tregister;
  1267. isub,bsub : tsubregister;
  1268. begin
  1269. process_ea:=false;
  1270. fillchar(output,sizeof(output),0);
  1271. {Register ?}
  1272. if (input.typ=top_reg) then
  1273. begin
  1274. rv:=regval(input.reg);
  1275. output.modrm:=$c0 or (rfield shl 3) or rv;
  1276. output.size:=1;
  1277. if ((getregtype(input.reg)=R_INTREGISTER) and
  1278. (getsupreg(input.reg)>=RS_R8)) or
  1279. ((getregtype(input.reg)=R_MMREGISTER) and
  1280. (getsupreg(input.reg)>=RS_XMM8)) then
  1281. begin
  1282. output.rex_present:=true;
  1283. output.rex:=output.rex or $41;
  1284. inc(output.size,1);
  1285. end
  1286. else if (getregtype(input.reg)=R_INTREGISTER) and
  1287. (getsubreg(input.reg)=R_SUBL) and
  1288. (getsupreg(input.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1289. begin
  1290. output.rex_present:=true;
  1291. output.rex:=output.rex or $40;
  1292. inc(output.size,1);
  1293. end;
  1294. process_ea:=true;
  1295. exit;
  1296. end;
  1297. {No register, so memory reference.}
  1298. if input.typ<>top_ref then
  1299. internalerror(200409263);
  1300. ir:=input.ref^.index;
  1301. br:=input.ref^.base;
  1302. isub:=getsubreg(ir);
  1303. bsub:=getsubreg(br);
  1304. s:=input.ref^.scalefactor;
  1305. o:=input.ref^.offset;
  1306. sym:=input.ref^.symbol;
  1307. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1308. ((br<>NR_NO) and (getregtype(br)<>R_INTREGISTER)) then
  1309. internalerror(200301081);
  1310. { it's direct address }
  1311. if (br=NR_NO) and (ir=NR_NO) then
  1312. begin
  1313. output.sib_present:=true;
  1314. output.bytes:=4;
  1315. output.modrm:=4 or (rfield shl 3);
  1316. output.sib:=$25;
  1317. end
  1318. else if (br=NR_RIP) and (ir=NR_NO) then
  1319. begin
  1320. { rip based }
  1321. output.sib_present:=false;
  1322. output.bytes:=4;
  1323. output.modrm:=5 or (rfield shl 3);
  1324. end
  1325. else
  1326. { it's an indirection }
  1327. begin
  1328. { 16 bit or 32 bit address? }
  1329. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1330. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1331. message(asmw_e_16bit_32bit_not_supported);
  1332. { wrong, for various reasons }
  1333. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1334. exit;
  1335. if ((getregtype(br)=R_INTREGISTER) and
  1336. (getsupreg(br)>=RS_R8)) or
  1337. ((getregtype(br)=R_MMREGISTER) and
  1338. (getsupreg(br)>=RS_XMM8)) then
  1339. begin
  1340. output.rex_present:=true;
  1341. output.rex:=output.rex or $41;
  1342. end;
  1343. if ((getregtype(ir)=R_INTREGISTER) and
  1344. (getsupreg(ir)>=RS_R8)) or
  1345. ((getregtype(ir)=R_MMREGISTER) and
  1346. (getsupreg(ir)>=RS_XMM8)) then
  1347. begin
  1348. output.rex_present:=true;
  1349. output.rex:=output.rex or $42;
  1350. end;
  1351. process_ea:=true;
  1352. { base }
  1353. case br of
  1354. NR_R8,
  1355. NR_RAX : base:=0;
  1356. NR_R9,
  1357. NR_RCX : base:=1;
  1358. NR_R10,
  1359. NR_RDX : base:=2;
  1360. NR_R11,
  1361. NR_RBX : base:=3;
  1362. NR_R12,
  1363. NR_RSP : base:=4;
  1364. NR_R13,
  1365. NR_NO,
  1366. NR_RBP : base:=5;
  1367. NR_R14,
  1368. NR_RSI : base:=6;
  1369. NR_R15,
  1370. NR_RDI : base:=7;
  1371. else
  1372. exit;
  1373. end;
  1374. { index }
  1375. case ir of
  1376. NR_R8,
  1377. NR_RAX : index:=0;
  1378. NR_R9,
  1379. NR_RCX : index:=1;
  1380. NR_R10,
  1381. NR_RDX : index:=2;
  1382. NR_R11,
  1383. NR_RBX : index:=3;
  1384. NR_R12,
  1385. NR_NO : index:=4;
  1386. NR_R13,
  1387. NR_RBP : index:=5;
  1388. NR_R14,
  1389. NR_RSI : index:=6;
  1390. NR_R15,
  1391. NR_RDI : index:=7;
  1392. else
  1393. exit;
  1394. end;
  1395. case s of
  1396. 0,
  1397. 1 : scalefactor:=0;
  1398. 2 : scalefactor:=1;
  1399. 4 : scalefactor:=2;
  1400. 8 : scalefactor:=3;
  1401. else
  1402. exit;
  1403. end;
  1404. { If rbp or r13 is used we must always include an offset }
  1405. if (br=NR_NO) or
  1406. ((br<>NR_RBP) and (br<>NR_R13) and (o=0) and (sym=nil)) then
  1407. md:=0
  1408. else
  1409. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1410. md:=1
  1411. else
  1412. md:=2;
  1413. if (br=NR_NO) or (md=2) then
  1414. output.bytes:=4
  1415. else
  1416. output.bytes:=md;
  1417. { SIB needed ? }
  1418. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) then
  1419. begin
  1420. output.sib_present:=false;
  1421. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1422. end
  1423. else
  1424. begin
  1425. output.sib_present:=true;
  1426. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1427. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1428. end;
  1429. end;
  1430. output.size:=1+ord(output.sib_present)+ord(output.rex_present)+output.bytes;
  1431. process_ea:=true;
  1432. end;
  1433. {$else x86_64}
  1434. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1435. var
  1436. sym : tasmsymbol;
  1437. md,s,rv : byte;
  1438. base,index,scalefactor,
  1439. o : longint;
  1440. ir,br : Tregister;
  1441. isub,bsub : tsubregister;
  1442. begin
  1443. process_ea:=false;
  1444. fillchar(output,sizeof(output),0);
  1445. {Register ?}
  1446. if (input.typ=top_reg) then
  1447. begin
  1448. rv:=regval(input.reg);
  1449. output.modrm:=$c0 or (rfield shl 3) or rv;
  1450. output.size:=1;
  1451. process_ea:=true;
  1452. exit;
  1453. end;
  1454. {No register, so memory reference.}
  1455. if (input.typ<>top_ref) then
  1456. internalerror(200409262);
  1457. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1458. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1459. internalerror(200301081);
  1460. ir:=input.ref^.index;
  1461. br:=input.ref^.base;
  1462. isub:=getsubreg(ir);
  1463. bsub:=getsubreg(br);
  1464. s:=input.ref^.scalefactor;
  1465. o:=input.ref^.offset;
  1466. sym:=input.ref^.symbol;
  1467. { it's direct address }
  1468. if (br=NR_NO) and (ir=NR_NO) then
  1469. begin
  1470. { it's a pure offset }
  1471. output.sib_present:=false;
  1472. output.bytes:=4;
  1473. output.modrm:=5 or (rfield shl 3);
  1474. end
  1475. else
  1476. { it's an indirection }
  1477. begin
  1478. { 16 bit address? }
  1479. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1480. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1481. message(asmw_e_16bit_not_supported);
  1482. {$ifdef OPTEA}
  1483. { make single reg base }
  1484. if (br=NR_NO) and (s=1) then
  1485. begin
  1486. br:=ir;
  1487. ir:=NR_NO;
  1488. end;
  1489. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1490. if (br=NR_NO) and
  1491. (((s=2) and (ir<>NR_ESP)) or
  1492. (s=3) or (s=5) or (s=9)) then
  1493. begin
  1494. br:=ir;
  1495. dec(s);
  1496. end;
  1497. { swap ESP into base if scalefactor is 1 }
  1498. if (s=1) and (ir=NR_ESP) then
  1499. begin
  1500. ir:=br;
  1501. br:=NR_ESP;
  1502. end;
  1503. {$endif OPTEA}
  1504. { wrong, for various reasons }
  1505. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1506. exit;
  1507. { base }
  1508. case br of
  1509. NR_EAX : base:=0;
  1510. NR_ECX : base:=1;
  1511. NR_EDX : base:=2;
  1512. NR_EBX : base:=3;
  1513. NR_ESP : base:=4;
  1514. NR_NO,
  1515. NR_EBP : base:=5;
  1516. NR_ESI : base:=6;
  1517. NR_EDI : base:=7;
  1518. else
  1519. exit;
  1520. end;
  1521. { index }
  1522. case ir of
  1523. NR_EAX : index:=0;
  1524. NR_ECX : index:=1;
  1525. NR_EDX : index:=2;
  1526. NR_EBX : index:=3;
  1527. NR_NO : index:=4;
  1528. NR_EBP : index:=5;
  1529. NR_ESI : index:=6;
  1530. NR_EDI : index:=7;
  1531. else
  1532. exit;
  1533. end;
  1534. case s of
  1535. 0,
  1536. 1 : scalefactor:=0;
  1537. 2 : scalefactor:=1;
  1538. 4 : scalefactor:=2;
  1539. 8 : scalefactor:=3;
  1540. else
  1541. exit;
  1542. end;
  1543. if (br=NR_NO) or
  1544. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1545. md:=0
  1546. else
  1547. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1548. md:=1
  1549. else
  1550. md:=2;
  1551. if (br=NR_NO) or (md=2) then
  1552. output.bytes:=4
  1553. else
  1554. output.bytes:=md;
  1555. { SIB needed ? }
  1556. if (ir=NR_NO) and (br<>NR_ESP) then
  1557. begin
  1558. output.sib_present:=false;
  1559. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1560. end
  1561. else
  1562. begin
  1563. output.sib_present:=true;
  1564. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1565. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1566. end;
  1567. end;
  1568. if output.sib_present then
  1569. output.size:=2+output.bytes
  1570. else
  1571. output.size:=1+output.bytes;
  1572. process_ea:=true;
  1573. end;
  1574. {$endif x86_64}
  1575. function taicpu.calcsize(p:PInsEntry):shortint;
  1576. var
  1577. codes : pchar;
  1578. c : byte;
  1579. len : shortint;
  1580. ea_data : ea;
  1581. begin
  1582. len:=0;
  1583. codes:=@p^.code;
  1584. {$ifdef x86_64}
  1585. rex:=0;
  1586. {$endif x86_64}
  1587. repeat
  1588. c:=ord(codes^);
  1589. inc(codes);
  1590. case c of
  1591. 0 :
  1592. break;
  1593. 1,2,3 :
  1594. begin
  1595. inc(codes,c);
  1596. inc(len,c);
  1597. end;
  1598. 8,9,10 :
  1599. begin
  1600. {$ifdef x86_64}
  1601. if ((getregtype(oper[c-8]^.reg)=R_INTREGISTER) and
  1602. (getsupreg(oper[c-8]^.reg)>=RS_R8)) or
  1603. ((getregtype(oper[c-8]^.reg)=R_MMREGISTER) and
  1604. (getsupreg(oper[c-8]^.reg)>=RS_XMM8)) then
  1605. begin
  1606. if rex=0 then
  1607. inc(len);
  1608. rex:=rex or $41;
  1609. end
  1610. else if (getregtype(oper[c-8]^.reg)=R_INTREGISTER) and
  1611. (getsubreg(oper[c-8]^.reg)=R_SUBL) and
  1612. (getsupreg(oper[c-8]^.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1613. begin
  1614. if rex=0 then
  1615. inc(len);
  1616. rex:=rex or $40;
  1617. end;
  1618. {$endif x86_64}
  1619. inc(codes);
  1620. inc(len);
  1621. end;
  1622. 11 :
  1623. begin
  1624. inc(codes);
  1625. inc(len);
  1626. end;
  1627. 4,5,6,7 :
  1628. begin
  1629. if opsize=S_W then
  1630. inc(len,2)
  1631. else
  1632. inc(len);
  1633. end;
  1634. 15,
  1635. 12,13,14,
  1636. 16,17,18,
  1637. 20,21,22,
  1638. 40,41,42 :
  1639. inc(len);
  1640. 24,25,26,
  1641. 31,
  1642. 48,49,50 :
  1643. inc(len,2);
  1644. 28,29,30:
  1645. begin
  1646. if opsize=S_Q then
  1647. inc(len,8)
  1648. else
  1649. inc(len,4);
  1650. end;
  1651. 32,33,34,
  1652. 52,53,54,
  1653. 56,57,58 :
  1654. inc(len,4);
  1655. 192,193,194 :
  1656. if NeedAddrPrefix(c-192) then
  1657. inc(len);
  1658. 208,209,210 :
  1659. begin
  1660. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1661. OT_BITS16:
  1662. inc(len);
  1663. {$ifdef x86_64}
  1664. OT_BITS64:
  1665. begin
  1666. if rex=0 then
  1667. inc(len);
  1668. rex:=rex or $48;
  1669. end;
  1670. {$endif x86_64}
  1671. end;
  1672. end;
  1673. 212 :
  1674. inc(len);
  1675. 214 :
  1676. begin
  1677. {$ifdef x86_64}
  1678. if rex=0 then
  1679. inc(len);
  1680. rex:=rex or $48;
  1681. {$endif x86_64}
  1682. end;
  1683. 200,
  1684. 201,
  1685. 202,
  1686. 211,
  1687. 213,
  1688. 215,
  1689. 217,218: ;
  1690. 219,220 :
  1691. inc(len);
  1692. 221:
  1693. {$ifdef x86_64}
  1694. { remove rex competely? }
  1695. if rex=$48 then
  1696. begin
  1697. rex:=0;
  1698. dec(len);
  1699. end
  1700. else
  1701. rex:=rex and $f7
  1702. {$endif x86_64}
  1703. ;
  1704. 64..191 :
  1705. begin
  1706. {$ifdef x86_64}
  1707. if (c<127) then
  1708. begin
  1709. if (oper[c and 7]^.typ=top_reg) then
  1710. begin
  1711. if ((getregtype(oper[c and 7]^.reg)=R_INTREGISTER) and
  1712. (getsupreg(oper[c and 7]^.reg)>=RS_R8)) or
  1713. ((getregtype(oper[c and 7]^.reg)=R_MMREGISTER) and
  1714. (getsupreg(oper[c and 7]^.reg)>=RS_XMM8)) then
  1715. begin
  1716. if rex=0 then
  1717. inc(len);
  1718. rex:=rex or $44;
  1719. end
  1720. else if (getregtype(oper[c and 7]^.reg)=R_INTREGISTER) and
  1721. (getsubreg(oper[c and 7]^.reg)=R_SUBL) and
  1722. (getsupreg(oper[c and 7]^.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1723. begin
  1724. if rex=0 then
  1725. inc(len);
  1726. rex:=rex or $40;
  1727. end;
  1728. end;
  1729. end;
  1730. {$endif x86_64}
  1731. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1732. Message(asmw_e_invalid_effective_address)
  1733. else
  1734. inc(len,ea_data.size);
  1735. {$ifdef x86_64}
  1736. { did we already create include a rex into the length calculation? }
  1737. if (rex<>0) and (ea_data.rex<>0) then
  1738. dec(len);
  1739. rex:=rex or ea_data.rex;
  1740. {$endif x86_64}
  1741. end;
  1742. else
  1743. InternalError(200603141);
  1744. end;
  1745. until false;
  1746. calcsize:=len;
  1747. end;
  1748. procedure taicpu.GenCode(objdata:TObjData);
  1749. {
  1750. * the actual codes (C syntax, i.e. octal):
  1751. * \0 - terminates the code. (Unless it's a literal of course.)
  1752. * \1, \2, \3 - that many literal bytes follow in the code stream
  1753. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1754. * (POP is never used for CS) depending on operand 0
  1755. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1756. * on operand 0
  1757. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1758. * to the register value of operand 0, 1 or 2
  1759. * \13 - a literal byte follows in the code stream, to be added
  1760. * to the condition code value of the instruction.
  1761. * \17 - encodes the literal byte 0. (Some compilers don't take
  1762. * kindly to a zero byte in the _middle_ of a compile time
  1763. * string constant, so I had to put this hack in.)
  1764. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1765. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1766. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1767. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1768. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1769. * assembly mode or the address-size override on the operand
  1770. * \37 - a word constant, from the _segment_ part of operand 0
  1771. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1772. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1773. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1774. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1775. * assembly mode or the address-size override on the operand
  1776. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1777. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1778. * field the register value of operand b.
  1779. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1780. * field equal to digit b.
  1781. * \300,\301,\302 - might be an 0x67 or 0x48 byte, depending on the address size of
  1782. * the memory reference in operand x.
  1783. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1784. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1785. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1786. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  1787. * size of operand x.
  1788. * \323 - insert x86_64 REX at this position.
  1789. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1790. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1791. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1792. * \327 - indicates that this instruction is only valid when the
  1793. * operand size is the default (instruction to disassembler,
  1794. * generates no code in the assembler)
  1795. * \331 - instruction not valid with REP prefix. Hint for
  1796. * disassembler only; for SSE instructions.
  1797. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  1798. * \333 - REP prefix (0xF3 byte); for SSE instructions. Not encoded
  1799. * \335 - removes rex size prefix, i.e. rex.w must be the last opcode
  1800. }
  1801. var
  1802. currval : aint;
  1803. currsym : tobjsymbol;
  1804. {$ifdef x86_64}
  1805. rexwritten : boolean;
  1806. {$endif x86_64}
  1807. procedure getvalsym(opidx:longint);
  1808. begin
  1809. case oper[opidx]^.typ of
  1810. top_ref :
  1811. begin
  1812. currval:=oper[opidx]^.ref^.offset;
  1813. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  1814. end;
  1815. top_const :
  1816. begin
  1817. currval:=aint(oper[opidx]^.val);
  1818. currsym:=nil;
  1819. end;
  1820. else
  1821. Message(asmw_e_immediate_or_reference_expected);
  1822. end;
  1823. end;
  1824. {$ifdef x86_64}
  1825. procedure maybewriterex;
  1826. begin
  1827. if rex<>0 then
  1828. begin
  1829. rexwritten:=true;
  1830. objdata.writebytes(rex,1);
  1831. end;
  1832. end;
  1833. {$endif x86_64}
  1834. const
  1835. CondVal:array[TAsmCond] of byte=($0,
  1836. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1837. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1838. $0, $A, $A, $B, $8, $4);
  1839. var
  1840. c : byte;
  1841. pb,
  1842. codes : pchar;
  1843. bytes : array[0..3] of byte;
  1844. rfield,
  1845. data,s,opidx : longint;
  1846. ea_data : ea;
  1847. begin
  1848. { safety check }
  1849. if objdata.currobjsec.size<>insoffset then
  1850. internalerror(200130121);
  1851. { load data to write }
  1852. codes:=insentry^.code;
  1853. {$ifdef x86_64}
  1854. rexwritten:=false;
  1855. {$endif x86_64}
  1856. { Force word push/pop for registers }
  1857. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1858. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1859. begin
  1860. bytes[0]:=$66;
  1861. objdata.writebytes(bytes,1);
  1862. end;
  1863. repeat
  1864. c:=ord(codes^);
  1865. inc(codes);
  1866. case c of
  1867. 0 :
  1868. break;
  1869. 1,2,3 :
  1870. begin
  1871. objdata.writebytes(codes^,c);
  1872. inc(codes,c);
  1873. end;
  1874. 4,6 :
  1875. begin
  1876. case oper[0]^.reg of
  1877. NR_CS:
  1878. bytes[0]:=$e;
  1879. NR_NO,
  1880. NR_DS:
  1881. bytes[0]:=$1e;
  1882. NR_ES:
  1883. bytes[0]:=$6;
  1884. NR_SS:
  1885. bytes[0]:=$16;
  1886. else
  1887. internalerror(777004);
  1888. end;
  1889. if c=4 then
  1890. inc(bytes[0]);
  1891. objdata.writebytes(bytes,1);
  1892. end;
  1893. 5,7 :
  1894. begin
  1895. case oper[0]^.reg of
  1896. NR_FS:
  1897. bytes[0]:=$a0;
  1898. NR_GS:
  1899. bytes[0]:=$a8;
  1900. else
  1901. internalerror(777005);
  1902. end;
  1903. if c=5 then
  1904. inc(bytes[0]);
  1905. objdata.writebytes(bytes,1);
  1906. end;
  1907. 8,9,10 :
  1908. begin
  1909. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1910. inc(codes);
  1911. objdata.writebytes(bytes,1);
  1912. end;
  1913. 11 :
  1914. begin
  1915. bytes[0]:=ord(codes^)+condval[condition];
  1916. inc(codes);
  1917. objdata.writebytes(bytes,1);
  1918. end;
  1919. 15 :
  1920. begin
  1921. bytes[0]:=0;
  1922. objdata.writebytes(bytes,1);
  1923. end;
  1924. 12,13,14 :
  1925. begin
  1926. getvalsym(c-12);
  1927. if (currval<-128) or (currval>127) then
  1928. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1929. if assigned(currsym) then
  1930. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1931. else
  1932. objdata.writebytes(currval,1);
  1933. end;
  1934. 16,17,18 :
  1935. begin
  1936. getvalsym(c-16);
  1937. if (currval<-256) or (currval>255) then
  1938. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1939. if assigned(currsym) then
  1940. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1941. else
  1942. objdata.writebytes(currval,1);
  1943. end;
  1944. 20,21,22 :
  1945. begin
  1946. getvalsym(c-20);
  1947. if (currval<0) or (currval>255) then
  1948. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1949. if assigned(currsym) then
  1950. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1951. else
  1952. objdata.writebytes(currval,1);
  1953. end;
  1954. 24,25,26 :
  1955. begin
  1956. getvalsym(c-24);
  1957. if (currval<-65536) or (currval>65535) then
  1958. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1959. if assigned(currsym) then
  1960. objdata.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1961. else
  1962. objdata.writebytes(currval,2);
  1963. end;
  1964. 28,29,30 :
  1965. begin
  1966. getvalsym(c-28);
  1967. if opsize=S_Q then
  1968. begin
  1969. if assigned(currsym) then
  1970. objdata.writereloc(currval,8,currsym,RELOC_ABSOLUTE)
  1971. else
  1972. objdata.writebytes(currval,8);
  1973. end
  1974. else
  1975. begin
  1976. if assigned(currsym) then
  1977. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE32)
  1978. else
  1979. objdata.writebytes(currval,4);
  1980. end
  1981. end;
  1982. 32,33,34 :
  1983. begin
  1984. getvalsym(c-32);
  1985. if assigned(currsym) then
  1986. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE32)
  1987. else
  1988. objdata.writebytes(currval,4);
  1989. end;
  1990. 40,41,42 :
  1991. begin
  1992. getvalsym(c-40);
  1993. data:=currval-insend;
  1994. if assigned(currsym) then
  1995. inc(data,currsym.address);
  1996. if (data>127) or (data<-128) then
  1997. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1998. objdata.writebytes(data,1);
  1999. end;
  2000. 52,53,54 :
  2001. begin
  2002. getvalsym(c-52);
  2003. if assigned(currsym) then
  2004. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  2005. else
  2006. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE32)
  2007. end;
  2008. 56,57,58 :
  2009. begin
  2010. getvalsym(c-56);
  2011. if assigned(currsym) then
  2012. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  2013. else
  2014. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE32)
  2015. end;
  2016. 192,193,194 :
  2017. begin
  2018. if NeedAddrPrefix(c-192) then
  2019. begin
  2020. bytes[0]:=$67;
  2021. objdata.writebytes(bytes,1);
  2022. end;
  2023. end;
  2024. 200 :
  2025. begin
  2026. bytes[0]:=$67;
  2027. objdata.writebytes(bytes,1);
  2028. end;
  2029. 208,209,210 :
  2030. begin
  2031. case oper[c-208]^.ot and OT_SIZE_MASK of
  2032. OT_BITS16 :
  2033. begin
  2034. bytes[0]:=$66;
  2035. objdata.writebytes(bytes,1);
  2036. end;
  2037. {$ifndef x86_64}
  2038. OT_BITS64 :
  2039. Message(asmw_e_64bit_not_supported);
  2040. {$endif x86_64}
  2041. end;
  2042. {$ifdef x86_64}
  2043. maybewriterex;
  2044. {$endif x86_64}
  2045. end;
  2046. 211,
  2047. 213 :
  2048. begin
  2049. {$ifdef x86_64}
  2050. maybewriterex;
  2051. {$endif x86_64}
  2052. end;
  2053. 212 :
  2054. begin
  2055. bytes[0]:=$66;
  2056. objdata.writebytes(bytes,1);
  2057. {$ifdef x86_64}
  2058. maybewriterex;
  2059. {$endif x86_64}
  2060. end;
  2061. 214 :
  2062. begin
  2063. {$ifdef x86_64}
  2064. maybewriterex;
  2065. {$else x86_64}
  2066. Message(asmw_e_64bit_not_supported);
  2067. {$endif x86_64}
  2068. end;
  2069. 219 :
  2070. begin
  2071. {$ifdef x86_64}
  2072. maybewriterex;
  2073. {$endif x86_64}
  2074. bytes[0]:=$f3;
  2075. objdata.writebytes(bytes,1);
  2076. end;
  2077. 220 :
  2078. begin
  2079. bytes[0]:=$f2;
  2080. objdata.writebytes(bytes,1);
  2081. end;
  2082. 221:
  2083. ;
  2084. 201,
  2085. 202,
  2086. 215,
  2087. 217,218 :
  2088. begin
  2089. { these are dissambler hints or 32 bit prefixes which
  2090. are not needed
  2091. It's usefull to write rex :) (FK) }
  2092. {$ifdef x86_64}
  2093. maybewriterex;
  2094. {$endif x86_64}
  2095. end;
  2096. 31,
  2097. 48,49,50 :
  2098. begin
  2099. InternalError(777006);
  2100. end
  2101. else
  2102. begin
  2103. { rex should be written at this point }
  2104. {$ifdef x86_64}
  2105. if (rex<>0) and not(rexwritten) then
  2106. internalerror(200603191);
  2107. {$endif x86_64}
  2108. if (c>=64) and (c<=191) then
  2109. begin
  2110. if (c<127) then
  2111. begin
  2112. if (oper[c and 7]^.typ=top_reg) then
  2113. rfield:=regval(oper[c and 7]^.reg)
  2114. else
  2115. rfield:=regval(oper[c and 7]^.ref^.base);
  2116. end
  2117. else
  2118. rfield:=c and 7;
  2119. opidx:=(c shr 3) and 7;
  2120. if not process_ea(oper[opidx]^,ea_data,rfield) then
  2121. Message(asmw_e_invalid_effective_address);
  2122. pb:=@bytes;
  2123. pb^:=chr(ea_data.modrm);
  2124. inc(pb);
  2125. if ea_data.sib_present then
  2126. begin
  2127. pb^:=chr(ea_data.sib);
  2128. inc(pb);
  2129. end;
  2130. s:=pb-pchar(@bytes);
  2131. objdata.writebytes(bytes,s);
  2132. case ea_data.bytes of
  2133. 0 : ;
  2134. 1 :
  2135. begin
  2136. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  2137. begin
  2138. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2139. objdata.writereloc(oper[opidx]^.ref^.offset,1,currsym,RELOC_ABSOLUTE);
  2140. end
  2141. else
  2142. begin
  2143. bytes[0]:=oper[opidx]^.ref^.offset;
  2144. objdata.writebytes(bytes,1);
  2145. end;
  2146. inc(s);
  2147. end;
  2148. 2,4 :
  2149. begin
  2150. objdata.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,
  2151. objdata.symbolref(oper[opidx]^.ref^.symbol),RELOC_ABSOLUTE32);
  2152. inc(s,ea_data.bytes);
  2153. end;
  2154. end;
  2155. end
  2156. else
  2157. InternalError(777007);
  2158. end;
  2159. end;
  2160. until false;
  2161. end;
  2162. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  2163. begin
  2164. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  2165. (regtype = R_INTREGISTER) and
  2166. (ops=2) and
  2167. (oper[0]^.typ=top_reg) and
  2168. (oper[1]^.typ=top_reg) and
  2169. (oper[0]^.reg=oper[1]^.reg)
  2170. ) or
  2171. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ)) and
  2172. (regtype = R_MMREGISTER) and
  2173. (ops=2) and
  2174. (oper[0]^.typ=top_reg) and
  2175. (oper[1]^.typ=top_reg) and
  2176. (oper[0]^.reg=oper[1]^.reg)
  2177. );
  2178. end;
  2179. procedure build_spilling_operation_type_table;
  2180. var
  2181. opcode : tasmop;
  2182. i : integer;
  2183. begin
  2184. new(operation_type_table);
  2185. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2186. for opcode:=low(tasmop) to high(tasmop) do
  2187. begin
  2188. for i:=1 to MaxInsChanges do
  2189. begin
  2190. case InsProp[opcode].Ch[i] of
  2191. Ch_Rop1 :
  2192. operation_type_table^[opcode,0]:=operand_read;
  2193. Ch_Wop1 :
  2194. operation_type_table^[opcode,0]:=operand_write;
  2195. Ch_RWop1,
  2196. Ch_Mop1 :
  2197. operation_type_table^[opcode,0]:=operand_readwrite;
  2198. Ch_Rop2 :
  2199. operation_type_table^[opcode,1]:=operand_read;
  2200. Ch_Wop2 :
  2201. operation_type_table^[opcode,1]:=operand_write;
  2202. Ch_RWop2,
  2203. Ch_Mop2 :
  2204. operation_type_table^[opcode,1]:=operand_readwrite;
  2205. Ch_Rop3 :
  2206. operation_type_table^[opcode,2]:=operand_read;
  2207. Ch_Wop3 :
  2208. operation_type_table^[opcode,2]:=operand_write;
  2209. Ch_RWop3,
  2210. Ch_Mop3 :
  2211. operation_type_table^[opcode,2]:=operand_readwrite;
  2212. end;
  2213. end;
  2214. end;
  2215. { Special cases that can't be decoded from the InsChanges flags }
  2216. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  2217. end;
  2218. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2219. begin
  2220. { the information in the instruction table is made for the string copy
  2221. operation MOVSD so hack here (FK)
  2222. }
  2223. if (opcode=A_MOVSD) and (ops=2) then
  2224. begin
  2225. case opnr of
  2226. 0:
  2227. result:=operand_read;
  2228. 1:
  2229. result:=operand_write;
  2230. else
  2231. internalerror(200506055);
  2232. end
  2233. end
  2234. else
  2235. result:=operation_type_table^[opcode,opnr];
  2236. end;
  2237. function spilling_create_load(const ref:treference;r:tregister): tai;
  2238. begin
  2239. case getregtype(r) of
  2240. R_INTREGISTER :
  2241. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
  2242. R_MMREGISTER :
  2243. case getsubreg(r) of
  2244. R_SUBMMD:
  2245. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  2246. R_SUBMMS:
  2247. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  2248. else
  2249. internalerror(200506043);
  2250. end;
  2251. else
  2252. internalerror(200401041);
  2253. end;
  2254. end;
  2255. function spilling_create_store(r:tregister; const ref:treference): tai;
  2256. begin
  2257. case getregtype(r) of
  2258. R_INTREGISTER :
  2259. result:=taicpu.op_reg_ref(A_MOV,reg2opsize(r),r,ref);
  2260. R_MMREGISTER :
  2261. case getsubreg(r) of
  2262. R_SUBMMD:
  2263. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  2264. R_SUBMMS:
  2265. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  2266. else
  2267. internalerror(200506042);
  2268. end;
  2269. else
  2270. internalerror(200401041);
  2271. end;
  2272. end;
  2273. {*****************************************************************************
  2274. Instruction table
  2275. *****************************************************************************}
  2276. procedure BuildInsTabCache;
  2277. var
  2278. i : longint;
  2279. begin
  2280. new(instabcache);
  2281. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2282. i:=0;
  2283. while (i<InsTabEntries) do
  2284. begin
  2285. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2286. InsTabCache^[InsTab[i].OPcode]:=i;
  2287. inc(i);
  2288. end;
  2289. end;
  2290. procedure InitAsm;
  2291. begin
  2292. build_spilling_operation_type_table;
  2293. if not assigned(instabcache) then
  2294. BuildInsTabCache;
  2295. end;
  2296. procedure DoneAsm;
  2297. begin
  2298. if assigned(operation_type_table) then
  2299. begin
  2300. dispose(operation_type_table);
  2301. operation_type_table:=nil;
  2302. end;
  2303. if assigned(instabcache) then
  2304. begin
  2305. dispose(instabcache);
  2306. instabcache:=nil;
  2307. end;
  2308. end;
  2309. begin
  2310. cai_align:=tai_align;
  2311. cai_cpu:=taicpu;
  2312. end.