aasmcpu.pas 80 KB

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