aasmcpu.pas 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  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. const
  30. { "mov reg,reg" source operand number }
  31. O_MOV_SOURCE = 0;
  32. { "mov reg,reg" destination operand number }
  33. O_MOV_DEST = 1;
  34. { Operand types }
  35. OT_NONE = $00000000;
  36. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  37. OT_BITS16 = $00000002;
  38. OT_BITS32 = $00000004;
  39. OT_BITS64 = $00000008; { FPU only }
  40. OT_BITS80 = $00000010;
  41. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  42. OT_NEAR = $00000040;
  43. OT_SHORT = $00000080;
  44. OT_SIZE_MASK = $000000FF; { all the size attributes }
  45. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  46. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  47. OT_TO = $00000200; { operand is followed by a colon }
  48. { reverse effect in FADD, FSUB &c }
  49. OT_COLON = $00000400;
  50. OT_REGISTER = $00001000;
  51. OT_IMMEDIATE = $00002000;
  52. OT_IMM8 = $00002001;
  53. OT_IMM16 = $00002002;
  54. OT_IMM32 = $00002004;
  55. OT_IMM64 = $00002008;
  56. OT_IMM80 = $00002010;
  57. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  58. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  59. OT_REG8 = $00201001;
  60. OT_REG16 = $00201002;
  61. OT_REG32 = $00201004;
  62. OT_REG64 = $00201008;
  63. OT_MMXREG = $00201008; { MMX registers }
  64. OT_XMMREG = $00201010; { Katmai registers }
  65. OT_MEMORY = $00204000; { register number in 'basereg' }
  66. OT_MEM8 = $00204001;
  67. OT_MEM16 = $00204002;
  68. OT_MEM32 = $00204004;
  69. OT_MEM64 = $00204008;
  70. OT_MEM80 = $00204010;
  71. OT_FPUREG = $01000000; { floating point stack registers }
  72. OT_FPU0 = $01000800; { FPU stack register zero }
  73. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  74. { a mask for the following }
  75. OT_REG_ACCUM = $00211000; { FUNCTION_RETURN_REG: AL, AX or EAX }
  76. OT_REG_AL = $00211001; { REG_ACCUM | BITSxx }
  77. OT_REG_AX = $00211002; { ditto }
  78. OT_REG_EAX = $00211004; { and again }
  79. {$ifdef x86_64}
  80. OT_REG_RAX = $00211008;
  81. {$endif x86_64}
  82. OT_REG_COUNT = $00221000; { counter: CL, CX or ECX }
  83. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  84. OT_REG_CX = $00221002; { ditto }
  85. OT_REG_ECX = $00221004; { another one }
  86. {$ifdef x86_64}
  87. OT_REG_RCX = $00221008;
  88. {$endif x86_64}
  89. OT_REG_DX = $00241002;
  90. OT_REG_EDX = $00241004;
  91. OT_REG_SREG = $00081002; { any segment register }
  92. OT_REG_CS = $01081002; { CS }
  93. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  94. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  95. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  96. OT_REG_CREG = $08101004; { CRn }
  97. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  98. OT_REG_DREG = $10101004; { DRn }
  99. OT_REG_TREG = $20101004; { TRn }
  100. OT_MEM_OFFS = $00604000; { special type of EA }
  101. { simple [address] offset }
  102. OT_ONENESS = $00800000; { special type of immediate operand }
  103. { so UNITY == IMMEDIATE | ONENESS }
  104. OT_UNITY = $00802000; { for shift/rotate instructions }
  105. { Size of the instruction table converted by nasmconv.pas }
  106. {$ifdef x86_64}
  107. instabentries = {$i x8664nop.inc}
  108. {$else x86_64}
  109. instabentries = {$i i386nop.inc}
  110. {$endif x86_64}
  111. maxinfolen = 8;
  112. MaxInsChanges = 3; { Max things a instruction can change }
  113. type
  114. { What an instruction can change. Needed for optimizer and spilling code.
  115. Note: The order of this enumeration is should not be changed! }
  116. TInsChange = (Ch_None,
  117. {Read from a register}
  118. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  119. {write from a register}
  120. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  121. {read and write from/to a register}
  122. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  123. {modify the contents of a register with the purpose of using
  124. this changed content afterwards (add/sub/..., but e.g. not rep
  125. or movsd)}
  126. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  127. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  128. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  129. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  130. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  131. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  132. Ch_WMemEDI,
  133. Ch_All,
  134. { x86_64 registers }
  135. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  136. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  137. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  138. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  139. );
  140. TInsProp = packed record
  141. Ch : Array[1..MaxInsChanges] of TInsChange;
  142. end;
  143. const
  144. InsProp : array[tasmop] of TInsProp =
  145. {$ifdef x86_64}
  146. {$i x8664pro.inc}
  147. {$else x86_64}
  148. {$i i386prop.inc}
  149. {$endif x86_64}
  150. type
  151. TOperandOrder = (op_intel,op_att);
  152. tinsentry=packed record
  153. opcode : tasmop;
  154. ops : byte;
  155. optypes : array[0..2] of longint;
  156. code : array[0..maxinfolen] of char;
  157. flags : longint;
  158. end;
  159. pinsentry=^tinsentry;
  160. { alignment for operator }
  161. tai_align = class(tai_align_abstract)
  162. reg : tregister;
  163. constructor create(b:byte);override;
  164. constructor create_op(b: byte; _op: byte);override;
  165. function calculatefillbuf(var buf : tfillbuffer):pchar;override;
  166. end;
  167. taicpu = class(tai_cpu_abstract)
  168. opsize : topsize;
  169. constructor op_none(op : tasmop);
  170. constructor op_none(op : tasmop;_size : topsize);
  171. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  172. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  173. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  174. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  175. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  176. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  177. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  178. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  179. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  180. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  181. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  182. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  183. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  184. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  185. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  186. { this is for Jmp instructions }
  187. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  188. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  189. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  190. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  191. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  192. procedure changeopsize(siz:topsize);
  193. function GetString:string;
  194. procedure CheckNonCommutativeOpcodes;
  195. private
  196. FOperandOrder : TOperandOrder;
  197. procedure init(_size : topsize); { this need to be called by all constructor }
  198. {$ifndef NOAG386BIN}
  199. public
  200. { the next will reset all instructions that can change in pass 2 }
  201. procedure ResetPass1;
  202. procedure ResetPass2;
  203. function CheckIfValid:boolean;
  204. function Pass1(offset:longint):longint;override;
  205. procedure Pass2(objdata:TAsmObjectdata);override;
  206. procedure SetOperandOrder(order:TOperandOrder);
  207. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  208. { register spilling code }
  209. function spilling_get_operation_type(opnr: longint): topertype;override;
  210. protected
  211. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  212. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  213. procedure ppubuildderefimploper(var o:toper);override;
  214. procedure ppuderefoper(var o:toper);override;
  215. private
  216. { next fields are filled in pass1, so pass2 is faster }
  217. inssize : shortint;
  218. insoffset : longint;
  219. LastInsOffset : longint; { need to be public to be reset }
  220. insentry : PInsEntry;
  221. function InsEnd:longint;
  222. procedure create_ot;
  223. function Matches(p:PInsEntry):longint;
  224. function calcsize(p:PInsEntry):shortint;
  225. procedure gencode(objdata:TAsmObjectData);
  226. function NeedAddrPrefix(opidx:byte):boolean;
  227. procedure Swapoperands;
  228. function FindInsentry:boolean;
  229. {$endif NOAG386BIN}
  230. end;
  231. function spilling_create_load(const ref:treference;r:tregister): tai;
  232. function spilling_create_store(r:tregister; const ref:treference): tai;
  233. procedure InitAsm;
  234. procedure DoneAsm;
  235. implementation
  236. uses
  237. cutils,
  238. itcpugas,
  239. symsym;
  240. {*****************************************************************************
  241. Instruction table
  242. *****************************************************************************}
  243. const
  244. {Instruction flags }
  245. IF_NONE = $00000000;
  246. IF_SM = $00000001; { size match first two operands }
  247. IF_SM2 = $00000002;
  248. IF_SB = $00000004; { unsized operands can't be non-byte }
  249. IF_SW = $00000008; { unsized operands can't be non-word }
  250. IF_SD = $00000010; { unsized operands can't be nondword }
  251. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  252. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  253. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  254. IF_ARMASK = $00000060; { mask for unsized argument spec }
  255. IF_PRIV = $00000100; { it's a privileged instruction }
  256. IF_SMM = $00000200; { it's only valid in SMM }
  257. IF_PROT = $00000400; { it's protected mode only }
  258. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  259. IF_UNDOC = $00001000; { it's an undocumented instruction }
  260. IF_FPU = $00002000; { it's an FPU instruction }
  261. IF_MMX = $00004000; { it's an MMX instruction }
  262. { it's a 3DNow! instruction }
  263. IF_3DNOW = $00008000;
  264. { it's a SSE (KNI, MMX2) instruction }
  265. IF_SSE = $00010000;
  266. { SSE2 instructions }
  267. IF_SSE2 = $00020000;
  268. { SSE3 instructions }
  269. IF_SSE3 = $00040000;
  270. { SSE64 instructions }
  271. IF_SSE64 = $00080000;
  272. { the mask for processor types }
  273. {IF_PMASK = longint($FF000000);}
  274. { the mask for disassembly "prefer" }
  275. {IF_PFMASK = longint($F001FF00);}
  276. IF_8086 = $00000000; { 8086 instruction }
  277. IF_186 = $01000000; { 186+ instruction }
  278. IF_286 = $02000000; { 286+ instruction }
  279. IF_386 = $03000000; { 386+ instruction }
  280. IF_486 = $04000000; { 486+ instruction }
  281. IF_PENT = $05000000; { Pentium instruction }
  282. IF_P6 = $06000000; { P6 instruction }
  283. IF_KATMAI = $07000000; { Katmai instructions }
  284. { Willamette instructions }
  285. IF_WILLAMETTE = $08000000;
  286. { Prescott instructions }
  287. IF_PRESCOTT = $09000000;
  288. IF_X86_64 = $0a000000;
  289. IF_CYRIX = $10000000; { Cyrix-specific instruction }
  290. IF_AMD = $20000000; { AMD-specific instruction }
  291. { added flags }
  292. IF_PRE = $40000000; { it's a prefix instruction }
  293. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  294. type
  295. TInsTabCache=array[TasmOp] of longint;
  296. PInsTabCache=^TInsTabCache;
  297. const
  298. {$ifdef x86_64}
  299. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  300. {$else x86_64}
  301. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  302. {$endif x86_64}
  303. var
  304. InsTabCache : PInsTabCache;
  305. const
  306. {$ifdef x86_64}
  307. { Intel style operands ! }
  308. opsize_2_type:array[0..2,topsize] of longint=(
  309. (OT_NONE,
  310. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  311. OT_BITS16,OT_BITS32,OT_BITS64,
  312. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  313. OT_BITS64,
  314. OT_NEAR,OT_FAR,OT_SHORT,
  315. OT_NONE,
  316. OT_NONE
  317. ),
  318. (OT_NONE,
  319. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  320. OT_BITS16,OT_BITS32,OT_BITS64,
  321. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  322. OT_BITS64,
  323. OT_NEAR,OT_FAR,OT_SHORT,
  324. OT_NONE,
  325. OT_NONE
  326. ),
  327. (OT_NONE,
  328. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  329. OT_BITS16,OT_BITS32,OT_BITS64,
  330. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  331. OT_BITS64,
  332. OT_NEAR,OT_FAR,OT_SHORT,
  333. OT_NONE,
  334. OT_NONE
  335. )
  336. );
  337. reg_ot_table : array[tregisterindex] of longint = (
  338. {$i r8664ot.inc}
  339. );
  340. {$else x86_64}
  341. { Intel style operands ! }
  342. opsize_2_type:array[0..2,topsize] of longint=(
  343. (OT_NONE,
  344. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  345. OT_BITS16,OT_BITS32,OT_BITS64,
  346. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  347. OT_BITS64,
  348. OT_NEAR,OT_FAR,OT_SHORT,
  349. OT_NONE,
  350. OT_NONE
  351. ),
  352. (OT_NONE,
  353. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  354. OT_BITS16,OT_BITS32,OT_BITS64,
  355. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  356. OT_BITS64,
  357. OT_NEAR,OT_FAR,OT_SHORT,
  358. OT_NONE,
  359. OT_NONE
  360. ),
  361. (OT_NONE,
  362. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  363. OT_BITS16,OT_BITS32,OT_BITS64,
  364. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  365. OT_BITS64,
  366. OT_NEAR,OT_FAR,OT_SHORT,
  367. OT_NONE,
  368. OT_NONE
  369. )
  370. );
  371. reg_ot_table : array[tregisterindex] of longint = (
  372. {$i r386ot.inc}
  373. );
  374. {$endif x86_64}
  375. { Operation type for spilling code }
  376. type
  377. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  378. var
  379. operation_type_table : ^toperation_type_table;
  380. {****************************************************************************
  381. TAI_ALIGN
  382. ****************************************************************************}
  383. constructor tai_align.create(b: byte);
  384. begin
  385. inherited create(b);
  386. reg:=NR_ECX;
  387. end;
  388. constructor tai_align.create_op(b: byte; _op: byte);
  389. begin
  390. inherited create_op(b,_op);
  391. reg:=NR_NO;
  392. end;
  393. function tai_align.calculatefillbuf(var buf : tfillbuffer):pchar;
  394. const
  395. alignarray:array[0..5] of string[8]=(
  396. #$8D#$B4#$26#$00#$00#$00#$00,
  397. #$8D#$B6#$00#$00#$00#$00,
  398. #$8D#$74#$26#$00,
  399. #$8D#$76#$00,
  400. #$89#$F6,
  401. #$90
  402. );
  403. var
  404. bufptr : pchar;
  405. j : longint;
  406. begin
  407. inherited calculatefillbuf(buf);
  408. if not use_op then
  409. begin
  410. bufptr:=pchar(@buf);
  411. while (fillsize>0) do
  412. begin
  413. for j:=0 to 5 do
  414. if (fillsize>=length(alignarray[j])) then
  415. break;
  416. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  417. inc(bufptr,length(alignarray[j]));
  418. dec(fillsize,length(alignarray[j]));
  419. end;
  420. end;
  421. calculatefillbuf:=pchar(@buf);
  422. end;
  423. {*****************************************************************************
  424. Taicpu Constructors
  425. *****************************************************************************}
  426. procedure taicpu.changeopsize(siz:topsize);
  427. begin
  428. opsize:=siz;
  429. end;
  430. procedure taicpu.init(_size : topsize);
  431. begin
  432. { default order is att }
  433. FOperandOrder:=op_att;
  434. segprefix:=NR_NO;
  435. opsize:=_size;
  436. {$ifndef NOAG386BIN}
  437. insentry:=nil;
  438. LastInsOffset:=-1;
  439. InsOffset:=0;
  440. InsSize:=0;
  441. {$endif}
  442. end;
  443. constructor taicpu.op_none(op : tasmop);
  444. begin
  445. inherited create(op);
  446. init(S_NO);
  447. end;
  448. constructor taicpu.op_none(op : tasmop;_size : topsize);
  449. begin
  450. inherited create(op);
  451. init(_size);
  452. end;
  453. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  454. begin
  455. inherited create(op);
  456. init(_size);
  457. ops:=1;
  458. loadreg(0,_op1);
  459. end;
  460. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  461. begin
  462. inherited create(op);
  463. init(_size);
  464. ops:=1;
  465. loadconst(0,_op1);
  466. end;
  467. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  468. begin
  469. inherited create(op);
  470. init(_size);
  471. ops:=1;
  472. loadref(0,_op1);
  473. end;
  474. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  475. begin
  476. inherited create(op);
  477. init(_size);
  478. ops:=2;
  479. loadreg(0,_op1);
  480. loadreg(1,_op2);
  481. end;
  482. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  483. begin
  484. inherited create(op);
  485. init(_size);
  486. ops:=2;
  487. loadreg(0,_op1);
  488. loadconst(1,_op2);
  489. end;
  490. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  491. begin
  492. inherited create(op);
  493. init(_size);
  494. ops:=2;
  495. loadreg(0,_op1);
  496. loadref(1,_op2);
  497. end;
  498. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  499. begin
  500. inherited create(op);
  501. init(_size);
  502. ops:=2;
  503. loadconst(0,_op1);
  504. loadreg(1,_op2);
  505. end;
  506. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  507. begin
  508. inherited create(op);
  509. init(_size);
  510. ops:=2;
  511. loadconst(0,_op1);
  512. loadconst(1,_op2);
  513. end;
  514. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  515. begin
  516. inherited create(op);
  517. init(_size);
  518. ops:=2;
  519. loadconst(0,_op1);
  520. loadref(1,_op2);
  521. end;
  522. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  523. begin
  524. inherited create(op);
  525. init(_size);
  526. ops:=2;
  527. loadref(0,_op1);
  528. loadreg(1,_op2);
  529. end;
  530. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  531. begin
  532. inherited create(op);
  533. init(_size);
  534. ops:=3;
  535. loadreg(0,_op1);
  536. loadreg(1,_op2);
  537. loadreg(2,_op3);
  538. end;
  539. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  540. begin
  541. inherited create(op);
  542. init(_size);
  543. ops:=3;
  544. loadconst(0,_op1);
  545. loadreg(1,_op2);
  546. loadreg(2,_op3);
  547. end;
  548. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  549. begin
  550. inherited create(op);
  551. init(_size);
  552. ops:=3;
  553. loadreg(0,_op1);
  554. loadreg(1,_op2);
  555. loadref(2,_op3);
  556. end;
  557. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  558. begin
  559. inherited create(op);
  560. init(_size);
  561. ops:=3;
  562. loadconst(0,_op1);
  563. loadref(1,_op2);
  564. loadreg(2,_op3);
  565. end;
  566. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  567. begin
  568. inherited create(op);
  569. init(_size);
  570. ops:=3;
  571. loadconst(0,_op1);
  572. loadreg(1,_op2);
  573. loadref(2,_op3);
  574. end;
  575. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  576. begin
  577. inherited create(op);
  578. init(_size);
  579. condition:=cond;
  580. ops:=1;
  581. loadsymbol(0,_op1,0);
  582. end;
  583. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  584. begin
  585. inherited create(op);
  586. init(_size);
  587. ops:=1;
  588. loadsymbol(0,_op1,0);
  589. end;
  590. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  591. begin
  592. inherited create(op);
  593. init(_size);
  594. ops:=1;
  595. loadsymbol(0,_op1,_op1ofs);
  596. end;
  597. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  598. begin
  599. inherited create(op);
  600. init(_size);
  601. ops:=2;
  602. loadsymbol(0,_op1,_op1ofs);
  603. loadreg(1,_op2);
  604. end;
  605. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  606. begin
  607. inherited create(op);
  608. init(_size);
  609. ops:=2;
  610. loadsymbol(0,_op1,_op1ofs);
  611. loadref(1,_op2);
  612. end;
  613. function taicpu.GetString:string;
  614. var
  615. i : longint;
  616. s : string;
  617. addsize : boolean;
  618. begin
  619. s:='['+std_op2str[opcode];
  620. for i:=0 to ops-1 do
  621. begin
  622. with oper[i]^ do
  623. begin
  624. if i=0 then
  625. s:=s+' '
  626. else
  627. s:=s+',';
  628. { type }
  629. addsize:=false;
  630. if (ot and OT_XMMREG)=OT_XMMREG then
  631. s:=s+'xmmreg'
  632. else
  633. if (ot and OT_MMXREG)=OT_MMXREG then
  634. s:=s+'mmxreg'
  635. else
  636. if (ot and OT_FPUREG)=OT_FPUREG then
  637. s:=s+'fpureg'
  638. else
  639. if (ot and OT_REGISTER)=OT_REGISTER then
  640. begin
  641. s:=s+'reg';
  642. addsize:=true;
  643. end
  644. else
  645. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  646. begin
  647. s:=s+'imm';
  648. addsize:=true;
  649. end
  650. else
  651. if (ot and OT_MEMORY)=OT_MEMORY then
  652. begin
  653. s:=s+'mem';
  654. addsize:=true;
  655. end
  656. else
  657. s:=s+'???';
  658. { size }
  659. if addsize then
  660. begin
  661. if (ot and OT_BITS8)<>0 then
  662. s:=s+'8'
  663. else
  664. if (ot and OT_BITS16)<>0 then
  665. s:=s+'16'
  666. else
  667. if (ot and OT_BITS32)<>0 then
  668. s:=s+'32'
  669. else
  670. s:=s+'??';
  671. { signed }
  672. if (ot and OT_SIGNED)<>0 then
  673. s:=s+'s';
  674. end;
  675. end;
  676. end;
  677. GetString:=s+']';
  678. end;
  679. procedure taicpu.Swapoperands;
  680. var
  681. p : POper;
  682. begin
  683. { Fix the operands which are in AT&T style and we need them in Intel style }
  684. case ops of
  685. 2 : begin
  686. { 0,1 -> 1,0 }
  687. p:=oper[0];
  688. oper[0]:=oper[1];
  689. oper[1]:=p;
  690. end;
  691. 3 : begin
  692. { 0,1,2 -> 2,1,0 }
  693. p:=oper[0];
  694. oper[0]:=oper[2];
  695. oper[2]:=p;
  696. end;
  697. end;
  698. end;
  699. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  700. begin
  701. if FOperandOrder<>order then
  702. begin
  703. Swapoperands;
  704. FOperandOrder:=order;
  705. end;
  706. end;
  707. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  708. begin
  709. o.typ:=toptype(ppufile.getbyte);
  710. o.ot:=ppufile.getlongint;
  711. case o.typ of
  712. top_reg :
  713. ppufile.getdata(o.reg,sizeof(Tregister));
  714. top_ref :
  715. begin
  716. new(o.ref);
  717. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  718. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  719. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  720. o.ref^.scalefactor:=ppufile.getbyte;
  721. o.ref^.offset:=ppufile.getaint;
  722. o.ref^.symbol:=ppufile.getasmsymbol;
  723. o.ref^.relsymbol:=ppufile.getasmsymbol;
  724. end;
  725. top_const :
  726. o.val:=ppufile.getaint;
  727. top_local :
  728. begin
  729. new(o.localoper);
  730. with o.localoper^ do
  731. begin
  732. ppufile.getderef(localsymderef);
  733. localsymofs:=ppufile.getaint;
  734. localindexreg:=tregister(ppufile.getlongint);
  735. localscale:=ppufile.getbyte;
  736. localgetoffset:=(ppufile.getbyte<>0);
  737. end;
  738. end;
  739. end;
  740. end;
  741. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  742. begin
  743. ppufile.putbyte(byte(o.typ));
  744. ppufile.putlongint(o.ot);
  745. case o.typ of
  746. top_reg :
  747. ppufile.putdata(o.reg,sizeof(Tregister));
  748. top_ref :
  749. begin
  750. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  751. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  752. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  753. ppufile.putbyte(o.ref^.scalefactor);
  754. ppufile.putaint(o.ref^.offset);
  755. ppufile.putasmsymbol(o.ref^.symbol);
  756. ppufile.putasmsymbol(o.ref^.relsymbol);
  757. end;
  758. top_const :
  759. ppufile.putaint(o.val);
  760. top_local :
  761. begin
  762. with o.localoper^ do
  763. begin
  764. ppufile.putderef(localsymderef);
  765. ppufile.putaint(localsymofs);
  766. ppufile.putlongint(longint(localindexreg));
  767. ppufile.putbyte(localscale);
  768. ppufile.putbyte(byte(localgetoffset));
  769. end;
  770. end;
  771. end;
  772. end;
  773. procedure taicpu.ppubuildderefimploper(var o:toper);
  774. begin
  775. case o.typ of
  776. top_local :
  777. o.localoper^.localsymderef.build(tlocalvarsym(o.localoper^.localsym));
  778. end;
  779. end;
  780. procedure taicpu.ppuderefoper(var o:toper);
  781. begin
  782. case o.typ of
  783. top_ref :
  784. begin
  785. if assigned(o.ref^.symbol) then
  786. objectlibrary.derefasmsymbol(o.ref^.symbol);
  787. if assigned(o.ref^.relsymbol) then
  788. objectlibrary.derefasmsymbol(o.ref^.relsymbol);
  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;
  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. begin
  865. if ops=0 then
  866. exit;
  867. { update oper[].ot field }
  868. for i:=0 to ops-1 do
  869. with oper[i]^ do
  870. begin
  871. case typ of
  872. top_reg :
  873. begin
  874. ot:=reg_ot_table[findreg_by_number(reg)];
  875. end;
  876. top_ref :
  877. begin
  878. if ref^.refaddr=addr_no then
  879. begin
  880. { create ot field }
  881. if (ot and OT_SIZE_MASK)=0 then
  882. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  883. else
  884. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  885. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  886. ot:=ot or OT_MEM_OFFS;
  887. { fix scalefactor }
  888. if (ref^.index=NR_NO) then
  889. ref^.scalefactor:=0
  890. else
  891. if (ref^.scalefactor=0) then
  892. ref^.scalefactor:=1;
  893. end
  894. else
  895. begin
  896. l:=ref^.offset;
  897. if assigned(ref^.symbol) then
  898. inc(l,ref^.symbol.address);
  899. { when it is a forward jump we need to compensate the
  900. offset of the instruction since the previous time,
  901. because the symbol address is then still using the
  902. 'old-style' addressing.
  903. For backwards jumps this is not required because the
  904. address of the symbol is already adjusted to the
  905. new offset }
  906. if (l>InsOffset) and (LastInsOffset<>-1) then
  907. inc(l,InsOffset-LastInsOffset);
  908. { instruction size will then always become 2 (PFV) }
  909. relsize:=(InsOffset+2)-l;
  910. if (not assigned(ref^.symbol) or
  911. ((ref^.symbol.currbind<>AB_EXTERNAL) and (ref^.symbol.address<>0))) and
  912. (relsize>=-128) and (relsize<=127) then
  913. ot:=OT_IMM32 or OT_SHORT
  914. else
  915. ot:=OT_IMM32 or OT_NEAR;
  916. end;
  917. end;
  918. top_local :
  919. begin
  920. if (ot and OT_SIZE_MASK)=0 then
  921. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  922. else
  923. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  924. end;
  925. top_const :
  926. begin
  927. if opsize=S_NO then
  928. message(asmr_e_invalid_opcode_and_operand);
  929. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  930. ot:=OT_IMM8 or OT_SIGNED
  931. else
  932. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  933. end;
  934. top_none :
  935. begin
  936. { generated when there was an error in the
  937. assembler reader. It never happends when generating
  938. assembler }
  939. end;
  940. else
  941. internalerror(200402261);
  942. end;
  943. end;
  944. end;
  945. function taicpu.InsEnd:longint;
  946. begin
  947. InsEnd:=InsOffset+InsSize;
  948. end;
  949. function taicpu.Matches(p:PInsEntry):longint;
  950. { * IF_SM stands for Size Match: any operand whose size is not
  951. * explicitly specified by the template is `really' intended to be
  952. * the same size as the first size-specified operand.
  953. * Non-specification is tolerated in the input instruction, but
  954. * _wrong_ specification is not.
  955. *
  956. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  957. * three-operand instructions such as SHLD: it implies that the
  958. * first two operands must match in size, but that the third is
  959. * required to be _unspecified_.
  960. *
  961. * IF_SB invokes Size Byte: operands with unspecified size in the
  962. * template are really bytes, and so no non-byte specification in
  963. * the input instruction will be tolerated. IF_SW similarly invokes
  964. * Size Word, and IF_SD invokes Size Doubleword.
  965. *
  966. * (The default state if neither IF_SM nor IF_SM2 is specified is
  967. * that any operand with unspecified size in the template is
  968. * required to have unspecified size in the instruction too...)
  969. }
  970. var
  971. i,j,asize,oprs : longint;
  972. siz : array[0..2] of longint;
  973. begin
  974. Matches:=100;
  975. { Check the opcode and operands }
  976. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  977. begin
  978. Matches:=0;
  979. exit;
  980. end;
  981. { Check that no spurious colons or TOs are present }
  982. for i:=0 to p^.ops-1 do
  983. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  984. begin
  985. Matches:=0;
  986. exit;
  987. end;
  988. { Check that the operand flags all match up }
  989. for i:=0 to p^.ops-1 do
  990. begin
  991. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  992. ((p^.optypes[i] and OT_SIZE_MASK) and
  993. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  994. begin
  995. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  996. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  997. begin
  998. Matches:=0;
  999. exit;
  1000. end
  1001. else
  1002. Matches:=1;
  1003. end;
  1004. end;
  1005. { Check operand sizes }
  1006. { as default an untyped size can get all the sizes, this is different
  1007. from nasm, but else we need to do a lot checking which opcodes want
  1008. size or not with the automatic size generation }
  1009. asize:=longint($ffffffff);
  1010. if (p^.flags and IF_SB)<>0 then
  1011. asize:=OT_BITS8
  1012. else if (p^.flags and IF_SW)<>0 then
  1013. asize:=OT_BITS16
  1014. else if (p^.flags and IF_SD)<>0 then
  1015. asize:=OT_BITS32;
  1016. if (p^.flags and IF_ARMASK)<>0 then
  1017. begin
  1018. siz[0]:=0;
  1019. siz[1]:=0;
  1020. siz[2]:=0;
  1021. if (p^.flags and IF_AR0)<>0 then
  1022. siz[0]:=asize
  1023. else if (p^.flags and IF_AR1)<>0 then
  1024. siz[1]:=asize
  1025. else if (p^.flags and IF_AR2)<>0 then
  1026. siz[2]:=asize;
  1027. end
  1028. else
  1029. begin
  1030. { we can leave because the size for all operands is forced to be
  1031. the same
  1032. but not if IF_SB IF_SW or IF_SD is set PM }
  1033. if asize=-1 then
  1034. exit;
  1035. siz[0]:=asize;
  1036. siz[1]:=asize;
  1037. siz[2]:=asize;
  1038. end;
  1039. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1040. begin
  1041. if (p^.flags and IF_SM2)<>0 then
  1042. oprs:=2
  1043. else
  1044. oprs:=p^.ops;
  1045. for i:=0 to oprs-1 do
  1046. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1047. begin
  1048. for j:=0 to oprs-1 do
  1049. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1050. break;
  1051. end;
  1052. end
  1053. else
  1054. oprs:=2;
  1055. { Check operand sizes }
  1056. for i:=0 to p^.ops-1 do
  1057. begin
  1058. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1059. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1060. { Immediates can always include smaller size }
  1061. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1062. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1063. Matches:=2;
  1064. end;
  1065. end;
  1066. procedure taicpu.ResetPass1;
  1067. begin
  1068. { we need to reset everything here, because the choosen insentry
  1069. can be invalid for a new situation where the previously optimized
  1070. insentry is not correct }
  1071. InsEntry:=nil;
  1072. InsSize:=0;
  1073. LastInsOffset:=-1;
  1074. end;
  1075. procedure taicpu.ResetPass2;
  1076. begin
  1077. { we are here in a second pass, check if the instruction can be optimized }
  1078. if assigned(InsEntry) and
  1079. ((InsEntry^.flags and IF_PASS2)<>0) then
  1080. begin
  1081. InsEntry:=nil;
  1082. InsSize:=0;
  1083. end;
  1084. LastInsOffset:=-1;
  1085. end;
  1086. function taicpu.CheckIfValid:boolean;
  1087. begin
  1088. result:=FindInsEntry;
  1089. end;
  1090. function taicpu.FindInsentry:boolean;
  1091. var
  1092. i : longint;
  1093. begin
  1094. result:=false;
  1095. { Things which may only be done once, not when a second pass is done to
  1096. optimize }
  1097. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1098. begin
  1099. { We need intel style operands }
  1100. SetOperandOrder(op_intel);
  1101. { create the .ot fields }
  1102. create_ot;
  1103. { set the file postion }
  1104. aktfilepos:=fileinfo;
  1105. end
  1106. else
  1107. begin
  1108. { we've already an insentry so it's valid }
  1109. result:=true;
  1110. exit;
  1111. end;
  1112. { Lookup opcode in the table }
  1113. InsSize:=-1;
  1114. i:=instabcache^[opcode];
  1115. if i=-1 then
  1116. begin
  1117. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1118. exit;
  1119. end;
  1120. insentry:=@instab[i];
  1121. while (insentry^.opcode=opcode) do
  1122. begin
  1123. if matches(insentry)=100 then
  1124. begin
  1125. result:=true;
  1126. exit;
  1127. end;
  1128. inc(i);
  1129. insentry:=@instab[i];
  1130. end;
  1131. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1132. { No instruction found, set insentry to nil and inssize to -1 }
  1133. insentry:=nil;
  1134. inssize:=-1;
  1135. end;
  1136. function taicpu.Pass1(offset:longint):longint;
  1137. begin
  1138. Pass1:=0;
  1139. { Save the old offset and set the new offset }
  1140. InsOffset:=Offset;
  1141. { Error? }
  1142. if (Insentry=nil) and (InsSize=-1) then
  1143. exit;
  1144. { set the file postion }
  1145. aktfilepos:=fileinfo;
  1146. { Get InsEntry }
  1147. if FindInsEntry then
  1148. begin
  1149. { Calculate instruction size }
  1150. InsSize:=calcsize(insentry);
  1151. if segprefix<>NR_NO then
  1152. inc(InsSize);
  1153. { Fix opsize if size if forced }
  1154. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1155. begin
  1156. if (insentry^.flags and IF_ARMASK)=0 then
  1157. begin
  1158. if (insentry^.flags and IF_SB)<>0 then
  1159. begin
  1160. if opsize=S_NO then
  1161. opsize:=S_B;
  1162. end
  1163. else if (insentry^.flags and IF_SW)<>0 then
  1164. begin
  1165. if opsize=S_NO then
  1166. opsize:=S_W;
  1167. end
  1168. else if (insentry^.flags and IF_SD)<>0 then
  1169. begin
  1170. if opsize=S_NO then
  1171. opsize:=S_L;
  1172. end;
  1173. end;
  1174. end;
  1175. LastInsOffset:=InsOffset;
  1176. Pass1:=InsSize;
  1177. exit;
  1178. end;
  1179. LastInsOffset:=-1;
  1180. end;
  1181. procedure taicpu.Pass2(objdata:TAsmObjectData);
  1182. var
  1183. c : longint;
  1184. begin
  1185. { error in pass1 ? }
  1186. if insentry=nil then
  1187. exit;
  1188. aktfilepos:=fileinfo;
  1189. { Segment override }
  1190. if (segprefix<>NR_NO) then
  1191. begin
  1192. case segprefix of
  1193. NR_CS : c:=$2e;
  1194. NR_DS : c:=$3e;
  1195. NR_ES : c:=$26;
  1196. NR_FS : c:=$64;
  1197. NR_GS : c:=$65;
  1198. NR_SS : c:=$36;
  1199. end;
  1200. objdata.writebytes(c,1);
  1201. { fix the offset for GenNode }
  1202. inc(InsOffset);
  1203. end;
  1204. { Generate the instruction }
  1205. GenCode(objdata);
  1206. end;
  1207. function taicpu.needaddrprefix(opidx:byte):boolean;
  1208. begin
  1209. result:=(oper[opidx]^.typ=top_ref) and
  1210. (oper[opidx]^.ref^.refaddr=addr_no) and
  1211. (
  1212. (
  1213. (oper[opidx]^.ref^.index<>NR_NO) and
  1214. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBD)
  1215. ) or
  1216. (
  1217. (oper[opidx]^.ref^.base<>NR_NO) and
  1218. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBD)
  1219. )
  1220. );
  1221. end;
  1222. function regval(r:Tregister):byte;
  1223. const
  1224. {$ifdef x86_64}
  1225. opcode_table:array[tregisterindex] of tregisterindex = (
  1226. {$i r8664op.inc}
  1227. );
  1228. {$else x86_64}
  1229. opcode_table:array[tregisterindex] of tregisterindex = (
  1230. {$i r386op.inc}
  1231. );
  1232. {$endif x86_64}
  1233. var
  1234. regidx : tregisterindex;
  1235. begin
  1236. regidx:=findreg_by_number(r);
  1237. if regidx<>0 then
  1238. result:=opcode_table[regidx]
  1239. else
  1240. begin
  1241. Message1(asmw_e_invalid_register,generic_regname(r));
  1242. result:=0;
  1243. end;
  1244. end;
  1245. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1246. var
  1247. sym : tasmsymbol;
  1248. md,s,rv : byte;
  1249. base,index,scalefactor,
  1250. o : longint;
  1251. ir,br : Tregister;
  1252. isub,bsub : tsubregister;
  1253. begin
  1254. process_ea:=false;
  1255. {Register ?}
  1256. if (input.typ=top_reg) then
  1257. begin
  1258. rv:=regval(input.reg);
  1259. output.sib_present:=false;
  1260. output.bytes:=0;
  1261. output.modrm:=$c0 or (rfield shl 3) or rv;
  1262. output.size:=1;
  1263. process_ea:=true;
  1264. exit;
  1265. end;
  1266. {No register, so memory reference.}
  1267. if (input.typ<>top_ref) then
  1268. internalerror(200409262);
  1269. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1270. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1271. internalerror(200301081);
  1272. ir:=input.ref^.index;
  1273. br:=input.ref^.base;
  1274. isub:=getsubreg(ir);
  1275. bsub:=getsubreg(br);
  1276. s:=input.ref^.scalefactor;
  1277. o:=input.ref^.offset;
  1278. sym:=input.ref^.symbol;
  1279. { it's direct address }
  1280. if (br=NR_NO) and (ir=NR_NO) then
  1281. begin
  1282. { it's a pure offset }
  1283. output.sib_present:=false;
  1284. output.bytes:=4;
  1285. output.modrm:=5 or (rfield shl 3);
  1286. end
  1287. else
  1288. { it's an indirection }
  1289. begin
  1290. { 16 bit address? }
  1291. if ((ir<>NR_NO) and (isub<>R_SUBD)) or
  1292. ((br<>NR_NO) and (bsub<>R_SUBD)) then
  1293. message(asmw_e_16bit_not_supported);
  1294. {$ifdef OPTEA}
  1295. { make single reg base }
  1296. if (br=NR_NO) and (s=1) then
  1297. begin
  1298. br:=ir;
  1299. ir:=NR_NO;
  1300. end;
  1301. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1302. if (br=NR_NO) and
  1303. (((s=2) and (ir<>NR_ESP)) or
  1304. (s=3) or (s=5) or (s=9)) then
  1305. begin
  1306. br:=ir;
  1307. dec(s);
  1308. end;
  1309. { swap ESP into base if scalefactor is 1 }
  1310. if (s=1) and (ir=NR_ESP) then
  1311. begin
  1312. ir:=br;
  1313. br:=NR_ESP;
  1314. end;
  1315. {$endif OPTEA}
  1316. { wrong, for various reasons }
  1317. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1318. exit;
  1319. { base }
  1320. case br of
  1321. NR_EAX : base:=0;
  1322. NR_ECX : base:=1;
  1323. NR_EDX : base:=2;
  1324. NR_EBX : base:=3;
  1325. NR_ESP : base:=4;
  1326. NR_NO,
  1327. NR_EBP : base:=5;
  1328. NR_ESI : base:=6;
  1329. NR_EDI : base:=7;
  1330. else
  1331. exit;
  1332. end;
  1333. { index }
  1334. case ir of
  1335. NR_EAX : index:=0;
  1336. NR_ECX : index:=1;
  1337. NR_EDX : index:=2;
  1338. NR_EBX : index:=3;
  1339. NR_NO : index:=4;
  1340. NR_EBP : index:=5;
  1341. NR_ESI : index:=6;
  1342. NR_EDI : index:=7;
  1343. else
  1344. exit;
  1345. end;
  1346. case s of
  1347. 0,
  1348. 1 : scalefactor:=0;
  1349. 2 : scalefactor:=1;
  1350. 4 : scalefactor:=2;
  1351. 8 : scalefactor:=3;
  1352. else
  1353. exit;
  1354. end;
  1355. if (br=NR_NO) or
  1356. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1357. md:=0
  1358. else
  1359. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1360. md:=1
  1361. else
  1362. md:=2;
  1363. if (br=NR_NO) or (md=2) then
  1364. output.bytes:=4
  1365. else
  1366. output.bytes:=md;
  1367. { SIB needed ? }
  1368. if (ir=NR_NO) and (br<>NR_ESP) then
  1369. begin
  1370. output.sib_present:=false;
  1371. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1372. end
  1373. else
  1374. begin
  1375. output.sib_present:=true;
  1376. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1377. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1378. end;
  1379. end;
  1380. if output.sib_present then
  1381. output.size:=2+output.bytes
  1382. else
  1383. output.size:=1+output.bytes;
  1384. process_ea:=true;
  1385. end;
  1386. function taicpu.calcsize(p:PInsEntry):shortint;
  1387. var
  1388. codes : pchar;
  1389. c : byte;
  1390. len : shortint;
  1391. ea_data : ea;
  1392. begin
  1393. len:=0;
  1394. codes:=@p^.code;
  1395. repeat
  1396. c:=ord(codes^);
  1397. inc(codes);
  1398. case c of
  1399. 0 :
  1400. break;
  1401. 1,2,3 :
  1402. begin
  1403. inc(codes,c);
  1404. inc(len,c);
  1405. end;
  1406. 8,9,10 :
  1407. begin
  1408. inc(codes);
  1409. inc(len);
  1410. end;
  1411. 4,5,6,7 :
  1412. begin
  1413. if opsize=S_W then
  1414. inc(len,2)
  1415. else
  1416. inc(len);
  1417. end;
  1418. 15,
  1419. 12,13,14,
  1420. 16,17,18,
  1421. 20,21,22,
  1422. 40,41,42 :
  1423. inc(len);
  1424. 24,25,26,
  1425. 31,
  1426. 48,49,50 :
  1427. inc(len,2);
  1428. 28,29,30, { we don't have 16 bit immediates code }
  1429. 32,33,34,
  1430. 52,53,54,
  1431. 56,57,58 :
  1432. inc(len,4);
  1433. 192,193,194 :
  1434. if NeedAddrPrefix(c-192) then
  1435. inc(len);
  1436. 208,
  1437. 210 :
  1438. inc(len);
  1439. 200,
  1440. 201,
  1441. 202,
  1442. 209,
  1443. 211,
  1444. 217,218: ;
  1445. 219,220 :
  1446. inc(len);
  1447. 216 :
  1448. begin
  1449. inc(codes);
  1450. inc(len);
  1451. end;
  1452. 224,225,226 :
  1453. begin
  1454. InternalError(777002);
  1455. end;
  1456. else
  1457. begin
  1458. if (c>=64) and (c<=191) then
  1459. begin
  1460. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1461. Message(asmw_e_invalid_effective_address)
  1462. else
  1463. inc(len,ea_data.size);
  1464. end
  1465. else
  1466. InternalError(777003);
  1467. end;
  1468. end;
  1469. until false;
  1470. calcsize:=len;
  1471. end;
  1472. procedure taicpu.GenCode(objdata:TAsmObjectData);
  1473. {
  1474. * the actual codes (C syntax, i.e. octal):
  1475. * \0 - terminates the code. (Unless it's a literal of course.)
  1476. * \1, \2, \3 - that many literal bytes follow in the code stream
  1477. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1478. * (POP is never used for CS) depending on operand 0
  1479. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1480. * on operand 0
  1481. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1482. * to the register value of operand 0, 1 or 2
  1483. * \17 - encodes the literal byte 0. (Some compilers don't take
  1484. * kindly to a zero byte in the _middle_ of a compile time
  1485. * string constant, so I had to put this hack in.)
  1486. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1487. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1488. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1489. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1490. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1491. * assembly mode or the address-size override on the operand
  1492. * \37 - a word constant, from the _segment_ part of operand 0
  1493. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1494. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1495. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1496. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1497. * assembly mode or the address-size override on the operand
  1498. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1499. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1500. * field the register value of operand b.
  1501. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1502. * field equal to digit b.
  1503. * \30x - might be an 0x67 byte, depending on the address size of
  1504. * the memory reference in operand x.
  1505. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1506. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1507. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1508. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1509. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1510. * \322 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1511. * \323 - indicates that this instruction is only valid when the
  1512. * operand size is the default (instruction to disassembler,
  1513. * generates no code in the assembler)
  1514. * \330 - a literal byte follows in the code stream, to be added
  1515. * to the condition code value of the instruction.
  1516. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1517. * Operand 0 had better be a segmentless constant.
  1518. }
  1519. var
  1520. currval : longint;
  1521. currsym : tasmsymbol;
  1522. procedure getvalsym(opidx:longint);
  1523. begin
  1524. case oper[opidx]^.typ of
  1525. top_ref :
  1526. begin
  1527. currval:=oper[opidx]^.ref^.offset;
  1528. currsym:=oper[opidx]^.ref^.symbol;
  1529. end;
  1530. top_const :
  1531. begin
  1532. currval:=longint(oper[opidx]^.val);
  1533. currsym:=nil;
  1534. end;
  1535. else
  1536. Message(asmw_e_immediate_or_reference_expected);
  1537. end;
  1538. end;
  1539. const
  1540. CondVal:array[TAsmCond] of byte=($0,
  1541. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1542. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1543. $0, $A, $A, $B, $8, $4);
  1544. var
  1545. c : byte;
  1546. pb,
  1547. codes : pchar;
  1548. bytes : array[0..3] of byte;
  1549. rfield,
  1550. data,s,opidx : longint;
  1551. ea_data : ea;
  1552. begin
  1553. {$ifdef EXTDEBUG}
  1554. { safety check }
  1555. if objdata.currsec.datasize<>insoffset then
  1556. internalerror(200130121);
  1557. {$endif EXTDEBUG}
  1558. { load data to write }
  1559. codes:=insentry^.code;
  1560. { Force word push/pop for registers }
  1561. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1562. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1563. begin
  1564. bytes[0]:=$66;
  1565. objdata.writebytes(bytes,1);
  1566. end;
  1567. repeat
  1568. c:=ord(codes^);
  1569. inc(codes);
  1570. case c of
  1571. 0 :
  1572. break;
  1573. 1,2,3 :
  1574. begin
  1575. objdata.writebytes(codes^,c);
  1576. inc(codes,c);
  1577. end;
  1578. 4,6 :
  1579. begin
  1580. case oper[0]^.reg of
  1581. NR_CS:
  1582. bytes[0]:=$e;
  1583. NR_NO,
  1584. NR_DS:
  1585. bytes[0]:=$1e;
  1586. NR_ES:
  1587. bytes[0]:=$6;
  1588. NR_SS:
  1589. bytes[0]:=$16;
  1590. else
  1591. internalerror(777004);
  1592. end;
  1593. if c=4 then
  1594. inc(bytes[0]);
  1595. objdata.writebytes(bytes,1);
  1596. end;
  1597. 5,7 :
  1598. begin
  1599. case oper[0]^.reg of
  1600. NR_FS:
  1601. bytes[0]:=$a0;
  1602. NR_GS:
  1603. bytes[0]:=$a8;
  1604. else
  1605. internalerror(777005);
  1606. end;
  1607. if c=5 then
  1608. inc(bytes[0]);
  1609. objdata.writebytes(bytes,1);
  1610. end;
  1611. 8,9,10 :
  1612. begin
  1613. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1614. inc(codes);
  1615. objdata.writebytes(bytes,1);
  1616. end;
  1617. 15 :
  1618. begin
  1619. bytes[0]:=0;
  1620. objdata.writebytes(bytes,1);
  1621. end;
  1622. 12,13,14 :
  1623. begin
  1624. getvalsym(c-12);
  1625. if (currval<-128) or (currval>127) then
  1626. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1627. if assigned(currsym) then
  1628. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1629. else
  1630. objdata.writebytes(currval,1);
  1631. end;
  1632. 16,17,18 :
  1633. begin
  1634. getvalsym(c-16);
  1635. if (currval<-256) or (currval>255) then
  1636. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1637. if assigned(currsym) then
  1638. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1639. else
  1640. objdata.writebytes(currval,1);
  1641. end;
  1642. 20,21,22 :
  1643. begin
  1644. getvalsym(c-20);
  1645. if (currval<0) or (currval>255) then
  1646. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1647. if assigned(currsym) then
  1648. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1649. else
  1650. objdata.writebytes(currval,1);
  1651. end;
  1652. 24,25,26 :
  1653. begin
  1654. getvalsym(c-24);
  1655. if (currval<-65536) or (currval>65535) then
  1656. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1657. if assigned(currsym) then
  1658. objdata.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1659. else
  1660. objdata.writebytes(currval,2);
  1661. end;
  1662. 28,29,30 :
  1663. begin
  1664. getvalsym(c-28);
  1665. if assigned(currsym) then
  1666. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1667. else
  1668. objdata.writebytes(currval,4);
  1669. end;
  1670. 32,33,34 :
  1671. begin
  1672. getvalsym(c-32);
  1673. if assigned(currsym) then
  1674. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1675. else
  1676. objdata.writebytes(currval,4);
  1677. end;
  1678. 40,41,42 :
  1679. begin
  1680. getvalsym(c-40);
  1681. data:=currval-insend;
  1682. if assigned(currsym) then
  1683. inc(data,currsym.address);
  1684. if (data>127) or (data<-128) then
  1685. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1686. objdata.writebytes(data,1);
  1687. end;
  1688. 52,53,54 :
  1689. begin
  1690. getvalsym(c-52);
  1691. if assigned(currsym) then
  1692. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1693. else
  1694. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1695. end;
  1696. 56,57,58 :
  1697. begin
  1698. getvalsym(c-56);
  1699. if assigned(currsym) then
  1700. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1701. else
  1702. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1703. end;
  1704. 192,193,194 :
  1705. begin
  1706. if NeedAddrPrefix(c-192) then
  1707. begin
  1708. bytes[0]:=$67;
  1709. objdata.writebytes(bytes,1);
  1710. end;
  1711. end;
  1712. 200 :
  1713. begin
  1714. bytes[0]:=$67;
  1715. objdata.writebytes(bytes,1);
  1716. end;
  1717. 208 :
  1718. begin
  1719. bytes[0]:=$66;
  1720. objdata.writebytes(bytes,1);
  1721. end;
  1722. 210 :
  1723. begin
  1724. bytes[0]:=$48;
  1725. objdata.writebytes(bytes,1);
  1726. end;
  1727. 216 :
  1728. begin
  1729. bytes[0]:=ord(codes^)+condval[condition];
  1730. inc(codes);
  1731. objdata.writebytes(bytes,1);
  1732. end;
  1733. 201,
  1734. 202,
  1735. 209,
  1736. 211,
  1737. 217,218 :
  1738. begin
  1739. { these are dissambler hints or 32 bit prefixes which
  1740. are not needed }
  1741. end;
  1742. 219 :
  1743. begin
  1744. bytes[0]:=$f3;
  1745. objdata.writebytes(bytes,1);
  1746. end;
  1747. 220 :
  1748. begin
  1749. bytes[0]:=$f2;
  1750. objdata.writebytes(bytes,1);
  1751. end;
  1752. 31,
  1753. 48,49,50,
  1754. 224,225,226 :
  1755. begin
  1756. InternalError(777006);
  1757. end
  1758. else
  1759. begin
  1760. if (c>=64) and (c<=191) then
  1761. begin
  1762. if (c<127) then
  1763. begin
  1764. if (oper[c and 7]^.typ=top_reg) then
  1765. rfield:=regval(oper[c and 7]^.reg)
  1766. else
  1767. rfield:=regval(oper[c and 7]^.ref^.base);
  1768. end
  1769. else
  1770. rfield:=c and 7;
  1771. opidx:=(c shr 3) and 7;
  1772. if not process_ea(oper[opidx]^,ea_data,rfield) then
  1773. Message(asmw_e_invalid_effective_address);
  1774. pb:=@bytes;
  1775. pb^:=chr(ea_data.modrm);
  1776. inc(pb);
  1777. if ea_data.sib_present then
  1778. begin
  1779. pb^:=chr(ea_data.sib);
  1780. inc(pb);
  1781. end;
  1782. s:=pb-pchar(@bytes);
  1783. objdata.writebytes(bytes,s);
  1784. case ea_data.bytes of
  1785. 0 : ;
  1786. 1 :
  1787. begin
  1788. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  1789. objdata.writereloc(oper[opidx]^.ref^.offset,1,oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE)
  1790. else
  1791. begin
  1792. bytes[0]:=oper[opidx]^.ref^.offset;
  1793. objdata.writebytes(bytes,1);
  1794. end;
  1795. inc(s);
  1796. end;
  1797. 2,4 :
  1798. begin
  1799. objdata.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,
  1800. oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE);
  1801. inc(s,ea_data.bytes);
  1802. end;
  1803. end;
  1804. end
  1805. else
  1806. InternalError(777007);
  1807. end;
  1808. end;
  1809. until false;
  1810. end;
  1811. {$endif NOAG386BIN}
  1812. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1813. begin
  1814. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  1815. (regtype = R_INTREGISTER) and
  1816. (ops=2) and
  1817. (oper[0]^.typ=top_reg) and
  1818. (oper[1]^.typ=top_reg) and
  1819. (oper[0]^.reg=oper[1]^.reg)
  1820. ) or
  1821. (((opcode=A_MOVSS) or (opcode=A_MOVSD)) and
  1822. (regtype = R_MMREGISTER) and
  1823. (ops=2) and
  1824. (oper[0]^.typ=top_reg) and
  1825. (oper[1]^.typ=top_reg) and
  1826. (oper[0]^.reg=oper[1]^.reg)
  1827. );
  1828. end;
  1829. procedure build_spilling_operation_type_table;
  1830. var
  1831. opcode : tasmop;
  1832. i : integer;
  1833. begin
  1834. new(operation_type_table);
  1835. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  1836. for opcode:=low(tasmop) to high(tasmop) do
  1837. begin
  1838. for i:=1 to MaxInsChanges do
  1839. begin
  1840. case InsProp[opcode].Ch[i] of
  1841. Ch_Rop1 :
  1842. operation_type_table^[opcode,0]:=operand_read;
  1843. Ch_Wop1 :
  1844. operation_type_table^[opcode,0]:=operand_write;
  1845. Ch_RWop1,
  1846. Ch_Mop1 :
  1847. operation_type_table^[opcode,0]:=operand_readwrite;
  1848. Ch_Rop2 :
  1849. operation_type_table^[opcode,1]:=operand_read;
  1850. Ch_Wop2 :
  1851. operation_type_table^[opcode,1]:=operand_write;
  1852. Ch_RWop2,
  1853. Ch_Mop2 :
  1854. operation_type_table^[opcode,1]:=operand_readwrite;
  1855. Ch_Rop3 :
  1856. operation_type_table^[opcode,2]:=operand_read;
  1857. Ch_Wop3 :
  1858. operation_type_table^[opcode,2]:=operand_write;
  1859. Ch_RWop3,
  1860. Ch_Mop3 :
  1861. operation_type_table^[opcode,2]:=operand_readwrite;
  1862. end;
  1863. end;
  1864. end;
  1865. { Special cases that can't be decoded from the InsChanges flags }
  1866. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  1867. end;
  1868. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  1869. begin
  1870. { the information in the instruction table is made for the string copy
  1871. operation MOVSD so hack here (FK)
  1872. }
  1873. if (opcode=A_MOVSD) and (ops=2) then
  1874. begin
  1875. case opnr of
  1876. 0:
  1877. result:=operand_read;
  1878. 1:
  1879. result:=operand_write;
  1880. else
  1881. internalerror(200506055);
  1882. end
  1883. end
  1884. else
  1885. result:=operation_type_table^[opcode,opnr];
  1886. end;
  1887. function spilling_create_load(const ref:treference;r:tregister): tai;
  1888. begin
  1889. case getregtype(r) of
  1890. R_INTREGISTER :
  1891. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
  1892. R_MMREGISTER :
  1893. case getsubreg(r) of
  1894. R_SUBMMD:
  1895. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  1896. R_SUBMMS:
  1897. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  1898. else
  1899. internalerror(200506043);
  1900. end;
  1901. else
  1902. internalerror(200401041);
  1903. end;
  1904. end;
  1905. function spilling_create_store(r:tregister; const ref:treference): tai;
  1906. begin
  1907. case getregtype(r) of
  1908. R_INTREGISTER :
  1909. result:=taicpu.op_reg_ref(A_MOV,reg2opsize(r),r,ref);
  1910. R_MMREGISTER :
  1911. case getsubreg(r) of
  1912. R_SUBMMD:
  1913. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  1914. R_SUBMMS:
  1915. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  1916. else
  1917. internalerror(200506042);
  1918. end;
  1919. else
  1920. internalerror(200401041);
  1921. end;
  1922. end;
  1923. {*****************************************************************************
  1924. Instruction table
  1925. *****************************************************************************}
  1926. procedure BuildInsTabCache;
  1927. {$ifndef NOAG386BIN}
  1928. var
  1929. i : longint;
  1930. {$endif}
  1931. begin
  1932. {$ifndef NOAG386BIN}
  1933. new(instabcache);
  1934. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1935. i:=0;
  1936. while (i<InsTabEntries) do
  1937. begin
  1938. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1939. InsTabCache^[InsTab[i].OPcode]:=i;
  1940. inc(i);
  1941. end;
  1942. {$endif NOAG386BIN}
  1943. end;
  1944. procedure InitAsm;
  1945. begin
  1946. build_spilling_operation_type_table;
  1947. {$ifndef NOAG386BIN}
  1948. if not assigned(instabcache) then
  1949. BuildInsTabCache;
  1950. {$endif NOAG386BIN}
  1951. end;
  1952. procedure DoneAsm;
  1953. begin
  1954. if assigned(operation_type_table) then
  1955. begin
  1956. dispose(operation_type_table);
  1957. operation_type_table:=nil;
  1958. end;
  1959. {$ifndef NOAG386BIN}
  1960. if assigned(instabcache) then
  1961. begin
  1962. dispose(instabcache);
  1963. instabcache:=nil;
  1964. end;
  1965. {$endif NOAG386BIN}
  1966. end;
  1967. begin
  1968. cai_align:=tai_align;
  1969. cai_cpu:=taicpu;
  1970. end.