aasmcpu.pas 85 KB

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