aasmcpu.pas 64 KB

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