aasmcpu.pas 69 KB

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