aasmcpu.pas 69 KB

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