aasmcpu.pas 65 KB

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