aasmcpu.pas 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl and Peter Vreman
  3. Contains the abstract assembler implementation for the i386
  4. * Portions of this code was inspired by the NASM sources
  5. The Netwide Assembler is Copyright (c) 1996 Simon Tatham and
  6. Julian Hall. All rights reserved.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ****************************************************************************
  19. }
  20. unit aasmcpu;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. globtype,globals,verbose,
  25. cpubase,
  26. cgbase,cgutils,
  27. symtype,
  28. aasmbase,aasmtai,aasmdata,aasmsym,
  29. ogbase;
  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_SIZE_MASK = $0000001F; { all the size attributes }
  43. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  44. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  45. OT_NEAR = $00000040;
  46. OT_SHORT = $00000080;
  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_XMMREG = $00201010; { Katmai registers }
  65. OT_MMXREG = $00201020; { MMX 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 x8664nop.inc}
  109. {$else x86_64}
  110. instabentries = {$i i386nop.inc}
  111. {$endif x86_64}
  112. maxinfolen = 11;
  113. MaxInsChanges = 3; { Max things a instruction can change }
  114. type
  115. { What an instruction can change. Needed for optimizer and spilling code.
  116. Note: The order of this enumeration is should not be changed! }
  117. TInsChange = (Ch_None,
  118. {Read from a register}
  119. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  120. {write from a register}
  121. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  122. {read and write from/to a register}
  123. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  124. {modify the contents of a register with the purpose of using
  125. this changed content afterwards (add/sub/..., but e.g. not rep
  126. or movsd)}
  127. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  128. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  129. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  130. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  131. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  132. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  133. Ch_WMemEDI,
  134. Ch_All,
  135. { x86_64 registers }
  136. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  137. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  138. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  139. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  140. );
  141. TInsProp = packed record
  142. Ch : Array[1..MaxInsChanges] of TInsChange;
  143. end;
  144. const
  145. InsProp : array[tasmop] of TInsProp =
  146. {$ifdef x86_64}
  147. {$i x8664pro.inc}
  148. {$else x86_64}
  149. {$i i386prop.inc}
  150. {$endif x86_64}
  151. type
  152. TOperandOrder = (op_intel,op_att);
  153. tinsentry=packed record
  154. opcode : tasmop;
  155. ops : byte;
  156. optypes : array[0..2] of longint;
  157. code : array[0..maxinfolen] of char;
  158. flags : longint;
  159. end;
  160. pinsentry=^tinsentry;
  161. { alignment for operator }
  162. tai_align = class(tai_align_abstract)
  163. reg : tregister;
  164. constructor create(b:byte);override;
  165. constructor create_op(b: byte; _op: byte);override;
  166. function calculatefillbuf(var buf : tfillbuffer):pchar;override;
  167. end;
  168. taicpu = class(tai_cpu_abstract_sym)
  169. opsize : topsize;
  170. constructor op_none(op : tasmop);
  171. constructor op_none(op : tasmop;_size : topsize);
  172. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  173. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  174. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  175. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  176. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  177. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  178. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  179. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  180. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  181. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  182. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  183. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  184. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  185. constructor op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister; const _op3 : treference);
  186. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  187. { this is for Jmp instructions }
  188. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  189. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  190. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  191. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  192. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  193. procedure changeopsize(siz:topsize);
  194. function GetString:string;
  195. procedure CheckNonCommutativeOpcodes;
  196. private
  197. FOperandOrder : TOperandOrder;
  198. procedure init(_size : topsize); { this need to be called by all constructor }
  199. public
  200. { the next will reset all instructions that can change in pass 2 }
  201. procedure ResetPass1;override;
  202. procedure ResetPass2;override;
  203. function CheckIfValid:boolean;
  204. function Pass1(objdata:TObjData):longint;override;
  205. procedure Pass2(objdata:TObjData);override;
  206. procedure SetOperandOrder(order:TOperandOrder);
  207. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  208. { register spilling code }
  209. function spilling_get_operation_type(opnr: longint): topertype;override;
  210. private
  211. { next fields are filled in pass1, so pass2 is faster }
  212. insentry : PInsEntry;
  213. insoffset : longint;
  214. LastInsOffset : longint; { need to be public to be reset }
  215. inssize : shortint;
  216. {$ifdef x86_64}
  217. rex : byte;
  218. {$endif x86_64}
  219. function InsEnd:longint;
  220. procedure create_ot(objdata:TObjData);
  221. function Matches(p:PInsEntry):boolean;
  222. function calcsize(p:PInsEntry):shortint;
  223. procedure gencode(objdata:TObjData);
  224. function NeedAddrPrefix(opidx:byte):boolean;
  225. procedure Swapoperands;
  226. function FindInsentry(objdata:TObjData):boolean;
  227. end;
  228. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  229. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  230. procedure InitAsm;
  231. procedure DoneAsm;
  232. implementation
  233. uses
  234. cutils,
  235. itcpugas,
  236. symsym;
  237. {*****************************************************************************
  238. Instruction table
  239. *****************************************************************************}
  240. const
  241. {Instruction flags }
  242. IF_NONE = $00000000;
  243. IF_SM = $00000001; { size match first two operands }
  244. IF_SM2 = $00000002;
  245. IF_SB = $00000004; { unsized operands can't be non-byte }
  246. IF_SW = $00000008; { unsized operands can't be non-word }
  247. IF_SD = $00000010; { unsized operands can't be nondword }
  248. IF_SMASK = $0000001f;
  249. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  250. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  251. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  252. IF_ARMASK = $00000060; { mask for unsized argument spec }
  253. IF_PRIV = $00000100; { it's a privileged instruction }
  254. IF_SMM = $00000200; { it's only valid in SMM }
  255. IF_PROT = $00000400; { it's protected mode only }
  256. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  257. IF_UNDOC = $00001000; { it's an undocumented instruction }
  258. IF_FPU = $00002000; { it's an FPU instruction }
  259. IF_MMX = $00004000; { it's an MMX instruction }
  260. { it's a 3DNow! instruction }
  261. IF_3DNOW = $00008000;
  262. { it's a SSE (KNI, MMX2) instruction }
  263. IF_SSE = $00010000;
  264. { SSE2 instructions }
  265. IF_SSE2 = $00020000;
  266. { SSE3 instructions }
  267. IF_SSE3 = $00040000;
  268. { SSE64 instructions }
  269. IF_SSE64 = $00080000;
  270. { the mask for processor types }
  271. {IF_PMASK = longint($FF000000);}
  272. { the mask for disassembly "prefer" }
  273. {IF_PFMASK = longint($F001FF00);}
  274. { SVM instructions }
  275. IF_SVM = $00100000;
  276. IF_8086 = $00000000; { 8086 instruction }
  277. IF_186 = $01000000; { 186+ instruction }
  278. IF_286 = $02000000; { 286+ instruction }
  279. IF_386 = $03000000; { 386+ instruction }
  280. IF_486 = $04000000; { 486+ instruction }
  281. IF_PENT = $05000000; { Pentium instruction }
  282. IF_P6 = $06000000; { P6 instruction }
  283. IF_KATMAI = $07000000; { Katmai instructions }
  284. { Willamette instructions }
  285. IF_WILLAMETTE = $08000000;
  286. { Prescott instructions }
  287. IF_PRESCOTT = $09000000;
  288. IF_X86_64 = $0a000000;
  289. IF_CYRIX = $0b000000; { Cyrix-specific instruction }
  290. IF_AMD = $0c000000; { AMD-specific instruction }
  291. IF_CENTAUR = $0d000000; { centaur-specific instruction }
  292. { added flags }
  293. IF_PRE = $40000000; { it's a prefix instruction }
  294. IF_PASS2 = longint($80000000); { if the instruction can change in a second pass }
  295. type
  296. TInsTabCache=array[TasmOp] of longint;
  297. PInsTabCache=^TInsTabCache;
  298. const
  299. {$ifdef x86_64}
  300. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  301. {$else x86_64}
  302. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  303. {$endif x86_64}
  304. var
  305. InsTabCache : PInsTabCache;
  306. const
  307. {$ifdef x86_64}
  308. { Intel style operands ! }
  309. opsize_2_type:array[0..2,topsize] of longint=(
  310. (OT_NONE,
  311. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  312. OT_BITS16,OT_BITS32,OT_BITS64,
  313. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  314. OT_BITS64,
  315. OT_NEAR,OT_FAR,OT_SHORT,
  316. OT_NONE,
  317. OT_NONE
  318. ),
  319. (OT_NONE,
  320. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  321. OT_BITS16,OT_BITS32,OT_BITS64,
  322. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  323. OT_BITS64,
  324. OT_NEAR,OT_FAR,OT_SHORT,
  325. OT_NONE,
  326. OT_NONE
  327. ),
  328. (OT_NONE,
  329. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  330. OT_BITS16,OT_BITS32,OT_BITS64,
  331. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  332. OT_BITS64,
  333. OT_NEAR,OT_FAR,OT_SHORT,
  334. OT_NONE,
  335. OT_NONE
  336. )
  337. );
  338. reg_ot_table : array[tregisterindex] of longint = (
  339. {$i r8664ot.inc}
  340. );
  341. {$else x86_64}
  342. { Intel style operands ! }
  343. opsize_2_type:array[0..2,topsize] of longint=(
  344. (OT_NONE,
  345. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  346. OT_BITS16,OT_BITS32,OT_BITS64,
  347. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  348. OT_BITS64,
  349. OT_NEAR,OT_FAR,OT_SHORT,
  350. OT_NONE,
  351. OT_NONE
  352. ),
  353. (OT_NONE,
  354. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  355. OT_BITS16,OT_BITS32,OT_BITS64,
  356. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  357. OT_BITS64,
  358. OT_NEAR,OT_FAR,OT_SHORT,
  359. OT_NONE,
  360. OT_NONE
  361. ),
  362. (OT_NONE,
  363. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  364. OT_BITS16,OT_BITS32,OT_BITS64,
  365. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  366. OT_BITS64,
  367. OT_NEAR,OT_FAR,OT_SHORT,
  368. OT_NONE,
  369. OT_NONE
  370. )
  371. );
  372. reg_ot_table : array[tregisterindex] of longint = (
  373. {$i r386ot.inc}
  374. );
  375. {$endif x86_64}
  376. { Operation type for spilling code }
  377. type
  378. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  379. var
  380. operation_type_table : ^toperation_type_table;
  381. {****************************************************************************
  382. TAI_ALIGN
  383. ****************************************************************************}
  384. constructor tai_align.create(b: byte);
  385. begin
  386. inherited create(b);
  387. reg:=NR_ECX;
  388. end;
  389. constructor tai_align.create_op(b: byte; _op: byte);
  390. begin
  391. inherited create_op(b,_op);
  392. reg:=NR_NO;
  393. end;
  394. function tai_align.calculatefillbuf(var buf : tfillbuffer):pchar;
  395. const
  396. {$ifdef x86_64}
  397. alignarray:array[0..3] of string[4]=(
  398. #$66#$66#$66#$90,
  399. #$66#$66#$90,
  400. #$66#$90,
  401. #$90
  402. );
  403. {$else x86_64}
  404. alignarray:array[0..5] of string[8]=(
  405. #$8D#$B4#$26#$00#$00#$00#$00,
  406. #$8D#$B6#$00#$00#$00#$00,
  407. #$8D#$74#$26#$00,
  408. #$8D#$76#$00,
  409. #$89#$F6,
  410. #$90);
  411. {$endif x86_64}
  412. var
  413. bufptr : pchar;
  414. j : longint;
  415. begin
  416. inherited calculatefillbuf(buf);
  417. if not use_op then
  418. begin
  419. bufptr:=pchar(@buf);
  420. while (fillsize>0) do
  421. begin
  422. for j:=low(alignarray) to high(alignarray) do
  423. if (fillsize>=length(alignarray[j])) then
  424. break;
  425. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  426. inc(bufptr,length(alignarray[j]));
  427. dec(fillsize,length(alignarray[j]));
  428. end;
  429. end;
  430. calculatefillbuf:=pchar(@buf);
  431. end;
  432. {*****************************************************************************
  433. Taicpu Constructors
  434. *****************************************************************************}
  435. procedure taicpu.changeopsize(siz:topsize);
  436. begin
  437. opsize:=siz;
  438. end;
  439. procedure taicpu.init(_size : topsize);
  440. begin
  441. { default order is att }
  442. FOperandOrder:=op_att;
  443. segprefix:=NR_NO;
  444. opsize:=_size;
  445. insentry:=nil;
  446. LastInsOffset:=-1;
  447. InsOffset:=0;
  448. InsSize:=0;
  449. end;
  450. constructor taicpu.op_none(op : tasmop);
  451. begin
  452. inherited create(op);
  453. init(S_NO);
  454. end;
  455. constructor taicpu.op_none(op : tasmop;_size : topsize);
  456. begin
  457. inherited create(op);
  458. init(_size);
  459. end;
  460. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  461. begin
  462. inherited create(op);
  463. init(_size);
  464. ops:=1;
  465. loadreg(0,_op1);
  466. end;
  467. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  468. begin
  469. inherited create(op);
  470. init(_size);
  471. ops:=1;
  472. loadconst(0,_op1);
  473. end;
  474. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  475. begin
  476. inherited create(op);
  477. init(_size);
  478. ops:=1;
  479. loadref(0,_op1);
  480. end;
  481. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  482. begin
  483. inherited create(op);
  484. init(_size);
  485. ops:=2;
  486. loadreg(0,_op1);
  487. loadreg(1,_op2);
  488. end;
  489. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  490. begin
  491. inherited create(op);
  492. init(_size);
  493. ops:=2;
  494. loadreg(0,_op1);
  495. loadconst(1,_op2);
  496. end;
  497. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  498. begin
  499. inherited create(op);
  500. init(_size);
  501. ops:=2;
  502. loadreg(0,_op1);
  503. loadref(1,_op2);
  504. end;
  505. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  506. begin
  507. inherited create(op);
  508. init(_size);
  509. ops:=2;
  510. loadconst(0,_op1);
  511. loadreg(1,_op2);
  512. end;
  513. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  514. begin
  515. inherited create(op);
  516. init(_size);
  517. ops:=2;
  518. loadconst(0,_op1);
  519. loadconst(1,_op2);
  520. end;
  521. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  522. begin
  523. inherited create(op);
  524. init(_size);
  525. ops:=2;
  526. loadconst(0,_op1);
  527. loadref(1,_op2);
  528. end;
  529. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  530. begin
  531. inherited create(op);
  532. init(_size);
  533. ops:=2;
  534. loadref(0,_op1);
  535. loadreg(1,_op2);
  536. end;
  537. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  538. begin
  539. inherited create(op);
  540. init(_size);
  541. ops:=3;
  542. loadreg(0,_op1);
  543. loadreg(1,_op2);
  544. loadreg(2,_op3);
  545. end;
  546. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  547. begin
  548. inherited create(op);
  549. init(_size);
  550. ops:=3;
  551. loadconst(0,_op1);
  552. loadreg(1,_op2);
  553. loadreg(2,_op3);
  554. end;
  555. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  556. begin
  557. inherited create(op);
  558. init(_size);
  559. ops:=3;
  560. loadreg(0,_op1);
  561. loadreg(1,_op2);
  562. loadref(2,_op3);
  563. end;
  564. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  565. begin
  566. inherited create(op);
  567. init(_size);
  568. ops:=3;
  569. loadconst(0,_op1);
  570. loadref(1,_op2);
  571. loadreg(2,_op3);
  572. end;
  573. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  574. begin
  575. inherited create(op);
  576. init(_size);
  577. ops:=3;
  578. loadconst(0,_op1);
  579. loadreg(1,_op2);
  580. loadref(2,_op3);
  581. end;
  582. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  583. begin
  584. inherited create(op);
  585. init(_size);
  586. condition:=cond;
  587. ops:=1;
  588. loadsymbol(0,_op1,0);
  589. end;
  590. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  591. begin
  592. inherited create(op);
  593. init(_size);
  594. ops:=1;
  595. loadsymbol(0,_op1,0);
  596. end;
  597. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  598. begin
  599. inherited create(op);
  600. init(_size);
  601. ops:=1;
  602. loadsymbol(0,_op1,_op1ofs);
  603. end;
  604. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  605. begin
  606. inherited create(op);
  607. init(_size);
  608. ops:=2;
  609. loadsymbol(0,_op1,_op1ofs);
  610. loadreg(1,_op2);
  611. end;
  612. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  613. begin
  614. inherited create(op);
  615. init(_size);
  616. ops:=2;
  617. loadsymbol(0,_op1,_op1ofs);
  618. loadref(1,_op2);
  619. end;
  620. function taicpu.GetString:string;
  621. var
  622. i : longint;
  623. s : string;
  624. addsize : boolean;
  625. begin
  626. s:='['+std_op2str[opcode];
  627. for i:=0 to ops-1 do
  628. begin
  629. with oper[i]^ do
  630. begin
  631. if i=0 then
  632. s:=s+' '
  633. else
  634. s:=s+',';
  635. { type }
  636. addsize:=false;
  637. if (ot and OT_XMMREG)=OT_XMMREG then
  638. s:=s+'xmmreg'
  639. else
  640. if (ot and OT_MMXREG)=OT_MMXREG then
  641. s:=s+'mmxreg'
  642. else
  643. if (ot and OT_FPUREG)=OT_FPUREG then
  644. s:=s+'fpureg'
  645. else
  646. if (ot and OT_REGISTER)=OT_REGISTER then
  647. begin
  648. s:=s+'reg';
  649. addsize:=true;
  650. end
  651. else
  652. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  653. begin
  654. s:=s+'imm';
  655. addsize:=true;
  656. end
  657. else
  658. if (ot and OT_MEMORY)=OT_MEMORY then
  659. begin
  660. s:=s+'mem';
  661. addsize:=true;
  662. end
  663. else
  664. s:=s+'???';
  665. { size }
  666. if addsize then
  667. begin
  668. if (ot and OT_BITS8)<>0 then
  669. s:=s+'8'
  670. else
  671. if (ot and OT_BITS16)<>0 then
  672. s:=s+'16'
  673. else
  674. if (ot and OT_BITS32)<>0 then
  675. s:=s+'32'
  676. else
  677. if (ot and OT_BITS64)<>0 then
  678. s:=s+'64'
  679. else
  680. s:=s+'??';
  681. { signed }
  682. if (ot and OT_SIGNED)<>0 then
  683. s:=s+'s';
  684. end;
  685. end;
  686. end;
  687. GetString:=s+']';
  688. end;
  689. procedure taicpu.Swapoperands;
  690. var
  691. p : POper;
  692. begin
  693. { Fix the operands which are in AT&T style and we need them in Intel style }
  694. case ops of
  695. 2 : begin
  696. { 0,1 -> 1,0 }
  697. p:=oper[0];
  698. oper[0]:=oper[1];
  699. oper[1]:=p;
  700. end;
  701. 3 : begin
  702. { 0,1,2 -> 2,1,0 }
  703. p:=oper[0];
  704. oper[0]:=oper[2];
  705. oper[2]:=p;
  706. end;
  707. end;
  708. end;
  709. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  710. begin
  711. if FOperandOrder<>order then
  712. begin
  713. Swapoperands;
  714. FOperandOrder:=order;
  715. end;
  716. end;
  717. procedure taicpu.CheckNonCommutativeOpcodes;
  718. begin
  719. { we need ATT order }
  720. SetOperandOrder(op_att);
  721. if (
  722. (ops=2) and
  723. (oper[0]^.typ=top_reg) and
  724. (oper[1]^.typ=top_reg) and
  725. { if the first is ST and the second is also a register
  726. it is necessarily ST1 .. ST7 }
  727. ((oper[0]^.reg=NR_ST) or
  728. (oper[0]^.reg=NR_ST0))
  729. ) or
  730. { ((ops=1) and
  731. (oper[0]^.typ=top_reg) and
  732. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  733. (ops=0) then
  734. begin
  735. if opcode=A_FSUBR then
  736. opcode:=A_FSUB
  737. else if opcode=A_FSUB then
  738. opcode:=A_FSUBR
  739. else if opcode=A_FDIVR then
  740. opcode:=A_FDIV
  741. else if opcode=A_FDIV then
  742. opcode:=A_FDIVR
  743. else if opcode=A_FSUBRP then
  744. opcode:=A_FSUBP
  745. else if opcode=A_FSUBP then
  746. opcode:=A_FSUBRP
  747. else if opcode=A_FDIVRP then
  748. opcode:=A_FDIVP
  749. else if opcode=A_FDIVP then
  750. opcode:=A_FDIVRP;
  751. end;
  752. if (
  753. (ops=1) and
  754. (oper[0]^.typ=top_reg) and
  755. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  756. (oper[0]^.reg<>NR_ST)
  757. ) then
  758. begin
  759. if opcode=A_FSUBRP then
  760. opcode:=A_FSUBP
  761. else if opcode=A_FSUBP then
  762. opcode:=A_FSUBRP
  763. else if opcode=A_FDIVRP then
  764. opcode:=A_FDIVP
  765. else if opcode=A_FDIVP then
  766. opcode:=A_FDIVRP;
  767. end;
  768. end;
  769. {*****************************************************************************
  770. Assembler
  771. *****************************************************************************}
  772. type
  773. ea = packed record
  774. sib_present : boolean;
  775. bytes : byte;
  776. size : byte;
  777. modrm : byte;
  778. sib : byte;
  779. {$ifdef x86_64}
  780. rex_present : boolean;
  781. rex : byte;
  782. {$endif x86_64}
  783. end;
  784. procedure taicpu.create_ot(objdata:TObjData);
  785. {
  786. this function will also fix some other fields which only needs to be once
  787. }
  788. var
  789. i,l,relsize : longint;
  790. currsym : TObjSymbol;
  791. begin
  792. if ops=0 then
  793. exit;
  794. { update oper[].ot field }
  795. for i:=0 to ops-1 do
  796. with oper[i]^ do
  797. begin
  798. case typ of
  799. top_reg :
  800. begin
  801. ot:=reg_ot_table[findreg_by_number(reg)];
  802. end;
  803. top_ref :
  804. begin
  805. if (ref^.refaddr=addr_no)
  806. {$ifdef x86_64}
  807. or (
  808. (ref^.refaddr=addr_pic) and
  809. (ref^.base<>NR_NO)
  810. )
  811. {$endif x86_64}
  812. then
  813. begin
  814. { create ot field }
  815. if (ot and OT_SIZE_MASK)=0 then
  816. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  817. else
  818. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  819. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  820. ot:=ot or OT_MEM_OFFS;
  821. { fix scalefactor }
  822. if (ref^.index=NR_NO) then
  823. ref^.scalefactor:=0
  824. else
  825. if (ref^.scalefactor=0) then
  826. ref^.scalefactor:=1;
  827. end
  828. else
  829. begin
  830. if assigned(objdata) then
  831. begin
  832. currsym:=objdata.symbolref(ref^.symbol);
  833. l:=ref^.offset;
  834. if assigned(currsym) then
  835. inc(l,currsym.address);
  836. { when it is a forward jump we need to compensate the
  837. offset of the instruction since the previous time,
  838. because the symbol address is then still using the
  839. 'old-style' addressing.
  840. For backwards jumps this is not required because the
  841. address of the symbol is already adjusted to the
  842. new offset }
  843. if (l>InsOffset) and (LastInsOffset<>-1) then
  844. inc(l,InsOffset-LastInsOffset);
  845. { instruction size will then always become 2 (PFV) }
  846. relsize:=(InsOffset+2)-l;
  847. if (relsize>=-128) and (relsize<=127) and
  848. (
  849. not assigned(currsym) or
  850. (currsym.objsection=objdata.currobjsec)
  851. ) then
  852. ot:=OT_IMM8 or OT_SHORT
  853. else
  854. ot:=OT_IMM32 or OT_NEAR;
  855. end
  856. else
  857. ot:=OT_IMM32 or OT_NEAR;
  858. end;
  859. end;
  860. top_local :
  861. begin
  862. if (ot and OT_SIZE_MASK)=0 then
  863. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  864. else
  865. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  866. end;
  867. top_const :
  868. begin
  869. { allow 2nd or 3rd operand being a constant and expect no size for shuf* etc. }
  870. if (opsize=S_NO) and not(i in [1,2]) then
  871. message(asmr_e_invalid_opcode_and_operand);
  872. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  873. ot:=OT_IMM8 or OT_SIGNED
  874. else
  875. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  876. end;
  877. top_none :
  878. begin
  879. { generated when there was an error in the
  880. assembler reader. It never happends when generating
  881. assembler }
  882. end;
  883. else
  884. internalerror(200402261);
  885. end;
  886. end;
  887. end;
  888. function taicpu.InsEnd:longint;
  889. begin
  890. InsEnd:=InsOffset+InsSize;
  891. end;
  892. function taicpu.Matches(p:PInsEntry):boolean;
  893. { * IF_SM stands for Size Match: any operand whose size is not
  894. * explicitly specified by the template is `really' intended to be
  895. * the same size as the first size-specified operand.
  896. * Non-specification is tolerated in the input instruction, but
  897. * _wrong_ specification is not.
  898. *
  899. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  900. * three-operand instructions such as SHLD: it implies that the
  901. * first two operands must match in size, but that the third is
  902. * required to be _unspecified_.
  903. *
  904. * IF_SB invokes Size Byte: operands with unspecified size in the
  905. * template are really bytes, and so no non-byte specification in
  906. * the input instruction will be tolerated. IF_SW similarly invokes
  907. * Size Word, and IF_SD invokes Size Doubleword.
  908. *
  909. * (The default state if neither IF_SM nor IF_SM2 is specified is
  910. * that any operand with unspecified size in the template is
  911. * required to have unspecified size in the instruction too...)
  912. }
  913. var
  914. insot,
  915. insflags,
  916. currot,
  917. i,j,asize,oprs : longint;
  918. siz : array[0..2] of longint;
  919. begin
  920. result:=false;
  921. { Check the opcode and operands }
  922. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  923. exit;
  924. for i:=0 to p^.ops-1 do
  925. begin
  926. insot:=p^.optypes[i];
  927. currot:=oper[i]^.ot;
  928. { Check the operand flags }
  929. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  930. exit;
  931. { Check if the passed operand size matches with one of
  932. the supported operand sizes }
  933. if ((insot and OT_SIZE_MASK)<>0) and
  934. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  935. exit;
  936. end;
  937. { Check operand sizes }
  938. insflags:=p^.flags;
  939. if insflags and IF_SMASK<>0 then
  940. begin
  941. { as default an untyped size can get all the sizes, this is different
  942. from nasm, but else we need to do a lot checking which opcodes want
  943. size or not with the automatic size generation }
  944. asize:=-1;
  945. if (insflags and IF_SB)<>0 then
  946. asize:=OT_BITS8
  947. else if (insflags and IF_SW)<>0 then
  948. asize:=OT_BITS16
  949. else if (insflags and IF_SD)<>0 then
  950. asize:=OT_BITS32;
  951. if (insflags and IF_ARMASK)<>0 then
  952. begin
  953. siz[0]:=0;
  954. siz[1]:=0;
  955. siz[2]:=0;
  956. if (insflags and IF_AR0)<>0 then
  957. siz[0]:=asize
  958. else if (insflags and IF_AR1)<>0 then
  959. siz[1]:=asize
  960. else if (insflags and IF_AR2)<>0 then
  961. siz[2]:=asize;
  962. end
  963. else
  964. begin
  965. siz[0]:=asize;
  966. siz[1]:=asize;
  967. siz[2]:=asize;
  968. end;
  969. if (insflags and (IF_SM or IF_SM2))<>0 then
  970. begin
  971. if (insflags and IF_SM2)<>0 then
  972. oprs:=2
  973. else
  974. oprs:=p^.ops;
  975. for i:=0 to oprs-1 do
  976. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  977. begin
  978. for j:=0 to oprs-1 do
  979. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  980. break;
  981. end;
  982. end
  983. else
  984. oprs:=2;
  985. { Check operand sizes }
  986. for i:=0 to p^.ops-1 do
  987. begin
  988. insot:=p^.optypes[i];
  989. currot:=oper[i]^.ot;
  990. if ((insot and OT_SIZE_MASK)=0) and
  991. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  992. { Immediates can always include smaller size }
  993. ((currot and OT_IMMEDIATE)=0) and
  994. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  995. exit;
  996. end;
  997. end;
  998. result:=true;
  999. end;
  1000. procedure taicpu.ResetPass1;
  1001. begin
  1002. { we need to reset everything here, because the choosen insentry
  1003. can be invalid for a new situation where the previously optimized
  1004. insentry is not correct }
  1005. InsEntry:=nil;
  1006. InsSize:=0;
  1007. LastInsOffset:=-1;
  1008. end;
  1009. procedure taicpu.ResetPass2;
  1010. begin
  1011. { we are here in a second pass, check if the instruction can be optimized }
  1012. if assigned(InsEntry) and
  1013. ((InsEntry^.flags and IF_PASS2)<>0) then
  1014. begin
  1015. InsEntry:=nil;
  1016. InsSize:=0;
  1017. end;
  1018. LastInsOffset:=-1;
  1019. end;
  1020. function taicpu.CheckIfValid:boolean;
  1021. begin
  1022. result:=FindInsEntry(nil);
  1023. end;
  1024. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1025. var
  1026. i : longint;
  1027. begin
  1028. result:=false;
  1029. { Things which may only be done once, not when a second pass is done to
  1030. optimize }
  1031. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1032. begin
  1033. { We need intel style operands }
  1034. SetOperandOrder(op_intel);
  1035. { create the .ot fields }
  1036. create_ot(objdata);
  1037. { set the file postion }
  1038. current_filepos:=fileinfo;
  1039. end
  1040. else
  1041. begin
  1042. { we've already an insentry so it's valid }
  1043. result:=true;
  1044. exit;
  1045. end;
  1046. { Lookup opcode in the table }
  1047. InsSize:=-1;
  1048. i:=instabcache^[opcode];
  1049. if i=-1 then
  1050. begin
  1051. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1052. exit;
  1053. end;
  1054. insentry:=@instab[i];
  1055. while (insentry^.opcode=opcode) do
  1056. begin
  1057. if matches(insentry) then
  1058. begin
  1059. result:=true;
  1060. exit;
  1061. end;
  1062. inc(insentry);
  1063. end;
  1064. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1065. { No instruction found, set insentry to nil and inssize to -1 }
  1066. insentry:=nil;
  1067. inssize:=-1;
  1068. end;
  1069. function taicpu.Pass1(objdata:TObjData):longint;
  1070. begin
  1071. Pass1:=0;
  1072. { Save the old offset and set the new offset }
  1073. InsOffset:=ObjData.CurrObjSec.Size;
  1074. { Error? }
  1075. if (Insentry=nil) and (InsSize=-1) then
  1076. exit;
  1077. { set the file postion }
  1078. current_filepos:=fileinfo;
  1079. { Get InsEntry }
  1080. if FindInsEntry(ObjData) then
  1081. begin
  1082. { Calculate instruction size }
  1083. InsSize:=calcsize(insentry);
  1084. if segprefix<>NR_NO then
  1085. inc(InsSize);
  1086. { Fix opsize if size if forced }
  1087. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1088. begin
  1089. if (insentry^.flags and IF_ARMASK)=0 then
  1090. begin
  1091. if (insentry^.flags and IF_SB)<>0 then
  1092. begin
  1093. if opsize=S_NO then
  1094. opsize:=S_B;
  1095. end
  1096. else if (insentry^.flags and IF_SW)<>0 then
  1097. begin
  1098. if opsize=S_NO then
  1099. opsize:=S_W;
  1100. end
  1101. else if (insentry^.flags and IF_SD)<>0 then
  1102. begin
  1103. if opsize=S_NO then
  1104. opsize:=S_L;
  1105. end;
  1106. end;
  1107. end;
  1108. LastInsOffset:=InsOffset;
  1109. Pass1:=InsSize;
  1110. exit;
  1111. end;
  1112. LastInsOffset:=-1;
  1113. end;
  1114. procedure taicpu.Pass2(objdata:TObjData);
  1115. var
  1116. c : longint;
  1117. begin
  1118. { error in pass1 ? }
  1119. if insentry=nil then
  1120. exit;
  1121. current_filepos:=fileinfo;
  1122. { Segment override }
  1123. if (segprefix<>NR_NO) then
  1124. begin
  1125. case segprefix of
  1126. NR_CS : c:=$2e;
  1127. NR_DS : c:=$3e;
  1128. NR_ES : c:=$26;
  1129. NR_FS : c:=$64;
  1130. NR_GS : c:=$65;
  1131. NR_SS : c:=$36;
  1132. end;
  1133. objdata.writebytes(c,1);
  1134. { fix the offset for GenNode }
  1135. inc(InsOffset);
  1136. end;
  1137. { Generate the instruction }
  1138. GenCode(objdata);
  1139. end;
  1140. function taicpu.needaddrprefix(opidx:byte):boolean;
  1141. begin
  1142. result:=(oper[opidx]^.typ=top_ref) and
  1143. (oper[opidx]^.ref^.refaddr=addr_no) and
  1144. (
  1145. (
  1146. (oper[opidx]^.ref^.index<>NR_NO) and
  1147. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1148. ) or
  1149. (
  1150. (oper[opidx]^.ref^.base<>NR_NO) and
  1151. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1152. )
  1153. );
  1154. end;
  1155. function regval(r:Tregister):byte;
  1156. const
  1157. {$ifdef x86_64}
  1158. opcode_table:array[tregisterindex] of tregisterindex = (
  1159. {$i r8664op.inc}
  1160. );
  1161. {$else x86_64}
  1162. opcode_table:array[tregisterindex] of tregisterindex = (
  1163. {$i r386op.inc}
  1164. );
  1165. {$endif x86_64}
  1166. var
  1167. regidx : tregisterindex;
  1168. begin
  1169. regidx:=findreg_by_number(r);
  1170. if regidx<>0 then
  1171. result:=opcode_table[regidx]
  1172. else
  1173. begin
  1174. Message1(asmw_e_invalid_register,generic_regname(r));
  1175. result:=0;
  1176. end;
  1177. end;
  1178. {$ifdef x86_64}
  1179. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1180. var
  1181. sym : tasmsymbol;
  1182. md,s,rv : byte;
  1183. base,index,scalefactor,
  1184. o : longint;
  1185. ir,br : Tregister;
  1186. isub,bsub : tsubregister;
  1187. begin
  1188. process_ea:=false;
  1189. fillchar(output,sizeof(output),0);
  1190. {Register ?}
  1191. if (input.typ=top_reg) then
  1192. begin
  1193. rv:=regval(input.reg);
  1194. output.modrm:=$c0 or (rfield shl 3) or rv;
  1195. output.size:=1;
  1196. if ((getregtype(input.reg)=R_INTREGISTER) and
  1197. (getsupreg(input.reg)>=RS_R8)) or
  1198. ((getregtype(input.reg)=R_MMREGISTER) and
  1199. (getsupreg(input.reg)>=RS_XMM8)) then
  1200. begin
  1201. output.rex_present:=true;
  1202. output.rex:=output.rex or $41;
  1203. inc(output.size,1);
  1204. end
  1205. else if (getregtype(input.reg)=R_INTREGISTER) and
  1206. (getsubreg(input.reg)=R_SUBL) and
  1207. (getsupreg(input.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1208. begin
  1209. output.rex_present:=true;
  1210. output.rex:=output.rex or $40;
  1211. inc(output.size,1);
  1212. end;
  1213. process_ea:=true;
  1214. exit;
  1215. end;
  1216. {No register, so memory reference.}
  1217. if input.typ<>top_ref then
  1218. internalerror(200409263);
  1219. ir:=input.ref^.index;
  1220. br:=input.ref^.base;
  1221. isub:=getsubreg(ir);
  1222. bsub:=getsubreg(br);
  1223. s:=input.ref^.scalefactor;
  1224. o:=input.ref^.offset;
  1225. sym:=input.ref^.symbol;
  1226. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1227. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1228. internalerror(200301081);
  1229. { it's direct address }
  1230. if (br=NR_NO) and (ir=NR_NO) then
  1231. begin
  1232. output.sib_present:=true;
  1233. output.bytes:=4;
  1234. output.modrm:=4 or (rfield shl 3);
  1235. output.sib:=$25;
  1236. end
  1237. else if (br=NR_RIP) and (ir=NR_NO) then
  1238. begin
  1239. { rip based }
  1240. output.sib_present:=false;
  1241. output.bytes:=4;
  1242. output.modrm:=5 or (rfield shl 3);
  1243. end
  1244. else
  1245. { it's an indirection }
  1246. begin
  1247. { 16 bit or 32 bit address? }
  1248. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1249. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1250. message(asmw_e_16bit_32bit_not_supported);
  1251. { wrong, for various reasons }
  1252. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1253. exit;
  1254. if ((getregtype(br)=R_INTREGISTER) and
  1255. (getsupreg(br)>=RS_R8)) or
  1256. ((getregtype(br)=R_MMREGISTER) and
  1257. (getsupreg(br)>=RS_XMM8)) then
  1258. begin
  1259. output.rex_present:=true;
  1260. output.rex:=output.rex or $41;
  1261. end;
  1262. if ((getregtype(ir)=R_INTREGISTER) and
  1263. (getsupreg(ir)>=RS_R8)) or
  1264. ((getregtype(ir)=R_MMREGISTER) and
  1265. (getsupreg(ir)>=RS_XMM8)) then
  1266. begin
  1267. output.rex_present:=true;
  1268. output.rex:=output.rex or $42;
  1269. end;
  1270. process_ea:=true;
  1271. { base }
  1272. case br of
  1273. NR_R8,
  1274. NR_RAX : base:=0;
  1275. NR_R9,
  1276. NR_RCX : base:=1;
  1277. NR_R10,
  1278. NR_RDX : base:=2;
  1279. NR_R11,
  1280. NR_RBX : base:=3;
  1281. NR_R12,
  1282. NR_RSP : base:=4;
  1283. NR_R13,
  1284. NR_NO,
  1285. NR_RBP : base:=5;
  1286. NR_R14,
  1287. NR_RSI : base:=6;
  1288. NR_R15,
  1289. NR_RDI : base:=7;
  1290. else
  1291. exit;
  1292. end;
  1293. { index }
  1294. case ir of
  1295. NR_R8,
  1296. NR_RAX : index:=0;
  1297. NR_R9,
  1298. NR_RCX : index:=1;
  1299. NR_R10,
  1300. NR_RDX : index:=2;
  1301. NR_R11,
  1302. NR_RBX : index:=3;
  1303. NR_R12,
  1304. NR_NO : index:=4;
  1305. NR_R13,
  1306. NR_RBP : index:=5;
  1307. NR_R14,
  1308. NR_RSI : index:=6;
  1309. NR_R15,
  1310. NR_RDI : index:=7;
  1311. else
  1312. exit;
  1313. end;
  1314. case s of
  1315. 0,
  1316. 1 : scalefactor:=0;
  1317. 2 : scalefactor:=1;
  1318. 4 : scalefactor:=2;
  1319. 8 : scalefactor:=3;
  1320. else
  1321. exit;
  1322. end;
  1323. { If rbp or r13 is used we must always include an offset }
  1324. if (br=NR_NO) or
  1325. ((br<>NR_RBP) and (br<>NR_R13) and (o=0) and (sym=nil)) then
  1326. md:=0
  1327. else
  1328. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1329. md:=1
  1330. else
  1331. md:=2;
  1332. if (br=NR_NO) or (md=2) then
  1333. output.bytes:=4
  1334. else
  1335. output.bytes:=md;
  1336. { SIB needed ? }
  1337. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) then
  1338. begin
  1339. output.sib_present:=false;
  1340. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1341. end
  1342. else
  1343. begin
  1344. output.sib_present:=true;
  1345. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1346. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1347. end;
  1348. end;
  1349. output.size:=1+ord(output.sib_present)+ord(output.rex_present)+output.bytes;
  1350. process_ea:=true;
  1351. end;
  1352. {$else x86_64}
  1353. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1354. var
  1355. sym : tasmsymbol;
  1356. md,s,rv : byte;
  1357. base,index,scalefactor,
  1358. o : longint;
  1359. ir,br : Tregister;
  1360. isub,bsub : tsubregister;
  1361. begin
  1362. process_ea:=false;
  1363. fillchar(output,sizeof(output),0);
  1364. {Register ?}
  1365. if (input.typ=top_reg) then
  1366. begin
  1367. rv:=regval(input.reg);
  1368. output.modrm:=$c0 or (rfield shl 3) or rv;
  1369. output.size:=1;
  1370. process_ea:=true;
  1371. exit;
  1372. end;
  1373. {No register, so memory reference.}
  1374. if (input.typ<>top_ref) then
  1375. internalerror(200409262);
  1376. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1377. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1378. internalerror(200301081);
  1379. ir:=input.ref^.index;
  1380. br:=input.ref^.base;
  1381. isub:=getsubreg(ir);
  1382. bsub:=getsubreg(br);
  1383. s:=input.ref^.scalefactor;
  1384. o:=input.ref^.offset;
  1385. sym:=input.ref^.symbol;
  1386. { it's direct address }
  1387. if (br=NR_NO) and (ir=NR_NO) then
  1388. begin
  1389. { it's a pure offset }
  1390. output.sib_present:=false;
  1391. output.bytes:=4;
  1392. output.modrm:=5 or (rfield shl 3);
  1393. end
  1394. else
  1395. { it's an indirection }
  1396. begin
  1397. { 16 bit address? }
  1398. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1399. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1400. message(asmw_e_16bit_not_supported);
  1401. {$ifdef OPTEA}
  1402. { make single reg base }
  1403. if (br=NR_NO) and (s=1) then
  1404. begin
  1405. br:=ir;
  1406. ir:=NR_NO;
  1407. end;
  1408. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1409. if (br=NR_NO) and
  1410. (((s=2) and (ir<>NR_ESP)) or
  1411. (s=3) or (s=5) or (s=9)) then
  1412. begin
  1413. br:=ir;
  1414. dec(s);
  1415. end;
  1416. { swap ESP into base if scalefactor is 1 }
  1417. if (s=1) and (ir=NR_ESP) then
  1418. begin
  1419. ir:=br;
  1420. br:=NR_ESP;
  1421. end;
  1422. {$endif OPTEA}
  1423. { wrong, for various reasons }
  1424. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1425. exit;
  1426. { base }
  1427. case br of
  1428. NR_EAX : base:=0;
  1429. NR_ECX : base:=1;
  1430. NR_EDX : base:=2;
  1431. NR_EBX : base:=3;
  1432. NR_ESP : base:=4;
  1433. NR_NO,
  1434. NR_EBP : base:=5;
  1435. NR_ESI : base:=6;
  1436. NR_EDI : base:=7;
  1437. else
  1438. exit;
  1439. end;
  1440. { index }
  1441. case ir of
  1442. NR_EAX : index:=0;
  1443. NR_ECX : index:=1;
  1444. NR_EDX : index:=2;
  1445. NR_EBX : index:=3;
  1446. NR_NO : index:=4;
  1447. NR_EBP : index:=5;
  1448. NR_ESI : index:=6;
  1449. NR_EDI : index:=7;
  1450. else
  1451. exit;
  1452. end;
  1453. case s of
  1454. 0,
  1455. 1 : scalefactor:=0;
  1456. 2 : scalefactor:=1;
  1457. 4 : scalefactor:=2;
  1458. 8 : scalefactor:=3;
  1459. else
  1460. exit;
  1461. end;
  1462. if (br=NR_NO) or
  1463. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1464. md:=0
  1465. else
  1466. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1467. md:=1
  1468. else
  1469. md:=2;
  1470. if (br=NR_NO) or (md=2) then
  1471. output.bytes:=4
  1472. else
  1473. output.bytes:=md;
  1474. { SIB needed ? }
  1475. if (ir=NR_NO) and (br<>NR_ESP) then
  1476. begin
  1477. output.sib_present:=false;
  1478. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1479. end
  1480. else
  1481. begin
  1482. output.sib_present:=true;
  1483. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1484. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1485. end;
  1486. end;
  1487. if output.sib_present then
  1488. output.size:=2+output.bytes
  1489. else
  1490. output.size:=1+output.bytes;
  1491. process_ea:=true;
  1492. end;
  1493. {$endif x86_64}
  1494. function taicpu.calcsize(p:PInsEntry):shortint;
  1495. var
  1496. codes : pchar;
  1497. c : byte;
  1498. len : shortint;
  1499. ea_data : ea;
  1500. begin
  1501. len:=0;
  1502. codes:=@p^.code[0];
  1503. {$ifdef x86_64}
  1504. rex:=0;
  1505. {$endif x86_64}
  1506. repeat
  1507. c:=ord(codes^);
  1508. inc(codes);
  1509. case c of
  1510. 0 :
  1511. break;
  1512. 1,2,3 :
  1513. begin
  1514. inc(codes,c);
  1515. inc(len,c);
  1516. end;
  1517. 8,9,10 :
  1518. begin
  1519. {$ifdef x86_64}
  1520. if ((getregtype(oper[c-8]^.reg)=R_INTREGISTER) and
  1521. (getsupreg(oper[c-8]^.reg)>=RS_R8)) or
  1522. ((getregtype(oper[c-8]^.reg)=R_MMREGISTER) and
  1523. (getsupreg(oper[c-8]^.reg)>=RS_XMM8)) then
  1524. begin
  1525. if rex=0 then
  1526. inc(len);
  1527. rex:=rex or $41;
  1528. end
  1529. else if (getregtype(oper[c-8]^.reg)=R_INTREGISTER) and
  1530. (getsubreg(oper[c-8]^.reg)=R_SUBL) and
  1531. (getsupreg(oper[c-8]^.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1532. begin
  1533. if rex=0 then
  1534. inc(len);
  1535. rex:=rex or $40;
  1536. end;
  1537. {$endif x86_64}
  1538. inc(codes);
  1539. inc(len);
  1540. end;
  1541. 11 :
  1542. begin
  1543. inc(codes);
  1544. inc(len);
  1545. end;
  1546. 4,5,6,7 :
  1547. begin
  1548. if opsize=S_W then
  1549. inc(len,2)
  1550. else
  1551. inc(len);
  1552. end;
  1553. 15,
  1554. 12,13,14,
  1555. 16,17,18,
  1556. 20,21,22,
  1557. 40,41,42 :
  1558. inc(len);
  1559. 24,25,26,
  1560. 31,
  1561. 48,49,50 :
  1562. inc(len,2);
  1563. 28,29,30:
  1564. begin
  1565. if opsize=S_Q then
  1566. inc(len,8)
  1567. else
  1568. inc(len,4);
  1569. end;
  1570. 32,33,34,
  1571. 52,53,54,
  1572. 56,57,58 :
  1573. inc(len,4);
  1574. 192,193,194 :
  1575. if NeedAddrPrefix(c-192) then
  1576. inc(len);
  1577. 208,209,210 :
  1578. begin
  1579. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1580. OT_BITS16:
  1581. inc(len);
  1582. {$ifdef x86_64}
  1583. OT_BITS64:
  1584. begin
  1585. if rex=0 then
  1586. inc(len);
  1587. rex:=rex or $48;
  1588. end;
  1589. {$endif x86_64}
  1590. end;
  1591. end;
  1592. 200,
  1593. 212 :
  1594. inc(len);
  1595. 214 :
  1596. begin
  1597. {$ifdef x86_64}
  1598. if rex=0 then
  1599. inc(len);
  1600. rex:=rex or $48;
  1601. {$endif x86_64}
  1602. end;
  1603. 201,
  1604. 202,
  1605. 211,
  1606. 213,
  1607. 215,
  1608. 217,218: ;
  1609. 219,220 :
  1610. inc(len);
  1611. 221:
  1612. {$ifdef x86_64}
  1613. { remove rex competely? }
  1614. if rex=$48 then
  1615. begin
  1616. rex:=0;
  1617. dec(len);
  1618. end
  1619. else
  1620. rex:=rex and $f7
  1621. {$endif x86_64}
  1622. ;
  1623. 64..191 :
  1624. begin
  1625. {$ifdef x86_64}
  1626. if (c<127) then
  1627. begin
  1628. if (oper[c and 7]^.typ=top_reg) then
  1629. begin
  1630. if ((getregtype(oper[c and 7]^.reg)=R_INTREGISTER) and
  1631. (getsupreg(oper[c and 7]^.reg)>=RS_R8)) or
  1632. ((getregtype(oper[c and 7]^.reg)=R_MMREGISTER) and
  1633. (getsupreg(oper[c and 7]^.reg)>=RS_XMM8)) then
  1634. begin
  1635. if rex=0 then
  1636. inc(len);
  1637. rex:=rex or $44;
  1638. end
  1639. else if (getregtype(oper[c and 7]^.reg)=R_INTREGISTER) and
  1640. (getsubreg(oper[c and 7]^.reg)=R_SUBL) and
  1641. (getsupreg(oper[c and 7]^.reg) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1642. begin
  1643. if rex=0 then
  1644. inc(len);
  1645. rex:=rex or $40;
  1646. end;
  1647. end;
  1648. end;
  1649. {$endif x86_64}
  1650. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1651. Message(asmw_e_invalid_effective_address)
  1652. else
  1653. inc(len,ea_data.size);
  1654. {$ifdef x86_64}
  1655. { did we already create include a rex into the length calculation? }
  1656. if (rex<>0) and (ea_data.rex<>0) then
  1657. dec(len);
  1658. rex:=rex or ea_data.rex;
  1659. {$endif x86_64}
  1660. end;
  1661. else
  1662. InternalError(200603141);
  1663. end;
  1664. until false;
  1665. calcsize:=len;
  1666. end;
  1667. procedure taicpu.GenCode(objdata:TObjData);
  1668. {
  1669. * the actual codes (C syntax, i.e. octal):
  1670. * \0 - terminates the code. (Unless it's a literal of course.)
  1671. * \1, \2, \3 - that many literal bytes follow in the code stream
  1672. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1673. * (POP is never used for CS) depending on operand 0
  1674. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1675. * on operand 0
  1676. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1677. * to the register value of operand 0, 1 or 2
  1678. * \13 - a literal byte follows in the code stream, to be added
  1679. * to the condition code value of the instruction.
  1680. * \17 - encodes the literal byte 0. (Some compilers don't take
  1681. * kindly to a zero byte in the _middle_ of a compile time
  1682. * string constant, so I had to put this hack in.)
  1683. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1684. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1685. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1686. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1687. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1688. * assembly mode or the address-size override on the operand
  1689. * \37 - a word constant, from the _segment_ part of operand 0
  1690. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1691. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1692. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1693. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1694. * assembly mode or the address-size override on the operand
  1695. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1696. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1697. * field the register value of operand b.
  1698. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1699. * field equal to digit b.
  1700. * \300,\301,\302 - might be an 0x67 or 0x48 byte, depending on the address size of
  1701. * the memory reference in operand x.
  1702. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1703. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1704. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1705. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  1706. * size of operand x.
  1707. * \323 - insert x86_64 REX at this position.
  1708. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1709. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1710. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1711. * \327 - indicates that this instruction is only valid when the
  1712. * operand size is the default (instruction to disassembler,
  1713. * generates no code in the assembler)
  1714. * \331 - instruction not valid with REP prefix. Hint for
  1715. * disassembler only; for SSE instructions.
  1716. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  1717. * \333 - REP prefix (0xF3 byte); for SSE instructions. Not encoded
  1718. * \335 - removes rex size prefix, i.e. rex.w must be the last opcode
  1719. }
  1720. var
  1721. currval : aint;
  1722. currsym : tobjsymbol;
  1723. currrelreloc,
  1724. currabsreloc,
  1725. currabsreloc32 : TObjRelocationType;
  1726. {$ifdef x86_64}
  1727. rexwritten : boolean;
  1728. {$endif x86_64}
  1729. procedure getvalsym(opidx:longint);
  1730. begin
  1731. case oper[opidx]^.typ of
  1732. top_ref :
  1733. begin
  1734. currval:=oper[opidx]^.ref^.offset;
  1735. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  1736. {$ifdef x86_64}
  1737. if oper[opidx]^.ref^.refaddr=addr_pic then
  1738. begin
  1739. currrelreloc:=RELOC_PLT32;
  1740. currabsreloc:=RELOC_GOTPCREL;
  1741. currabsreloc32:=RELOC_GOTPCREL;
  1742. end
  1743. else
  1744. {$endif x86_64}
  1745. begin
  1746. currrelreloc:=RELOC_RELATIVE;
  1747. currabsreloc:=RELOC_ABSOLUTE;
  1748. currabsreloc32:=RELOC_ABSOLUTE32;
  1749. end;
  1750. end;
  1751. top_const :
  1752. begin
  1753. currval:=aint(oper[opidx]^.val);
  1754. currsym:=nil;
  1755. currabsreloc:=RELOC_ABSOLUTE;
  1756. currabsreloc32:=RELOC_ABSOLUTE32;
  1757. end;
  1758. else
  1759. Message(asmw_e_immediate_or_reference_expected);
  1760. end;
  1761. end;
  1762. {$ifdef x86_64}
  1763. procedure maybewriterex;
  1764. begin
  1765. if (rex<>0) and not(rexwritten) then
  1766. begin
  1767. rexwritten:=true;
  1768. objdata.writebytes(rex,1);
  1769. end;
  1770. end;
  1771. {$endif x86_64}
  1772. const
  1773. CondVal:array[TAsmCond] of byte=($0,
  1774. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1775. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1776. $0, $A, $A, $B, $8, $4);
  1777. var
  1778. c : byte;
  1779. pb : pbyte;
  1780. codes : pchar;
  1781. bytes : array[0..3] of byte;
  1782. rfield,
  1783. data,s,opidx : longint;
  1784. ea_data : ea;
  1785. begin
  1786. { safety check }
  1787. if objdata.currobjsec.size<>insoffset then
  1788. internalerror(200130121);
  1789. { load data to write }
  1790. codes:=insentry^.code;
  1791. {$ifdef x86_64}
  1792. rexwritten:=false;
  1793. {$endif x86_64}
  1794. { Force word push/pop for registers }
  1795. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1796. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1797. begin
  1798. bytes[0]:=$66;
  1799. objdata.writebytes(bytes,1);
  1800. end;
  1801. repeat
  1802. c:=ord(codes^);
  1803. inc(codes);
  1804. case c of
  1805. 0 :
  1806. break;
  1807. 1,2,3 :
  1808. begin
  1809. objdata.writebytes(codes^,c);
  1810. inc(codes,c);
  1811. end;
  1812. 4,6 :
  1813. begin
  1814. case oper[0]^.reg of
  1815. NR_CS:
  1816. bytes[0]:=$e;
  1817. NR_NO,
  1818. NR_DS:
  1819. bytes[0]:=$1e;
  1820. NR_ES:
  1821. bytes[0]:=$6;
  1822. NR_SS:
  1823. bytes[0]:=$16;
  1824. else
  1825. internalerror(777004);
  1826. end;
  1827. if c=4 then
  1828. inc(bytes[0]);
  1829. objdata.writebytes(bytes,1);
  1830. end;
  1831. 5,7 :
  1832. begin
  1833. case oper[0]^.reg of
  1834. NR_FS:
  1835. bytes[0]:=$a0;
  1836. NR_GS:
  1837. bytes[0]:=$a8;
  1838. else
  1839. internalerror(777005);
  1840. end;
  1841. if c=5 then
  1842. inc(bytes[0]);
  1843. objdata.writebytes(bytes,1);
  1844. end;
  1845. 8,9,10 :
  1846. begin
  1847. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1848. inc(codes);
  1849. objdata.writebytes(bytes,1);
  1850. end;
  1851. 11 :
  1852. begin
  1853. bytes[0]:=ord(codes^)+condval[condition];
  1854. inc(codes);
  1855. objdata.writebytes(bytes,1);
  1856. end;
  1857. 15 :
  1858. begin
  1859. bytes[0]:=0;
  1860. objdata.writebytes(bytes,1);
  1861. end;
  1862. 12,13,14 :
  1863. begin
  1864. getvalsym(c-12);
  1865. if (currval<-128) or (currval>127) then
  1866. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1867. if assigned(currsym) then
  1868. objdata.writereloc(currval,1,currsym,currabsreloc)
  1869. else
  1870. objdata.writebytes(currval,1);
  1871. end;
  1872. 16,17,18 :
  1873. begin
  1874. getvalsym(c-16);
  1875. if (currval<-256) or (currval>255) then
  1876. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1877. if assigned(currsym) then
  1878. objdata.writereloc(currval,1,currsym,currabsreloc)
  1879. else
  1880. objdata.writebytes(currval,1);
  1881. end;
  1882. 20,21,22 :
  1883. begin
  1884. getvalsym(c-20);
  1885. if (currval<0) or (currval>255) then
  1886. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1887. if assigned(currsym) then
  1888. objdata.writereloc(currval,1,currsym,currabsreloc)
  1889. else
  1890. objdata.writebytes(currval,1);
  1891. end;
  1892. 24,25,26 :
  1893. begin
  1894. getvalsym(c-24);
  1895. if (currval<-65536) or (currval>65535) then
  1896. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1897. if assigned(currsym) then
  1898. objdata.writereloc(currval,2,currsym,currabsreloc)
  1899. else
  1900. objdata.writebytes(currval,2);
  1901. end;
  1902. 28,29,30 :
  1903. begin
  1904. getvalsym(c-28);
  1905. if opsize=S_Q then
  1906. begin
  1907. if assigned(currsym) then
  1908. objdata.writereloc(currval,8,currsym,currabsreloc)
  1909. else
  1910. objdata.writebytes(currval,8);
  1911. end
  1912. else
  1913. begin
  1914. if assigned(currsym) then
  1915. objdata.writereloc(currval,4,currsym,currabsreloc32)
  1916. else
  1917. objdata.writebytes(currval,4);
  1918. end
  1919. end;
  1920. 32,33,34 :
  1921. begin
  1922. getvalsym(c-32);
  1923. if assigned(currsym) then
  1924. objdata.writereloc(currval,4,currsym,currabsreloc32)
  1925. else
  1926. objdata.writebytes(currval,4);
  1927. end;
  1928. 40,41,42 :
  1929. begin
  1930. getvalsym(c-40);
  1931. data:=currval-insend;
  1932. if assigned(currsym) then
  1933. inc(data,currsym.address);
  1934. if (data>127) or (data<-128) then
  1935. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1936. objdata.writebytes(data,1);
  1937. end;
  1938. 52,53,54 :
  1939. begin
  1940. getvalsym(c-52);
  1941. if assigned(currsym) then
  1942. objdata.writereloc(currval,4,currsym,currrelreloc)
  1943. else
  1944. objdata.writereloc(currval-insend,4,nil,currabsreloc32)
  1945. end;
  1946. 56,57,58 :
  1947. begin
  1948. getvalsym(c-56);
  1949. if assigned(currsym) then
  1950. objdata.writereloc(currval,4,currsym,currrelreloc)
  1951. else
  1952. objdata.writereloc(currval-insend,4,nil,currabsreloc32)
  1953. end;
  1954. 192,193,194 :
  1955. begin
  1956. if NeedAddrPrefix(c-192) then
  1957. begin
  1958. bytes[0]:=$67;
  1959. objdata.writebytes(bytes,1);
  1960. end;
  1961. end;
  1962. 200 :
  1963. begin
  1964. bytes[0]:=$67;
  1965. objdata.writebytes(bytes,1);
  1966. end;
  1967. 208,209,210 :
  1968. begin
  1969. case oper[c-208]^.ot and OT_SIZE_MASK of
  1970. OT_BITS16 :
  1971. begin
  1972. bytes[0]:=$66;
  1973. objdata.writebytes(bytes,1);
  1974. end;
  1975. {$ifndef x86_64}
  1976. OT_BITS64 :
  1977. Message(asmw_e_64bit_not_supported);
  1978. {$endif x86_64}
  1979. end;
  1980. {$ifdef x86_64}
  1981. maybewriterex;
  1982. {$endif x86_64}
  1983. end;
  1984. 211,
  1985. 213 :
  1986. begin
  1987. {$ifdef x86_64}
  1988. maybewriterex;
  1989. {$endif x86_64}
  1990. end;
  1991. 212 :
  1992. begin
  1993. bytes[0]:=$66;
  1994. objdata.writebytes(bytes,1);
  1995. {$ifdef x86_64}
  1996. maybewriterex;
  1997. {$endif x86_64}
  1998. end;
  1999. 214 :
  2000. begin
  2001. {$ifdef x86_64}
  2002. maybewriterex;
  2003. {$else x86_64}
  2004. Message(asmw_e_64bit_not_supported);
  2005. {$endif x86_64}
  2006. end;
  2007. 219 :
  2008. begin
  2009. bytes[0]:=$f3;
  2010. objdata.writebytes(bytes,1);
  2011. {$ifdef x86_64}
  2012. maybewriterex;
  2013. {$endif x86_64}
  2014. end;
  2015. 220 :
  2016. begin
  2017. bytes[0]:=$f2;
  2018. objdata.writebytes(bytes,1);
  2019. end;
  2020. 221:
  2021. ;
  2022. 201,
  2023. 202,
  2024. 215,
  2025. 217,218 :
  2026. begin
  2027. { these are dissambler hints or 32 bit prefixes which
  2028. are not needed
  2029. It's usefull to write rex :) (FK) }
  2030. {$ifdef x86_64}
  2031. maybewriterex;
  2032. {$endif x86_64}
  2033. end;
  2034. 31,
  2035. 48,49,50 :
  2036. begin
  2037. InternalError(777006);
  2038. end
  2039. else
  2040. begin
  2041. { rex should be written at this point }
  2042. {$ifdef x86_64}
  2043. if (rex<>0) and not(rexwritten) then
  2044. internalerror(200603191);
  2045. {$endif x86_64}
  2046. if (c>=64) and (c<=191) then
  2047. begin
  2048. if (c<127) then
  2049. begin
  2050. if (oper[c and 7]^.typ=top_reg) then
  2051. rfield:=regval(oper[c and 7]^.reg)
  2052. else
  2053. rfield:=regval(oper[c and 7]^.ref^.base);
  2054. end
  2055. else
  2056. rfield:=c and 7;
  2057. opidx:=(c shr 3) and 7;
  2058. if not process_ea(oper[opidx]^,ea_data,rfield) then
  2059. Message(asmw_e_invalid_effective_address);
  2060. pb:=@bytes[0];
  2061. pb^:=ea_data.modrm;
  2062. inc(pb);
  2063. if ea_data.sib_present then
  2064. begin
  2065. pb^:=ea_data.sib;
  2066. inc(pb);
  2067. end;
  2068. s:=pb-@bytes[0];
  2069. objdata.writebytes(bytes,s);
  2070. case ea_data.bytes of
  2071. 0 : ;
  2072. 1 :
  2073. begin
  2074. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  2075. begin
  2076. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2077. {$ifdef x86_64}
  2078. if oper[opidx]^.ref^.refaddr=addr_pic then
  2079. currabsreloc:=RELOC_GOTPCREL
  2080. else
  2081. {$endif x86_64}
  2082. currabsreloc:=RELOC_ABSOLUTE;
  2083. objdata.writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  2084. end
  2085. else
  2086. begin
  2087. bytes[0]:=oper[opidx]^.ref^.offset;
  2088. objdata.writebytes(bytes,1);
  2089. end;
  2090. inc(s);
  2091. end;
  2092. 2,4 :
  2093. begin
  2094. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2095. {$ifdef x86_64}
  2096. if oper[opidx]^.ref^.refaddr=addr_pic then
  2097. currabsreloc:=RELOC_GOTPCREL
  2098. else
  2099. {$endif x86_64}
  2100. currabsreloc:=RELOC_ABSOLUTE32;
  2101. objdata.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,currsym,currabsreloc);
  2102. inc(s,ea_data.bytes);
  2103. end;
  2104. end;
  2105. end
  2106. else
  2107. InternalError(777007);
  2108. end;
  2109. end;
  2110. until false;
  2111. end;
  2112. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  2113. begin
  2114. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  2115. (regtype = R_INTREGISTER) and
  2116. (ops=2) and
  2117. (oper[0]^.typ=top_reg) and
  2118. (oper[1]^.typ=top_reg) and
  2119. (oper[0]^.reg=oper[1]^.reg)
  2120. ) or
  2121. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
  2122. (opcode=A_MOVAPS) or (OPCODE=A_MOVAPD)) and
  2123. (regtype = R_MMREGISTER) and
  2124. (ops=2) and
  2125. (oper[0]^.typ=top_reg) and
  2126. (oper[1]^.typ=top_reg) and
  2127. (oper[0]^.reg=oper[1]^.reg)
  2128. );
  2129. end;
  2130. procedure build_spilling_operation_type_table;
  2131. var
  2132. opcode : tasmop;
  2133. i : integer;
  2134. begin
  2135. new(operation_type_table);
  2136. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2137. for opcode:=low(tasmop) to high(tasmop) do
  2138. begin
  2139. for i:=1 to MaxInsChanges do
  2140. begin
  2141. case InsProp[opcode].Ch[i] of
  2142. Ch_Rop1 :
  2143. operation_type_table^[opcode,0]:=operand_read;
  2144. Ch_Wop1 :
  2145. operation_type_table^[opcode,0]:=operand_write;
  2146. Ch_RWop1,
  2147. Ch_Mop1 :
  2148. operation_type_table^[opcode,0]:=operand_readwrite;
  2149. Ch_Rop2 :
  2150. operation_type_table^[opcode,1]:=operand_read;
  2151. Ch_Wop2 :
  2152. operation_type_table^[opcode,1]:=operand_write;
  2153. Ch_RWop2,
  2154. Ch_Mop2 :
  2155. operation_type_table^[opcode,1]:=operand_readwrite;
  2156. Ch_Rop3 :
  2157. operation_type_table^[opcode,2]:=operand_read;
  2158. Ch_Wop3 :
  2159. operation_type_table^[opcode,2]:=operand_write;
  2160. Ch_RWop3,
  2161. Ch_Mop3 :
  2162. operation_type_table^[opcode,2]:=operand_readwrite;
  2163. end;
  2164. end;
  2165. end;
  2166. { Special cases that can't be decoded from the InsChanges flags }
  2167. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  2168. end;
  2169. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2170. begin
  2171. { the information in the instruction table is made for the string copy
  2172. operation MOVSD so hack here (FK)
  2173. }
  2174. if (opcode=A_MOVSD) and (ops=2) then
  2175. begin
  2176. case opnr of
  2177. 0:
  2178. result:=operand_read;
  2179. 1:
  2180. result:=operand_write;
  2181. else
  2182. internalerror(200506055);
  2183. end
  2184. end
  2185. else
  2186. result:=operation_type_table^[opcode,opnr];
  2187. end;
  2188. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  2189. begin
  2190. case getregtype(r) of
  2191. R_INTREGISTER :
  2192. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
  2193. R_MMREGISTER :
  2194. case getsubreg(r) of
  2195. R_SUBMMD:
  2196. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  2197. R_SUBMMS:
  2198. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  2199. R_SUBMMWHOLE:
  2200. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,ref,r);
  2201. else
  2202. internalerror(200506043);
  2203. end;
  2204. else
  2205. internalerror(200401041);
  2206. end;
  2207. end;
  2208. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  2209. begin
  2210. case getregtype(r) of
  2211. R_INTREGISTER :
  2212. result:=taicpu.op_reg_ref(A_MOV,reg2opsize(r),r,ref);
  2213. R_MMREGISTER :
  2214. case getsubreg(r) of
  2215. R_SUBMMD:
  2216. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  2217. R_SUBMMS:
  2218. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  2219. R_SUBMMWHOLE:
  2220. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,ref);
  2221. else
  2222. internalerror(200506042);
  2223. end;
  2224. else
  2225. internalerror(200401041);
  2226. end;
  2227. end;
  2228. {*****************************************************************************
  2229. Instruction table
  2230. *****************************************************************************}
  2231. procedure BuildInsTabCache;
  2232. var
  2233. i : longint;
  2234. begin
  2235. new(instabcache);
  2236. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2237. i:=0;
  2238. while (i<InsTabEntries) do
  2239. begin
  2240. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2241. InsTabCache^[InsTab[i].OPcode]:=i;
  2242. inc(i);
  2243. end;
  2244. end;
  2245. procedure InitAsm;
  2246. begin
  2247. build_spilling_operation_type_table;
  2248. if not assigned(instabcache) then
  2249. BuildInsTabCache;
  2250. end;
  2251. procedure DoneAsm;
  2252. begin
  2253. if assigned(operation_type_table) then
  2254. begin
  2255. dispose(operation_type_table);
  2256. operation_type_table:=nil;
  2257. end;
  2258. if assigned(instabcache) then
  2259. begin
  2260. dispose(instabcache);
  2261. instabcache:=nil;
  2262. end;
  2263. end;
  2264. begin
  2265. cai_align:=tai_align;
  2266. cai_cpu:=taicpu;
  2267. end.