aasmcpu.pas 70 KB

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