aasmcpu.pas 79 KB

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