aasmcpu.pas 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095
  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,globals,verbose,
  26. cpuinfo,cpubase,
  27. symppu,
  28. aasmbase,aasmtai;
  29. const
  30. { Operand types }
  31. OT_NONE = $00000000;
  32. OT_BITS8 = $00000001; { size, and other attributes, of the operand }
  33. OT_BITS16 = $00000002;
  34. OT_BITS32 = $00000004;
  35. OT_BITS64 = $00000008; { FPU only }
  36. OT_BITS80 = $00000010;
  37. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  38. OT_NEAR = $00000040;
  39. OT_SHORT = $00000080;
  40. OT_SIZE_MASK = $000000FF; { all the size attributes }
  41. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  42. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  43. OT_TO = $00000200; { operand is followed by a colon }
  44. { reverse effect in FADD, FSUB &c }
  45. OT_COLON = $00000400;
  46. OT_REGISTER = $00001000;
  47. OT_IMMEDIATE = $00002000;
  48. OT_IMM8 = $00002001;
  49. OT_IMM16 = $00002002;
  50. OT_IMM32 = $00002004;
  51. OT_IMM64 = $00002008;
  52. OT_IMM80 = $00002010;
  53. OT_REGMEM = $00200000; { for r/m, ie EA, operands }
  54. OT_REGNORM = $00201000; { 'normal' reg, qualifies as EA }
  55. OT_REG8 = $00201001;
  56. OT_REG16 = $00201002;
  57. OT_REG32 = $00201004;
  58. {$ifdef x86_64}
  59. OT_REG64 = $00201008;
  60. {$endif x86_64}
  61. OT_MMXREG = $00201008; { MMX registers }
  62. OT_XMMREG = $00201010; { Katmai registers }
  63. OT_MEMORY = $00204000; { register number in 'basereg' }
  64. OT_MEM8 = $00204001;
  65. OT_MEM16 = $00204002;
  66. OT_MEM32 = $00204004;
  67. OT_MEM64 = $00204008;
  68. OT_MEM80 = $00204010;
  69. OT_FPUREG = $01000000; { floating point stack registers }
  70. OT_FPU0 = $01000800; { FPU stack register zero }
  71. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  72. { a mask for the following }
  73. OT_REG_ACCUM = $00211000; { accumulator: AL, AX or EAX }
  74. OT_REG_AL = $00211001; { REG_ACCUM | BITSxx }
  75. OT_REG_AX = $00211002; { ditto }
  76. OT_REG_EAX = $00211004; { and again }
  77. OT_REG_COUNT = $00221000; { counter: CL, CX or ECX }
  78. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  79. OT_REG_CX = $00221002; { ditto }
  80. OT_REG_ECX = $00221004; { another one }
  81. OT_REG_DX = $00241002;
  82. OT_REG_SREG = $00081002; { any segment register }
  83. OT_REG_CS = $01081002; { CS }
  84. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  85. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  86. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  87. OT_REG_CREG = $08101004; { CRn }
  88. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  89. OT_REG_DREG = $10101004; { DRn }
  90. OT_REG_TREG = $20101004; { TRn }
  91. OT_MEM_OFFS = $00604000; { special type of EA }
  92. { simple [address] offset }
  93. OT_ONENESS = $00800000; { special type of immediate operand }
  94. { so UNITY == IMMEDIATE | ONENESS }
  95. OT_UNITY = $00802000; { for shift/rotate instructions }
  96. { Size of the instruction table converted by nasmconv.pas }
  97. {$ifdef x86_64}
  98. instabentries = {$i x86_64no.inc}
  99. {$else x86_64}
  100. instabentries = {$i i386nop.inc}
  101. {$endif x86_64}
  102. maxinfolen = 8;
  103. type
  104. TOperandOrder = (op_intel,op_att);
  105. tinsentry=packed record
  106. opcode : tasmop;
  107. ops : byte;
  108. optypes : array[0..2] of longint;
  109. code : array[0..maxinfolen] of char;
  110. flags : longint;
  111. end;
  112. pinsentry=^tinsentry;
  113. { alignment for operator }
  114. tai_align = class(tai_align_abstract)
  115. reg : tregister;
  116. constructor create(b:byte);
  117. constructor create_op(b: byte; _op: byte);
  118. function calculatefillbuf(var buf : tfillbuffer):pchar;override;
  119. end;
  120. taicpu = class(taicpu_abstract)
  121. opsize : topsize;
  122. constructor op_none(op : tasmop;_size : topsize);
  123. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  124. constructor op_const(op : tasmop;_size : topsize;_op1 : aword);
  125. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  126. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  127. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  128. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aword);
  129. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister);
  130. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aword);
  131. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference);
  132. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  133. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  134. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;_op3 : tregister);
  135. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference;_op3 : tregister);
  136. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  137. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;const _op3 : treference);
  138. { this is for Jmp instructions }
  139. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  140. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  141. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  142. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  143. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  144. procedure changeopsize(siz:topsize);
  145. function GetString:string;
  146. procedure CheckNonCommutativeOpcodes;
  147. private
  148. FOperandOrder : TOperandOrder;
  149. procedure init(_size : topsize); { this need to be called by all constructor }
  150. {$ifndef NOAG386BIN}
  151. public
  152. { the next will reset all instructions that can change in pass 2 }
  153. procedure ResetPass1;
  154. procedure ResetPass2;
  155. function CheckIfValid:boolean;
  156. function Pass1(offset:longint):longint;virtual;
  157. procedure Pass2(sec:TAsmObjectdata);virtual;
  158. procedure SetOperandOrder(order:TOperandOrder);
  159. protected
  160. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  161. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  162. procedure ppuderefoper(var o:toper);override;
  163. private
  164. { next fields are filled in pass1, so pass2 is faster }
  165. insentry : PInsEntry;
  166. insoffset,
  167. inssize : longint;
  168. LastInsOffset : longint; { need to be public to be reset }
  169. function InsEnd:longint;
  170. procedure create_ot;
  171. function Matches(p:PInsEntry):longint;
  172. function calcsize(p:PInsEntry):longint;
  173. procedure gencode(sec:TAsmObjectData);
  174. function NeedAddrPrefix(opidx:byte):boolean;
  175. procedure Swapoperands;
  176. {$endif NOAG386BIN}
  177. function is_nop:boolean;override;
  178. end;
  179. procedure InitAsm;
  180. procedure DoneAsm;
  181. implementation
  182. uses
  183. cutils,
  184. agx86att;
  185. {*****************************************************************************
  186. Instruction table
  187. *****************************************************************************}
  188. const
  189. {Instruction flags }
  190. IF_NONE = $00000000;
  191. IF_SM = $00000001; { size match first two operands }
  192. IF_SM2 = $00000002;
  193. IF_SB = $00000004; { unsized operands can't be non-byte }
  194. IF_SW = $00000008; { unsized operands can't be non-word }
  195. IF_SD = $00000010; { unsized operands can't be nondword }
  196. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  197. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  198. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  199. IF_ARMASK = $00000060; { mask for unsized argument spec }
  200. IF_PRIV = $00000100; { it's a privileged instruction }
  201. IF_SMM = $00000200; { it's only valid in SMM }
  202. IF_PROT = $00000400; { it's protected mode only }
  203. IF_UNDOC = $00001000; { it's an undocumented instruction }
  204. IF_FPU = $00002000; { it's an FPU instruction }
  205. IF_MMX = $00004000; { it's an MMX instruction }
  206. { it's a 3DNow! instruction }
  207. IF_3DNOW = $00008000;
  208. { it's a SSE (KNI, MMX2) instruction }
  209. IF_SSE = $00010000;
  210. { SSE2 instructions }
  211. IF_SSE2 = $00020000;
  212. { the mask for processor types }
  213. {IF_PMASK = longint($FF000000);}
  214. { the mask for disassembly "prefer" }
  215. {IF_PFMASK = longint($F001FF00);}
  216. IF_8086 = $00000000; { 8086 instruction }
  217. IF_186 = $01000000; { 186+ instruction }
  218. IF_286 = $02000000; { 286+ instruction }
  219. IF_386 = $03000000; { 386+ instruction }
  220. IF_486 = $04000000; { 486+ instruction }
  221. IF_PENT = $05000000; { Pentium instruction }
  222. IF_P6 = $06000000; { P6 instruction }
  223. IF_KATMAI = $07000000; { Katmai instructions }
  224. { Willamette instructions }
  225. IF_WILLAMETTE = $08000000;
  226. IF_CYRIX = $10000000; { Cyrix-specific instruction }
  227. IF_AMD = $20000000; { AMD-specific instruction }
  228. { added flags }
  229. IF_PRE = $40000000; { it's a prefix instruction }
  230. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  231. type
  232. TInsTabCache=array[TasmOp] of longint;
  233. PInsTabCache=^TInsTabCache;
  234. const
  235. {$ifdef x86_64}
  236. InsTab:array[0..instabentries-1] of TInsEntry={$i x86_64ta.inc}
  237. {$else x86_64}
  238. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  239. {$endif x86_64}
  240. var
  241. InsTabCache : PInsTabCache;
  242. const
  243. {$ifdef x86_64}
  244. { Intel style operands ! }
  245. opsize_2_type:array[0..2,topsize] of longint=(
  246. (OT_NONE,
  247. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  248. OT_BITS16,OT_BITS32,OT_BITS64,
  249. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  250. OT_NEAR,OT_FAR,OT_SHORT
  251. ),
  252. (OT_NONE,
  253. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  254. OT_BITS16,OT_BITS32,OT_BITS64,
  255. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  256. OT_NEAR,OT_FAR,OT_SHORT
  257. ),
  258. (OT_NONE,
  259. OT_BITS8,OT_BITS16,OT_BITS32,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  260. OT_BITS16,OT_BITS32,OT_BITS64,
  261. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  262. OT_NEAR,OT_FAR,OT_SHORT
  263. )
  264. );
  265. { Convert reg to operand type }
  266. reg2type : array[firstreg..lastreg] of longint = (OT_NONE,
  267. OT_REG_RAX,OT_REG_RCX,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,
  268. OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG64,OT_REG_RIP,
  269. OT_REG_EAX,OT_REG_ECX,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,
  270. OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,
  271. OT_REG_AX,OT_REG_CX,OT_REG_DX,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,
  272. OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,
  273. OT_REG_AL,OT_REG_CL,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  274. OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  275. OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  276. OT_REG_CS,OT_REG_DESS,OT_REG_DESS,OT_REG_DESS,OT_REG_FSGS,OT_REG_FSGS,
  277. OT_FPU0,OT_FPU0,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,
  278. OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,
  279. OT_REG_CREG,OT_REG_CREG,OT_REG_CREG,OT_REG_CR4,
  280. OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,
  281. OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,
  282. OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,
  283. OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG
  284. );
  285. {$else x86_64}
  286. { Intel style operands ! }
  287. opsize_2_type:array[0..2,topsize] of longint=(
  288. (OT_NONE,
  289. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS16,OT_BITS32,OT_BITS32,
  290. OT_BITS16,OT_BITS32,OT_BITS64,
  291. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  292. OT_NEAR,OT_FAR,OT_SHORT
  293. ),
  294. (OT_NONE,
  295. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS8,OT_BITS8,OT_BITS16,
  296. OT_BITS16,OT_BITS32,OT_BITS64,
  297. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  298. OT_NEAR,OT_FAR,OT_SHORT
  299. ),
  300. (OT_NONE,
  301. OT_BITS8,OT_BITS16,OT_BITS32,OT_NONE,OT_NONE,OT_NONE,
  302. OT_BITS16,OT_BITS32,OT_BITS64,
  303. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_BITS64,OT_BITS64,OT_NONE,
  304. OT_NEAR,OT_FAR,OT_SHORT
  305. )
  306. );
  307. subreg2type:array[R_SUBL..R_SUBD] of longint = (
  308. OT_REG8,OT_REG8,OT_REG16,OT_REG32
  309. );
  310. { Convert reg to operand type }
  311. reg2type : array[firstreg..lastreg] of longint = (OT_NONE,
  312. OT_REG_EAX,OT_REG_ECX,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,OT_REG32,
  313. OT_REG_AX,OT_REG_CX,OT_REG_DX,OT_REG16,OT_REG16,OT_REG16,OT_REG16,OT_REG16,
  314. OT_REG_AL,OT_REG_CL,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,OT_REG8,
  315. OT_REG_CS,OT_REG_DESS,OT_REG_DESS,OT_REG_DESS,OT_REG_FSGS,OT_REG_FSGS,
  316. OT_FPU0,OT_FPU0,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,OT_FPUREG,
  317. OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,OT_REG_DREG,
  318. OT_REG_CREG,OT_REG_CREG,OT_REG_CREG,OT_REG_CR4,
  319. OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,OT_REG_TREG,
  320. OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,OT_MMXREG,
  321. OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG,OT_XMMREG
  322. );
  323. {$endif x86_64}
  324. {****************************************************************************
  325. TAI_ALIGN
  326. ****************************************************************************}
  327. constructor tai_align.create(b: byte);
  328. begin
  329. inherited create(b);
  330. reg.enum := R_ECX;
  331. end;
  332. constructor tai_align.create_op(b: byte; _op: byte);
  333. begin
  334. inherited create_op(b,_op);
  335. reg.enum := R_NO;
  336. end;
  337. function tai_align.calculatefillbuf(var buf : tfillbuffer):pchar;
  338. const
  339. alignarray:array[0..5] of string[8]=(
  340. #$8D#$B4#$26#$00#$00#$00#$00,
  341. #$8D#$B6#$00#$00#$00#$00,
  342. #$8D#$74#$26#$00,
  343. #$8D#$76#$00,
  344. #$89#$F6,
  345. #$90
  346. );
  347. var
  348. bufptr : pchar;
  349. j : longint;
  350. begin
  351. inherited calculatefillbuf(buf);
  352. if not use_op then
  353. begin
  354. bufptr:=pchar(@buf);
  355. while (fillsize>0) do
  356. begin
  357. for j:=0 to 5 do
  358. if (fillsize>=length(alignarray[j])) then
  359. break;
  360. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  361. inc(bufptr,length(alignarray[j]));
  362. dec(fillsize,length(alignarray[j]));
  363. end;
  364. end;
  365. calculatefillbuf:=pchar(@buf);
  366. end;
  367. {*****************************************************************************
  368. Taicpu Constructors
  369. *****************************************************************************}
  370. procedure taicpu.changeopsize(siz:topsize);
  371. begin
  372. opsize:=siz;
  373. end;
  374. procedure taicpu.init(_size : topsize);
  375. begin
  376. { default order is att }
  377. FOperandOrder:=op_att;
  378. segprefix.enum:=R_NO;
  379. opsize:=_size;
  380. {$ifndef NOAG386BIN}
  381. insentry:=nil;
  382. LastInsOffset:=-1;
  383. InsOffset:=0;
  384. InsSize:=0;
  385. {$endif}
  386. end;
  387. constructor taicpu.op_none(op : tasmop;_size : topsize);
  388. begin
  389. inherited create(op);
  390. init(_size);
  391. end;
  392. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  393. begin
  394. inherited create(op);
  395. init(_size);
  396. ops:=1;
  397. loadreg(0,_op1);
  398. end;
  399. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aword);
  400. begin
  401. inherited create(op);
  402. init(_size);
  403. ops:=1;
  404. loadconst(0,_op1);
  405. end;
  406. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  407. begin
  408. inherited create(op);
  409. init(_size);
  410. ops:=1;
  411. loadref(0,_op1);
  412. end;
  413. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  414. begin
  415. inherited create(op);
  416. init(_size);
  417. ops:=2;
  418. loadreg(0,_op1);
  419. loadreg(1,_op2);
  420. end;
  421. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aword);
  422. begin
  423. inherited create(op);
  424. init(_size);
  425. ops:=2;
  426. loadreg(0,_op1);
  427. loadconst(1,_op2);
  428. end;
  429. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  430. begin
  431. inherited create(op);
  432. init(_size);
  433. ops:=2;
  434. loadreg(0,_op1);
  435. loadref(1,_op2);
  436. end;
  437. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister);
  438. begin
  439. inherited create(op);
  440. init(_size);
  441. ops:=2;
  442. loadconst(0,_op1);
  443. loadreg(1,_op2);
  444. end;
  445. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aword);
  446. begin
  447. inherited create(op);
  448. init(_size);
  449. ops:=2;
  450. loadconst(0,_op1);
  451. loadconst(1,_op2);
  452. end;
  453. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference);
  454. begin
  455. inherited create(op);
  456. init(_size);
  457. ops:=2;
  458. loadconst(0,_op1);
  459. loadref(1,_op2);
  460. end;
  461. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  462. begin
  463. inherited create(op);
  464. init(_size);
  465. ops:=2;
  466. loadref(0,_op1);
  467. loadreg(1,_op2);
  468. end;
  469. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  470. begin
  471. inherited create(op);
  472. init(_size);
  473. ops:=3;
  474. loadreg(0,_op1);
  475. loadreg(1,_op2);
  476. loadreg(2,_op3);
  477. end;
  478. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;_op3 : tregister);
  479. begin
  480. inherited create(op);
  481. init(_size);
  482. ops:=3;
  483. loadconst(0,_op1);
  484. loadreg(1,_op2);
  485. loadreg(2,_op3);
  486. end;
  487. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  488. begin
  489. inherited create(op);
  490. init(_size);
  491. ops:=3;
  492. loadreg(0,_op1);
  493. loadreg(1,_op2);
  494. loadref(2,_op3);
  495. end;
  496. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aword;const _op2 : treference;_op3 : tregister);
  497. begin
  498. inherited create(op);
  499. init(_size);
  500. ops:=3;
  501. loadconst(0,_op1);
  502. loadref(1,_op2);
  503. loadreg(2,_op3);
  504. end;
  505. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aword;_op2 : tregister;const _op3 : treference);
  506. begin
  507. inherited create(op);
  508. init(_size);
  509. ops:=3;
  510. loadconst(0,_op1);
  511. loadreg(1,_op2);
  512. loadref(2,_op3);
  513. end;
  514. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  515. begin
  516. inherited create(op);
  517. init(_size);
  518. condition:=cond;
  519. ops:=1;
  520. loadsymbol(0,_op1,0);
  521. end;
  522. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  523. begin
  524. inherited create(op);
  525. init(_size);
  526. ops:=1;
  527. loadsymbol(0,_op1,0);
  528. end;
  529. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  530. begin
  531. inherited create(op);
  532. init(_size);
  533. ops:=1;
  534. loadsymbol(0,_op1,_op1ofs);
  535. end;
  536. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  537. begin
  538. inherited create(op);
  539. init(_size);
  540. ops:=2;
  541. loadsymbol(0,_op1,_op1ofs);
  542. loadreg(1,_op2);
  543. end;
  544. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  545. begin
  546. inherited create(op);
  547. init(_size);
  548. ops:=2;
  549. loadsymbol(0,_op1,_op1ofs);
  550. loadref(1,_op2);
  551. end;
  552. function taicpu.GetString:string;
  553. var
  554. i : longint;
  555. s : string;
  556. addsize : boolean;
  557. begin
  558. s:='['+std_op2str[opcode];
  559. for i:=1to ops do
  560. begin
  561. if i=1 then
  562. s:=s+' '
  563. else
  564. s:=s+',';
  565. { type }
  566. addsize:=false;
  567. if (oper[i-1].ot and OT_XMMREG)=OT_XMMREG then
  568. s:=s+'xmmreg'
  569. else
  570. if (oper[i-1].ot and OT_MMXREG)=OT_MMXREG then
  571. s:=s+'mmxreg'
  572. else
  573. if (oper[i-1].ot and OT_FPUREG)=OT_FPUREG then
  574. s:=s+'fpureg'
  575. else
  576. if (oper[i-1].ot and OT_REGISTER)=OT_REGISTER then
  577. begin
  578. s:=s+'reg';
  579. addsize:=true;
  580. end
  581. else
  582. if (oper[i-1].ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  583. begin
  584. s:=s+'imm';
  585. addsize:=true;
  586. end
  587. else
  588. if (oper[i-1].ot and OT_MEMORY)=OT_MEMORY then
  589. begin
  590. s:=s+'mem';
  591. addsize:=true;
  592. end
  593. else
  594. s:=s+'???';
  595. { size }
  596. if addsize then
  597. begin
  598. if (oper[i-1].ot and OT_BITS8)<>0 then
  599. s:=s+'8'
  600. else
  601. if (oper[i-1].ot and OT_BITS16)<>0 then
  602. s:=s+'16'
  603. else
  604. if (oper[i-1].ot and OT_BITS32)<>0 then
  605. s:=s+'32'
  606. else
  607. s:=s+'??';
  608. { signed }
  609. if (oper[i-1].ot and OT_SIGNED)<>0 then
  610. s:=s+'s';
  611. end;
  612. end;
  613. GetString:=s+']';
  614. end;
  615. procedure taicpu.Swapoperands;
  616. var
  617. p : TOper;
  618. begin
  619. { Fix the operands which are in AT&T style and we need them in Intel style }
  620. case ops of
  621. 2 : begin
  622. { 0,1 -> 1,0 }
  623. p:=oper[0];
  624. oper[0]:=oper[1];
  625. oper[1]:=p;
  626. end;
  627. 3 : begin
  628. { 0,1,2 -> 2,1,0 }
  629. p:=oper[0];
  630. oper[0]:=oper[2];
  631. oper[2]:=p;
  632. end;
  633. end;
  634. end;
  635. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  636. begin
  637. if FOperandOrder<>order then
  638. begin
  639. Swapoperands;
  640. FOperandOrder:=order;
  641. end;
  642. end;
  643. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  644. begin
  645. o.typ:=toptype(ppufile.getbyte);
  646. o.ot:=ppufile.getlongint;
  647. case o.typ of
  648. top_reg :
  649. ppufile.getdata(o.reg,sizeof(Tregister));
  650. top_ref :
  651. begin
  652. new(o.ref);
  653. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  654. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  655. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  656. o.ref^.scalefactor:=ppufile.getbyte;
  657. o.ref^.offset:=ppufile.getlongint;
  658. o.ref^.symbol:=ppufile.getasmsymbol;
  659. o.ref^.offsetfixup:=ppufile.getlongint;
  660. o.ref^.options:=trefoptions(ppufile.getbyte);
  661. end;
  662. top_const :
  663. o.val:=aword(ppufile.getlongint);
  664. top_symbol :
  665. begin
  666. o.sym:=ppufile.getasmsymbol;
  667. o.symofs:=ppufile.getlongint;
  668. end;
  669. end;
  670. end;
  671. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  672. begin
  673. ppufile.putbyte(byte(o.typ));
  674. ppufile.putlongint(o.ot);
  675. case o.typ of
  676. top_reg :
  677. ppufile.putdata(o.reg,sizeof(Tregister));
  678. top_ref :
  679. begin
  680. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  681. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  682. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  683. ppufile.putbyte(o.ref^.scalefactor);
  684. ppufile.putlongint(o.ref^.offset);
  685. ppufile.putasmsymbol(o.ref^.symbol);
  686. ppufile.putlongint(o.ref^.offsetfixup);
  687. ppufile.putbyte(byte(o.ref^.options));
  688. end;
  689. top_const :
  690. ppufile.putlongint(longint(o.val));
  691. top_symbol :
  692. begin
  693. ppufile.putasmsymbol(o.sym);
  694. ppufile.putlongint(longint(o.symofs));
  695. end;
  696. end;
  697. end;
  698. procedure taicpu.ppuderefoper(var o:toper);
  699. begin
  700. case o.typ of
  701. top_ref :
  702. begin
  703. if assigned(o.ref^.symbol) then
  704. objectlibrary.derefasmsymbol(o.ref^.symbol);
  705. end;
  706. top_symbol :
  707. objectlibrary.derefasmsymbol(o.sym);
  708. end;
  709. end;
  710. procedure taicpu.CheckNonCommutativeOpcodes;
  711. begin
  712. { we need ATT order }
  713. SetOperandOrder(op_att);
  714. if ((ops=2) and
  715. (oper[0].typ=top_reg) and
  716. (oper[1].typ=top_reg) and
  717. { if the first is ST and the second is also a register
  718. it is necessarily ST1 .. ST7 }
  719. (oper[0].reg.enum in [R_ST..R_ST0])) or
  720. { ((ops=1) and
  721. (oper[0].typ=top_reg) and
  722. (oper[0].reg in [R_ST1..R_ST7])) or}
  723. (ops=0) then
  724. if opcode=A_FSUBR then
  725. opcode:=A_FSUB
  726. else if opcode=A_FSUB then
  727. opcode:=A_FSUBR
  728. else if opcode=A_FDIVR then
  729. opcode:=A_FDIV
  730. else if opcode=A_FDIV then
  731. opcode:=A_FDIVR
  732. else if opcode=A_FSUBRP then
  733. opcode:=A_FSUBP
  734. else if opcode=A_FSUBP then
  735. opcode:=A_FSUBRP
  736. else if opcode=A_FDIVRP then
  737. opcode:=A_FDIVP
  738. else if opcode=A_FDIVP then
  739. opcode:=A_FDIVRP;
  740. if ((ops=1) and
  741. (oper[0].typ=top_reg) and
  742. (oper[0].reg.enum in [R_ST1..R_ST7])) then
  743. if opcode=A_FSUBRP then
  744. opcode:=A_FSUBP
  745. else if opcode=A_FSUBP then
  746. opcode:=A_FSUBRP
  747. else if opcode=A_FDIVRP then
  748. opcode:=A_FDIVP
  749. else if opcode=A_FDIVP then
  750. opcode:=A_FDIVRP;
  751. end;
  752. {*****************************************************************************
  753. Assembler
  754. *****************************************************************************}
  755. {$ifndef NOAG386BIN}
  756. type
  757. ea=packed record
  758. sib_present : boolean;
  759. bytes : byte;
  760. size : byte;
  761. modrm : byte;
  762. sib : byte;
  763. end;
  764. procedure taicpu.create_ot;
  765. {
  766. this function will also fix some other fields which only needs to be once
  767. }
  768. var
  769. i,l,relsize : longint;
  770. nb,ni:boolean;
  771. begin
  772. if ops=0 then
  773. exit;
  774. { update oper[].ot field }
  775. for i:=0 to ops-1 do
  776. with oper[i] do
  777. begin
  778. case typ of
  779. top_reg :
  780. begin
  781. if reg.enum=R_INTREGISTER then
  782. case reg.number of
  783. NR_AL:
  784. ot:=OT_REG_AL;
  785. NR_AX:
  786. ot:=OT_REG_AX;
  787. NR_EAX:
  788. ot:=OT_REG_EAX;
  789. NR_CL:
  790. ot:=OT_REG_CL;
  791. NR_CX:
  792. ot:=OT_REG_CX;
  793. NR_ECX:
  794. ot:=OT_REG_ECX;
  795. NR_DX:
  796. ot:=OT_REG_DX;
  797. NR_CS:
  798. ot:=OT_REG_CS;
  799. NR_DS,NR_ES,NR_SS:
  800. ot:=OT_REG_DESS;
  801. NR_FS,NR_GS:
  802. ot:=OT_REG_FSGS;
  803. NR_DR0..NR_DR7:
  804. ot:=OT_REG_DREG;
  805. NR_CR0..NR_CR3:
  806. ot:=OT_REG_CREG;
  807. NR_CR4:
  808. ot:=OT_REG_CR4;
  809. NR_TR3..NR_TR7:
  810. ot:=OT_REG_TREG;
  811. else
  812. ot:=subreg2type[reg.number and $ff];
  813. end
  814. else
  815. ot:=reg2type[reg.enum];
  816. end;
  817. top_ref :
  818. begin
  819. nb:=(ref^.base.enum=R_NO) or
  820. ((ref^.base.enum=R_INTREGISTER) and (ref^.base.number=NR_NO));
  821. ni:=(ref^.index.enum=R_NO) or
  822. ((ref^.index.enum=R_INTREGISTER) and (ref^.index.number=NR_NO));
  823. { create ot field }
  824. if (ot and OT_SIZE_MASK)=0 then
  825. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  826. else
  827. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  828. if nb and ni then
  829. ot:=ot or OT_MEM_OFFS;
  830. { fix scalefactor }
  831. if ni then
  832. ref^.scalefactor:=0
  833. else
  834. if (ref^.scalefactor=0) then
  835. ref^.scalefactor:=1;
  836. end;
  837. top_const :
  838. begin
  839. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  840. ot:=OT_IMM8 or OT_SIGNED
  841. else
  842. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  843. end;
  844. top_symbol :
  845. begin
  846. if LastInsOffset=-1 then
  847. l:=0
  848. else
  849. l:=InsOffset-LastInsOffset;
  850. inc(l,symofs);
  851. if assigned(sym) then
  852. inc(l,sym.address);
  853. { instruction size will then always become 2 (PFV) }
  854. relsize:=(InsOffset+2)-l;
  855. if (not assigned(sym) or
  856. ((sym.currbind<>AB_EXTERNAL) and (sym.address<>0))) and
  857. (relsize>=-128) and (relsize<=127) then
  858. ot:=OT_IMM32 or OT_SHORT
  859. else
  860. ot:=OT_IMM32 or OT_NEAR;
  861. end;
  862. end;
  863. end;
  864. end;
  865. function taicpu.InsEnd:longint;
  866. begin
  867. InsEnd:=InsOffset+InsSize;
  868. end;
  869. function taicpu.Matches(p:PInsEntry):longint;
  870. { * IF_SM stands for Size Match: any operand whose size is not
  871. * explicitly specified by the template is `really' intended to be
  872. * the same size as the first size-specified operand.
  873. * Non-specification is tolerated in the input instruction, but
  874. * _wrong_ specification is not.
  875. *
  876. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  877. * three-operand instructions such as SHLD: it implies that the
  878. * first two operands must match in size, but that the third is
  879. * required to be _unspecified_.
  880. *
  881. * IF_SB invokes Size Byte: operands with unspecified size in the
  882. * template are really bytes, and so no non-byte specification in
  883. * the input instruction will be tolerated. IF_SW similarly invokes
  884. * Size Word, and IF_SD invokes Size Doubleword.
  885. *
  886. * (The default state if neither IF_SM nor IF_SM2 is specified is
  887. * that any operand with unspecified size in the template is
  888. * required to have unspecified size in the instruction too...)
  889. }
  890. var
  891. i,j,asize,oprs : longint;
  892. siz : array[0..2] of longint;
  893. begin
  894. Matches:=100;
  895. { Check the opcode and operands }
  896. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  897. begin
  898. Matches:=0;
  899. exit;
  900. end;
  901. { Check that no spurious colons or TOs are present }
  902. for i:=0 to p^.ops-1 do
  903. if (oper[i].ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  904. begin
  905. Matches:=0;
  906. exit;
  907. end;
  908. { Check that the operand flags all match up }
  909. for i:=0 to p^.ops-1 do
  910. begin
  911. if ((p^.optypes[i] and (not oper[i].ot)) or
  912. ((p^.optypes[i] and OT_SIZE_MASK) and
  913. ((p^.optypes[i] xor oper[i].ot) and OT_SIZE_MASK)))<>0 then
  914. begin
  915. if ((p^.optypes[i] and (not oper[i].ot) and OT_NON_SIZE) or
  916. (oper[i].ot and OT_SIZE_MASK))<>0 then
  917. begin
  918. Matches:=0;
  919. exit;
  920. end
  921. else
  922. Matches:=1;
  923. end;
  924. end;
  925. { Check operand sizes }
  926. { as default an untyped size can get all the sizes, this is different
  927. from nasm, but else we need to do a lot checking which opcodes want
  928. size or not with the automatic size generation }
  929. asize:=longint($ffffffff);
  930. if (p^.flags and IF_SB)<>0 then
  931. asize:=OT_BITS8
  932. else if (p^.flags and IF_SW)<>0 then
  933. asize:=OT_BITS16
  934. else if (p^.flags and IF_SD)<>0 then
  935. asize:=OT_BITS32;
  936. if (p^.flags and IF_ARMASK)<>0 then
  937. begin
  938. siz[0]:=0;
  939. siz[1]:=0;
  940. siz[2]:=0;
  941. if (p^.flags and IF_AR0)<>0 then
  942. siz[0]:=asize
  943. else if (p^.flags and IF_AR1)<>0 then
  944. siz[1]:=asize
  945. else if (p^.flags and IF_AR2)<>0 then
  946. siz[2]:=asize;
  947. end
  948. else
  949. begin
  950. { we can leave because the size for all operands is forced to be
  951. the same
  952. but not if IF_SB IF_SW or IF_SD is set PM }
  953. if asize=-1 then
  954. exit;
  955. siz[0]:=asize;
  956. siz[1]:=asize;
  957. siz[2]:=asize;
  958. end;
  959. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  960. begin
  961. if (p^.flags and IF_SM2)<>0 then
  962. oprs:=2
  963. else
  964. oprs:=p^.ops;
  965. for i:=0 to oprs-1 do
  966. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  967. begin
  968. for j:=0 to oprs-1 do
  969. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  970. break;
  971. end;
  972. end
  973. else
  974. oprs:=2;
  975. { Check operand sizes }
  976. for i:=0 to p^.ops-1 do
  977. begin
  978. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  979. ((oper[i].ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  980. { Immediates can always include smaller size }
  981. ((oper[i].ot and OT_IMMEDIATE)=0) and
  982. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i].ot and OT_SIZE_MASK)) then
  983. Matches:=2;
  984. end;
  985. end;
  986. procedure taicpu.ResetPass1;
  987. begin
  988. { we need to reset everything here, because the choosen insentry
  989. can be invalid for a new situation where the previously optimized
  990. insentry is not correct }
  991. InsEntry:=nil;
  992. InsSize:=0;
  993. LastInsOffset:=-1;
  994. end;
  995. procedure taicpu.ResetPass2;
  996. begin
  997. { we are here in a second pass, check if the instruction can be optimized }
  998. if assigned(InsEntry) and
  999. ((InsEntry^.flags and IF_PASS2)<>0) then
  1000. begin
  1001. InsEntry:=nil;
  1002. InsSize:=0;
  1003. end;
  1004. LastInsOffset:=-1;
  1005. end;
  1006. function taicpu.CheckIfValid:boolean;
  1007. var
  1008. m,i : longint;
  1009. begin
  1010. CheckIfValid:=false;
  1011. { Things which may only be done once, not when a second pass is done to
  1012. optimize }
  1013. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1014. begin
  1015. { We need intel style operands }
  1016. SetOperandOrder(op_intel);
  1017. { create the .ot fields }
  1018. create_ot;
  1019. { set the file postion }
  1020. aktfilepos:=fileinfo;
  1021. end
  1022. else
  1023. begin
  1024. { we've already an insentry so it's valid }
  1025. CheckIfValid:=true;
  1026. exit;
  1027. end;
  1028. { Lookup opcode in the table }
  1029. InsSize:=-1;
  1030. i:=instabcache^[opcode];
  1031. if i=-1 then
  1032. begin
  1033. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1034. exit;
  1035. end;
  1036. insentry:=@instab[i];
  1037. while (insentry^.opcode=opcode) do
  1038. begin
  1039. m:=matches(insentry);
  1040. if m=100 then
  1041. begin
  1042. InsSize:=calcsize(insentry);
  1043. if not((segprefix.enum=R_NO) or ((segprefix.enum=R_INTREGISTER) and (segprefix.number=NR_NO))) then
  1044. inc(InsSize);
  1045. { For opsize if size if forced }
  1046. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1047. begin
  1048. if (insentry^.flags and IF_ARMASK)=0 then
  1049. begin
  1050. if (insentry^.flags and IF_SB)<>0 then
  1051. begin
  1052. if opsize=S_NO then
  1053. opsize:=S_B;
  1054. end
  1055. else if (insentry^.flags and IF_SW)<>0 then
  1056. begin
  1057. if opsize=S_NO then
  1058. opsize:=S_W;
  1059. end
  1060. else if (insentry^.flags and IF_SD)<>0 then
  1061. begin
  1062. if opsize=S_NO then
  1063. opsize:=S_L;
  1064. end;
  1065. end;
  1066. end;
  1067. CheckIfValid:=true;
  1068. exit;
  1069. end;
  1070. inc(i);
  1071. insentry:=@instab[i];
  1072. end;
  1073. if insentry^.opcode<>opcode then
  1074. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1075. { No instruction found, set insentry to nil and inssize to -1 }
  1076. insentry:=nil;
  1077. inssize:=-1;
  1078. end;
  1079. function taicpu.Pass1(offset:longint):longint;
  1080. begin
  1081. Pass1:=0;
  1082. { Save the old offset and set the new offset }
  1083. InsOffset:=Offset;
  1084. { Things which may only be done once, not when a second pass is done to
  1085. optimize }
  1086. if Insentry=nil then
  1087. begin
  1088. { Check if error last time then InsSize=-1 }
  1089. if InsSize=-1 then
  1090. exit;
  1091. { set the file postion }
  1092. aktfilepos:=fileinfo;
  1093. end
  1094. else
  1095. begin
  1096. {$ifdef PASS2FLAG}
  1097. { we are here in a second pass, check if the instruction can be optimized }
  1098. if (InsEntry^.flags and IF_PASS2)=0 then
  1099. begin
  1100. Pass1:=InsSize;
  1101. exit;
  1102. end;
  1103. { update the .ot fields, some top_const can be updated }
  1104. create_ot;
  1105. {$endif PASS2FLAG}
  1106. end;
  1107. { Check if it's a valid instruction }
  1108. if CheckIfValid then
  1109. begin
  1110. LastInsOffset:=InsOffset;
  1111. Pass1:=InsSize;
  1112. exit;
  1113. end;
  1114. LastInsOffset:=-1;
  1115. end;
  1116. procedure taicpu.Pass2(sec:TAsmObjectData);
  1117. var
  1118. c : longint;
  1119. begin
  1120. { error in pass1 ? }
  1121. if insentry=nil then
  1122. exit;
  1123. aktfilepos:=fileinfo;
  1124. { Segment override }
  1125. if segprefix.enum>lastreg then
  1126. internalerror(200201081);
  1127. if (segprefix.enum<>R_NO) then
  1128. begin
  1129. case segprefix.enum of
  1130. R_CS : c:=$2e;
  1131. R_DS : c:=$3e;
  1132. R_ES : c:=$26;
  1133. R_FS : c:=$64;
  1134. R_GS : c:=$65;
  1135. R_SS : c:=$36;
  1136. end;
  1137. sec.writebytes(c,1);
  1138. { fix the offset for GenNode }
  1139. inc(InsOffset);
  1140. end;
  1141. { Generate the instruction }
  1142. GenCode(sec);
  1143. end;
  1144. function taicpu.needaddrprefix(opidx:byte):boolean;
  1145. var i,b:Tnewregister;
  1146. ia,ba:boolean;
  1147. begin
  1148. needaddrprefix:=false;
  1149. if (OT_MEMORY and (not oper[opidx].ot))=0 then
  1150. begin
  1151. if oper[opidx].ref^.index.enum=R_INTREGISTER then
  1152. begin
  1153. i:=oper[opidx].ref^.index.number;
  1154. ia:=(i<>NR_NO) and (i and $ff<>R_SUBD);
  1155. end
  1156. else
  1157. ia:=not(oper[opidx].ref^.index.enum in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]);
  1158. if oper[opidx].ref^.base.enum=R_INTREGISTER then
  1159. begin
  1160. b:=oper[opidx].ref^.base.number;
  1161. ba:=(b<>NR_NO) and (b and $ff<>R_SUBD);
  1162. end
  1163. else
  1164. ba:=not(oper[opidx].ref^.base.enum in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]);
  1165. b:=oper[opidx].ref^.base.number;
  1166. i:=oper[opidx].ref^.index.number;
  1167. if ia or ba then
  1168. needaddrprefix:=true;
  1169. end;
  1170. end;
  1171. function regval(r:tregister):byte;
  1172. begin
  1173. case r.enum of
  1174. R_EAX,R_AX,R_AL,R_ES,R_CR0,R_DR0,R_ST,R_ST0,R_MM0,R_XMM0 :
  1175. regval:=0;
  1176. R_ECX,R_CX,R_CL,R_CS,R_DR1,R_ST1,R_MM1,R_XMM1 :
  1177. regval:=1;
  1178. R_EDX,R_DX,R_DL,R_SS,R_CR2,R_DR2,R_ST2,R_MM2,R_XMM2 :
  1179. regval:=2;
  1180. R_EBX,R_BX,R_BL,R_DS,R_CR3,R_DR3,R_TR3,R_ST3,R_MM3,R_XMM3 :
  1181. regval:=3;
  1182. R_ESP,R_SP,R_AH,R_FS,R_CR4,R_TR4,R_ST4,R_MM4,R_XMM4 :
  1183. regval:=4;
  1184. R_EBP,R_BP,R_CH,R_GS,R_TR5,R_ST5,R_MM5,R_XMM5 :
  1185. regval:=5;
  1186. R_ESI,R_SI,R_DH,R_DR6,R_TR6,R_ST6,R_MM6,R_XMM6 :
  1187. regval:=6;
  1188. R_EDI,R_DI,R_BH,R_DR7,R_TR7,R_ST7,R_MM7,R_XMM7 :
  1189. regval:=7;
  1190. else
  1191. begin
  1192. internalerror(777001);
  1193. regval:=0;
  1194. end;
  1195. end;
  1196. end;
  1197. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1198. const
  1199. regs : array[0..63] of Toldregister=(
  1200. R_MM0, R_EAX, R_AX, R_AL, R_XMM0, R_NO, R_NO, R_NO,
  1201. R_MM1, R_ECX, R_CX, R_CL, R_XMM1, R_NO, R_NO, R_NO,
  1202. R_MM2, R_EDX, R_DX, R_DL, R_XMM2, R_NO, R_NO, R_NO,
  1203. R_MM3, R_EBX, R_BX, R_BL, R_XMM3, R_NO, R_NO, R_NO,
  1204. R_MM4, R_ESP, R_SP, R_AH, R_XMM4, R_NO, R_NO, R_NO,
  1205. R_MM5, R_EBP, R_BP, R_CH, R_XMM5, R_NO, R_NO, R_NO,
  1206. R_MM6, R_ESI, R_SI, R_DH, R_XMM6, R_NO, R_NO, R_NO,
  1207. R_MM7, R_EDI, R_DI, R_BH, R_XMM7, R_NO, R_NO, R_NO
  1208. );
  1209. var
  1210. j : longint;
  1211. i,b : Toldregister;
  1212. sym : tasmsymbol;
  1213. md,s : byte;
  1214. base,index,scalefactor,
  1215. o : longint;
  1216. ireg : Tregister;
  1217. ir,br : Tregister;
  1218. begin
  1219. process_ea:=false;
  1220. { register ? }
  1221. if (input.typ=top_reg) then
  1222. begin
  1223. ireg:=input.reg;
  1224. convert_register_to_enum(ireg);
  1225. j:=0;
  1226. while (j<=high(regs)) do
  1227. begin
  1228. if ireg.enum=regs[j] then
  1229. break;
  1230. inc(j);
  1231. end;
  1232. if j<=high(regs) then
  1233. begin
  1234. output.sib_present:=false;
  1235. output.bytes:=0;
  1236. output.modrm:=$c0 or (rfield shl 3) or (j shr 3);
  1237. output.size:=1;
  1238. process_ea:=true;
  1239. end;
  1240. exit;
  1241. end;
  1242. { memory reference }
  1243. ir:=input.ref^.index;
  1244. br:=input.ref^.base;
  1245. convert_register_to_enum(ir);
  1246. convert_register_to_enum(br);
  1247. i:=ir.enum;
  1248. b:=br.enum;
  1249. if (i>lastreg) or (b>lastreg) then
  1250. internalerror(200301081);
  1251. s:=input.ref^.scalefactor;
  1252. o:=input.ref^.offset+input.ref^.offsetfixup;
  1253. sym:=input.ref^.symbol;
  1254. { it's direct address }
  1255. if (b=R_NO) and (i=R_NO) then
  1256. begin
  1257. { it's a pure offset }
  1258. output.sib_present:=false;
  1259. output.bytes:=4;
  1260. output.modrm:=5 or (rfield shl 3);
  1261. end
  1262. else
  1263. { it's an indirection }
  1264. begin
  1265. { 16 bit address? }
  1266. if not((i in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI]) and
  1267. (b in [R_NO,R_EAX,R_EBX,R_ECX,R_EDX,R_EBP,R_ESP,R_ESI,R_EDI])) then
  1268. Message(asmw_e_16bit_not_supported);
  1269. {$ifdef OPTEA}
  1270. { make single reg base }
  1271. if (b=R_NO) and (s=1) then
  1272. begin
  1273. b:=i;
  1274. i:=R_NO;
  1275. end;
  1276. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1277. if (b=R_NO) and
  1278. (((s=2) and (i<>R_ESP)) or
  1279. (s=3) or (s=5) or (s=9)) then
  1280. begin
  1281. b:=i;
  1282. dec(s);
  1283. end;
  1284. { swap ESP into base if scalefactor is 1 }
  1285. if (s=1) and (i=R_ESP) then
  1286. begin
  1287. i:=b;
  1288. b:=R_ESP;
  1289. end;
  1290. {$endif OPTEA}
  1291. { wrong, for various reasons }
  1292. if (i=R_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (i<>R_NO)) then
  1293. exit;
  1294. { base }
  1295. case b of
  1296. R_EAX : base:=0;
  1297. R_ECX : base:=1;
  1298. R_EDX : base:=2;
  1299. R_EBX : base:=3;
  1300. R_ESP : base:=4;
  1301. R_NO,
  1302. R_EBP : base:=5;
  1303. R_ESI : base:=6;
  1304. R_EDI : base:=7;
  1305. else
  1306. exit;
  1307. end;
  1308. { index }
  1309. case i of
  1310. R_EAX : index:=0;
  1311. R_ECX : index:=1;
  1312. R_EDX : index:=2;
  1313. R_EBX : index:=3;
  1314. R_NO : index:=4;
  1315. R_EBP : index:=5;
  1316. R_ESI : index:=6;
  1317. R_EDI : index:=7;
  1318. else
  1319. exit;
  1320. end;
  1321. case s of
  1322. 0,
  1323. 1 : scalefactor:=0;
  1324. 2 : scalefactor:=1;
  1325. 4 : scalefactor:=2;
  1326. 8 : scalefactor:=3;
  1327. else
  1328. exit;
  1329. end;
  1330. if (b=R_NO) or
  1331. ((b<>R_EBP) and (o=0) and (sym=nil)) then
  1332. md:=0
  1333. else
  1334. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1335. md:=1
  1336. else
  1337. md:=2;
  1338. if (b=R_NO) or (md=2) then
  1339. output.bytes:=4
  1340. else
  1341. output.bytes:=md;
  1342. { SIB needed ? }
  1343. if (i=R_NO) and (b<>R_ESP) then
  1344. begin
  1345. output.sib_present:=false;
  1346. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1347. end
  1348. else
  1349. begin
  1350. output.sib_present:=true;
  1351. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1352. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1353. end;
  1354. end;
  1355. if output.sib_present then
  1356. output.size:=2+output.bytes
  1357. else
  1358. output.size:=1+output.bytes;
  1359. process_ea:=true;
  1360. end;
  1361. function taicpu.calcsize(p:PInsEntry):longint;
  1362. var
  1363. codes : pchar;
  1364. c : byte;
  1365. len : longint;
  1366. ea_data : ea;
  1367. begin
  1368. len:=0;
  1369. codes:=@p^.code;
  1370. repeat
  1371. c:=ord(codes^);
  1372. inc(codes);
  1373. case c of
  1374. 0 :
  1375. break;
  1376. 1,2,3 :
  1377. begin
  1378. inc(codes,c);
  1379. inc(len,c);
  1380. end;
  1381. 8,9,10 :
  1382. begin
  1383. inc(codes);
  1384. inc(len);
  1385. end;
  1386. 4,5,6,7 :
  1387. begin
  1388. if opsize=S_W then
  1389. inc(len,2)
  1390. else
  1391. inc(len);
  1392. end;
  1393. 15,
  1394. 12,13,14,
  1395. 16,17,18,
  1396. 20,21,22,
  1397. 40,41,42 :
  1398. inc(len);
  1399. 24,25,26,
  1400. 31,
  1401. 48,49,50 :
  1402. inc(len,2);
  1403. 28,29,30, { we don't have 16 bit immediates code }
  1404. 32,33,34,
  1405. 52,53,54,
  1406. 56,57,58 :
  1407. inc(len,4);
  1408. 192,193,194 :
  1409. if NeedAddrPrefix(c-192) then
  1410. inc(len);
  1411. 208 :
  1412. inc(len);
  1413. 200,
  1414. 201,
  1415. 202,
  1416. 209,
  1417. 210,
  1418. 217,218,219 : ;
  1419. 216 :
  1420. begin
  1421. inc(codes);
  1422. inc(len);
  1423. end;
  1424. 224,225,226 :
  1425. begin
  1426. InternalError(777002);
  1427. end;
  1428. else
  1429. begin
  1430. if (c>=64) and (c<=191) then
  1431. begin
  1432. if not process_ea(oper[(c shr 3) and 7], ea_data, 0) then
  1433. Message(asmw_e_invalid_effective_address)
  1434. else
  1435. inc(len,ea_data.size);
  1436. end
  1437. else
  1438. InternalError(777003);
  1439. end;
  1440. end;
  1441. until false;
  1442. calcsize:=len;
  1443. end;
  1444. procedure taicpu.GenCode(sec:TAsmObjectData);
  1445. {
  1446. * the actual codes (C syntax, i.e. octal):
  1447. * \0 - terminates the code. (Unless it's a literal of course.)
  1448. * \1, \2, \3 - that many literal bytes follow in the code stream
  1449. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1450. * (POP is never used for CS) depending on operand 0
  1451. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1452. * on operand 0
  1453. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1454. * to the register value of operand 0, 1 or 2
  1455. * \17 - encodes the literal byte 0. (Some compilers don't take
  1456. * kindly to a zero byte in the _middle_ of a compile time
  1457. * string constant, so I had to put this hack in.)
  1458. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1459. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1460. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1461. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1462. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1463. * assembly mode or the address-size override on the operand
  1464. * \37 - a word constant, from the _segment_ part of operand 0
  1465. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1466. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1467. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1468. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1469. * assembly mode or the address-size override on the operand
  1470. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1471. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1472. * field the register value of operand b.
  1473. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1474. * field equal to digit b.
  1475. * \30x - might be an 0x67 byte, depending on the address size of
  1476. * the memory reference in operand x.
  1477. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1478. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1479. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1480. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1481. * \322 - indicates that this instruction is only valid when the
  1482. * operand size is the default (instruction to disassembler,
  1483. * generates no code in the assembler)
  1484. * \330 - a literal byte follows in the code stream, to be added
  1485. * to the condition code value of the instruction.
  1486. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1487. * Operand 0 had better be a segmentless constant.
  1488. }
  1489. var
  1490. currval : longint;
  1491. currsym : tasmsymbol;
  1492. procedure getvalsym(opidx:longint);
  1493. begin
  1494. case oper[opidx].typ of
  1495. top_ref :
  1496. begin
  1497. currval:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1498. currsym:=oper[opidx].ref^.symbol;
  1499. end;
  1500. top_const :
  1501. begin
  1502. currval:=longint(oper[opidx].val);
  1503. currsym:=nil;
  1504. end;
  1505. top_symbol :
  1506. begin
  1507. currval:=oper[opidx].symofs;
  1508. currsym:=oper[opidx].sym;
  1509. end;
  1510. else
  1511. Message(asmw_e_immediate_or_reference_expected);
  1512. end;
  1513. end;
  1514. const
  1515. CondVal:array[TAsmCond] of byte=($0,
  1516. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1517. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1518. $0, $A, $A, $B, $8, $4);
  1519. var
  1520. c : byte;
  1521. pb,
  1522. codes : pchar;
  1523. bytes : array[0..3] of byte;
  1524. rfield,
  1525. data,s,opidx : longint;
  1526. ea_data : ea;
  1527. begin
  1528. {$ifdef EXTDEBUG}
  1529. { safety check }
  1530. if sec.sects[sec.currsec].datasize<>insoffset then
  1531. internalerror(200130121);
  1532. {$endif EXTDEBUG}
  1533. { load data to write }
  1534. codes:=insentry^.code;
  1535. { Force word push/pop for registers }
  1536. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1537. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1538. begin
  1539. bytes[0]:=$66;
  1540. sec.writebytes(bytes,1);
  1541. end;
  1542. repeat
  1543. c:=ord(codes^);
  1544. inc(codes);
  1545. case c of
  1546. 0 :
  1547. break;
  1548. 1,2,3 :
  1549. begin
  1550. sec.writebytes(codes^,c);
  1551. inc(codes,c);
  1552. end;
  1553. 4,6 :
  1554. begin
  1555. case oper[0].reg.enum of
  1556. R_CS :
  1557. begin
  1558. if c=4 then
  1559. bytes[0]:=$f
  1560. else
  1561. bytes[0]:=$e;
  1562. end;
  1563. R_NO,
  1564. R_DS :
  1565. begin
  1566. if c=4 then
  1567. bytes[0]:=$1f
  1568. else
  1569. bytes[0]:=$1e;
  1570. end;
  1571. R_ES :
  1572. begin
  1573. if c=4 then
  1574. bytes[0]:=$7
  1575. else
  1576. bytes[0]:=$6;
  1577. end;
  1578. R_SS :
  1579. begin
  1580. if c=4 then
  1581. bytes[0]:=$17
  1582. else
  1583. bytes[0]:=$16;
  1584. end;
  1585. else
  1586. InternalError(777004);
  1587. end;
  1588. sec.writebytes(bytes,1);
  1589. end;
  1590. 5,7 :
  1591. begin
  1592. case oper[0].reg.enum of
  1593. R_FS :
  1594. begin
  1595. if c=5 then
  1596. bytes[0]:=$a1
  1597. else
  1598. bytes[0]:=$a0;
  1599. end;
  1600. R_GS :
  1601. begin
  1602. if c=5 then
  1603. bytes[0]:=$a9
  1604. else
  1605. bytes[0]:=$a8;
  1606. end;
  1607. else
  1608. InternalError(777005);
  1609. end;
  1610. sec.writebytes(bytes,1);
  1611. end;
  1612. 8,9,10 :
  1613. begin
  1614. bytes[0]:=ord(codes^)+regval(oper[c-8].reg);
  1615. inc(codes);
  1616. sec.writebytes(bytes,1);
  1617. end;
  1618. 15 :
  1619. begin
  1620. bytes[0]:=0;
  1621. sec.writebytes(bytes,1);
  1622. end;
  1623. 12,13,14 :
  1624. begin
  1625. getvalsym(c-12);
  1626. if (currval<-128) or (currval>127) then
  1627. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1628. if assigned(currsym) then
  1629. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1630. else
  1631. sec.writebytes(currval,1);
  1632. end;
  1633. 16,17,18 :
  1634. begin
  1635. getvalsym(c-16);
  1636. if (currval<-256) or (currval>255) then
  1637. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1638. if assigned(currsym) then
  1639. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1640. else
  1641. sec.writebytes(currval,1);
  1642. end;
  1643. 20,21,22 :
  1644. begin
  1645. getvalsym(c-20);
  1646. if (currval<0) or (currval>255) then
  1647. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1648. if assigned(currsym) then
  1649. sec.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1650. else
  1651. sec.writebytes(currval,1);
  1652. end;
  1653. 24,25,26 :
  1654. begin
  1655. getvalsym(c-24);
  1656. if (currval<-65536) or (currval>65535) then
  1657. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1658. if assigned(currsym) then
  1659. sec.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1660. else
  1661. sec.writebytes(currval,2);
  1662. end;
  1663. 28,29,30 :
  1664. begin
  1665. getvalsym(c-28);
  1666. if assigned(currsym) then
  1667. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1668. else
  1669. sec.writebytes(currval,4);
  1670. end;
  1671. 32,33,34 :
  1672. begin
  1673. getvalsym(c-32);
  1674. if assigned(currsym) then
  1675. sec.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1676. else
  1677. sec.writebytes(currval,4);
  1678. end;
  1679. 40,41,42 :
  1680. begin
  1681. getvalsym(c-40);
  1682. data:=currval-insend;
  1683. if assigned(currsym) then
  1684. inc(data,currsym.address);
  1685. if (data>127) or (data<-128) then
  1686. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1687. sec.writebytes(data,1);
  1688. end;
  1689. 52,53,54 :
  1690. begin
  1691. getvalsym(c-52);
  1692. if assigned(currsym) then
  1693. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1694. else
  1695. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1696. end;
  1697. 56,57,58 :
  1698. begin
  1699. getvalsym(c-56);
  1700. if assigned(currsym) then
  1701. sec.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1702. else
  1703. sec.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1704. end;
  1705. 192,193,194 :
  1706. begin
  1707. if NeedAddrPrefix(c-192) then
  1708. begin
  1709. bytes[0]:=$67;
  1710. sec.writebytes(bytes,1);
  1711. end;
  1712. end;
  1713. 200 :
  1714. begin
  1715. bytes[0]:=$67;
  1716. sec.writebytes(bytes,1);
  1717. end;
  1718. 208 :
  1719. begin
  1720. bytes[0]:=$66;
  1721. sec.writebytes(bytes,1);
  1722. end;
  1723. 216 :
  1724. begin
  1725. bytes[0]:=ord(codes^)+condval[condition];
  1726. inc(codes);
  1727. sec.writebytes(bytes,1);
  1728. end;
  1729. 201,
  1730. 202,
  1731. 209,
  1732. 210,
  1733. 217,218,219 :
  1734. begin
  1735. { these are dissambler hints or 32 bit prefixes which
  1736. are not needed }
  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. sec.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. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,1,oper[opidx].ref^.symbol,RELOC_ABSOLUTE)
  1776. else
  1777. begin
  1778. bytes[0]:=oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup;
  1779. sec.writebytes(bytes,1);
  1780. end;
  1781. inc(s);
  1782. end;
  1783. 2,4 :
  1784. begin
  1785. sec.writereloc(oper[opidx].ref^.offset+oper[opidx].ref^.offsetfixup,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_nop:boolean;
  1799. begin
  1800. {We do not check the number of operands; we assume that nobody constructs
  1801. a mov or xchg instruction with less than 2 operands.}
  1802. is_nop:=(opcode=A_NOP) or
  1803. (opcode=A_MOV) and (oper[0].typ=top_reg) and (oper[1].typ=top_reg) and (oper[0].reg.number=oper[1].reg.number) or
  1804. (opcode=A_XCHG) and (oper[0].typ=top_reg) and (oper[1].typ=top_reg) and (oper[0].reg.number=oper[1].reg.number);
  1805. end;
  1806. {*****************************************************************************
  1807. Instruction table
  1808. *****************************************************************************}
  1809. procedure BuildInsTabCache;
  1810. {$ifndef NOAG386BIN}
  1811. var
  1812. i : longint;
  1813. {$endif}
  1814. begin
  1815. {$ifndef NOAG386BIN}
  1816. new(instabcache);
  1817. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1818. i:=0;
  1819. while (i<InsTabEntries) do
  1820. begin
  1821. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1822. InsTabCache^[InsTab[i].OPcode]:=i;
  1823. inc(i);
  1824. end;
  1825. {$endif NOAG386BIN}
  1826. end;
  1827. procedure InitAsm;
  1828. begin
  1829. {$ifndef NOAG386BIN}
  1830. if not assigned(instabcache) then
  1831. BuildInsTabCache;
  1832. {$endif NOAG386BIN}
  1833. end;
  1834. procedure DoneAsm;
  1835. begin
  1836. {$ifndef NOAG386BIN}
  1837. if assigned(instabcache) then
  1838. begin
  1839. dispose(instabcache);
  1840. instabcache:=nil;
  1841. end;
  1842. {$endif NOAG386BIN}
  1843. end;
  1844. end.
  1845. {
  1846. $Log$
  1847. Revision 1.1 2003-04-25 12:43:40 florian
  1848. * merged i386/aasmcpu and x86_64/aasmcpu to x86/aasmcpu
  1849. Revision 1.18 2003/04/25 12:04:31 florian
  1850. * merged agx64att and ag386att to x86/agx86att
  1851. Revision 1.17 2003/04/22 14:33:38 peter
  1852. * removed some notes/hints
  1853. Revision 1.16 2003/04/22 10:09:35 daniel
  1854. + Implemented the actual register allocator
  1855. + Scratch registers unavailable when new register allocator used
  1856. + maybe_save/maybe_restore unavailable when new register allocator used
  1857. Revision 1.15 2003/03/26 12:50:54 armin
  1858. * avoid problems with the ide in init/dome
  1859. Revision 1.14 2003/03/08 08:59:07 daniel
  1860. + $define newra will enable new register allocator
  1861. + getregisterint will return imaginary registers with $newra
  1862. + -sr switch added, will skip register allocation so you can see
  1863. the direct output of the code generator before register allocation
  1864. Revision 1.13 2003/02/25 07:41:54 daniel
  1865. * Properly fixed reversed operands bug
  1866. Revision 1.12 2003/02/19 22:00:15 daniel
  1867. * Code generator converted to new register notation
  1868. - Horribily outdated todo.txt removed
  1869. Revision 1.11 2003/01/09 20:40:59 daniel
  1870. * Converted some code in cgx86.pas to new register numbering
  1871. Revision 1.10 2003/01/08 18:43:57 daniel
  1872. * Tregister changed into a record
  1873. Revision 1.9 2003/01/05 13:36:53 florian
  1874. * x86-64 compiles
  1875. + very basic support for float128 type (x86-64 only)
  1876. Revision 1.8 2002/11/17 16:31:58 carl
  1877. * memory optimization (3-4%) : cleanup of tai fields,
  1878. cleanup of tdef and tsym fields.
  1879. * make it work for m68k
  1880. Revision 1.7 2002/11/15 01:58:54 peter
  1881. * merged changes from 1.0.7 up to 04-11
  1882. - -V option for generating bug report tracing
  1883. - more tracing for option parsing
  1884. - errors for cdecl and high()
  1885. - win32 import stabs
  1886. - win32 records<=8 are returned in eax:edx (turned off by default)
  1887. - heaptrc update
  1888. - more info for temp management in .s file with EXTDEBUG
  1889. Revision 1.6 2002/10/31 13:28:32 pierre
  1890. * correct last wrong fix for tw2158
  1891. Revision 1.5 2002/10/30 17:10:00 pierre
  1892. * merge of fix for tw2158 bug
  1893. Revision 1.4 2002/08/15 19:10:36 peter
  1894. * first things tai,tnode storing in ppu
  1895. Revision 1.3 2002/08/13 18:01:52 carl
  1896. * rename swatoperands to swapoperands
  1897. + m68k first compilable version (still needs a lot of testing):
  1898. assembler generator, system information , inline
  1899. assembler reader.
  1900. Revision 1.2 2002/07/20 11:57:59 florian
  1901. * types.pas renamed to defbase.pas because D6 contains a types
  1902. unit so this would conflicts if D6 programms are compiled
  1903. + Willamette/SSE2 instructions to assembler added
  1904. Revision 1.1 2002/07/01 18:46:29 peter
  1905. * internal linker
  1906. * reorganized aasm layer
  1907. }