aasmcpu.pas 86 KB

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