aasmcpu.pas 79 KB

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