aasmcpu.pas 62 KB

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