aasmcpu.pas 68 KB

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