aasmcpu.pas 86 KB

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