aasmcpu.pas 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312
  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,
  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_MMXREG = $00201008; { MMX registers }
  65. OT_XMMREG = $00201010; { Katmai registers }
  66. OT_MEMORY = $00204000; { register number in 'basereg' }
  67. OT_MEM8 = $00204001;
  68. OT_MEM16 = $00204002;
  69. OT_MEM32 = $00204004;
  70. OT_MEM64 = $00204008;
  71. OT_MEM80 = $00204010;
  72. OT_FPUREG = $01000000; { floating point stack registers }
  73. OT_FPU0 = $01000800; { FPU stack register zero }
  74. OT_REG_SMASK = $00070000; { special register operands: these may be treated differently }
  75. { a mask for the following }
  76. OT_REG_ACCUM = $00211000; { FUNCTION_RETURN_REG: AL, AX or EAX }
  77. OT_REG_AL = $00211001; { REG_ACCUM | BITSxx }
  78. OT_REG_AX = $00211002; { ditto }
  79. OT_REG_EAX = $00211004; { and again }
  80. {$ifdef x86_64}
  81. OT_REG_RAX = $00211008;
  82. {$endif x86_64}
  83. OT_REG_COUNT = $00221000; { counter: CL, CX or ECX }
  84. OT_REG_CL = $00221001; { REG_COUNT | BITSxx }
  85. OT_REG_CX = $00221002; { ditto }
  86. OT_REG_ECX = $00221004; { another one }
  87. {$ifdef x86_64}
  88. OT_REG_RCX = $00221008;
  89. {$endif x86_64}
  90. OT_REG_DX = $00241002;
  91. OT_REG_EDX = $00241004;
  92. OT_REG_SREG = $00081002; { any segment register }
  93. OT_REG_CS = $01081002; { CS }
  94. OT_REG_DESS = $02081002; { DS, ES, SS (non-CS 86 registers) }
  95. OT_REG_FSGS = $04081002; { FS, GS (386 extended registers) }
  96. OT_REG_CDT = $00101004; { CRn, DRn and TRn }
  97. OT_REG_CREG = $08101004; { CRn }
  98. OT_REG_CR4 = $08101404; { CR4 (Pentium only) }
  99. OT_REG_DREG = $10101004; { DRn }
  100. OT_REG_TREG = $20101004; { TRn }
  101. OT_MEM_OFFS = $00604000; { special type of EA }
  102. { simple [address] offset }
  103. OT_ONENESS = $00800000; { special type of immediate operand }
  104. { so UNITY == IMMEDIATE | ONENESS }
  105. OT_UNITY = $00802000; { for shift/rotate instructions }
  106. { Size of the instruction table converted by nasmconv.pas }
  107. {$ifdef x86_64}
  108. instabentries = {$i x8664nop.inc}
  109. {$else x86_64}
  110. instabentries = {$i i386nop.inc}
  111. {$endif x86_64}
  112. maxinfolen = 8;
  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)
  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. protected
  211. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);override;
  212. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);override;
  213. procedure ppubuildderefimploper(var o:toper);override;
  214. procedure ppuderefoper(var o:toper);override;
  215. private
  216. { next fields are filled in pass1, so pass2 is faster }
  217. inssize : shortint;
  218. insoffset : longint;
  219. LastInsOffset : longint; { need to be public to be reset }
  220. insentry : PInsEntry;
  221. function InsEnd:longint;
  222. procedure create_ot(objdata:TObjData);
  223. function Matches(p:PInsEntry):boolean;
  224. function calcsize(p:PInsEntry):shortint;
  225. procedure gencode(objdata:TObjData);
  226. function NeedAddrPrefix(opidx:byte):boolean;
  227. procedure Swapoperands;
  228. function FindInsentry(objdata:TObjData):boolean;
  229. end;
  230. function spilling_create_load(const ref:treference;r:tregister): tai;
  231. function spilling_create_store(r:tregister; const ref:treference): tai;
  232. procedure InitAsm;
  233. procedure DoneAsm;
  234. implementation
  235. uses
  236. cutils,
  237. itcpugas,
  238. symsym;
  239. {*****************************************************************************
  240. Instruction table
  241. *****************************************************************************}
  242. const
  243. {Instruction flags }
  244. IF_NONE = $00000000;
  245. IF_SM = $00000001; { size match first two operands }
  246. IF_SM2 = $00000002;
  247. IF_SB = $00000004; { unsized operands can't be non-byte }
  248. IF_SW = $00000008; { unsized operands can't be non-word }
  249. IF_SD = $00000010; { unsized operands can't be nondword }
  250. IF_SMASK = $0000001f;
  251. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  252. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  253. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  254. IF_ARMASK = $00000060; { mask for unsized argument spec }
  255. IF_PRIV = $00000100; { it's a privileged instruction }
  256. IF_SMM = $00000200; { it's only valid in SMM }
  257. IF_PROT = $00000400; { it's protected mode only }
  258. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  259. IF_UNDOC = $00001000; { it's an undocumented instruction }
  260. IF_FPU = $00002000; { it's an FPU instruction }
  261. IF_MMX = $00004000; { it's an MMX instruction }
  262. { it's a 3DNow! instruction }
  263. IF_3DNOW = $00008000;
  264. { it's a SSE (KNI, MMX2) instruction }
  265. IF_SSE = $00010000;
  266. { SSE2 instructions }
  267. IF_SSE2 = $00020000;
  268. { SSE3 instructions }
  269. IF_SSE3 = $00040000;
  270. { SSE64 instructions }
  271. IF_SSE64 = $00080000;
  272. { the mask for processor types }
  273. {IF_PMASK = longint($FF000000);}
  274. { the mask for disassembly "prefer" }
  275. {IF_PFMASK = longint($F001FF00);}
  276. { SVM instructions }
  277. IF_SVM = $00100000;
  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. alignarray:array[0..5] of string[8]=(
  399. #$8D#$B4#$26#$00#$00#$00#$00,
  400. #$8D#$B6#$00#$00#$00#$00,
  401. #$8D#$74#$26#$00,
  402. #$8D#$76#$00,
  403. #$89#$F6,
  404. #$90
  405. );
  406. var
  407. bufptr : pchar;
  408. j : longint;
  409. begin
  410. inherited calculatefillbuf(buf);
  411. if not use_op then
  412. begin
  413. bufptr:=pchar(@buf);
  414. while (fillsize>0) do
  415. begin
  416. for j:=0 to 5 do
  417. if (fillsize>=length(alignarray[j])) then
  418. break;
  419. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  420. inc(bufptr,length(alignarray[j]));
  421. dec(fillsize,length(alignarray[j]));
  422. end;
  423. end;
  424. calculatefillbuf:=pchar(@buf);
  425. end;
  426. {*****************************************************************************
  427. Taicpu Constructors
  428. *****************************************************************************}
  429. procedure taicpu.changeopsize(siz:topsize);
  430. begin
  431. opsize:=siz;
  432. end;
  433. procedure taicpu.init(_size : topsize);
  434. begin
  435. { default order is att }
  436. FOperandOrder:=op_att;
  437. segprefix:=NR_NO;
  438. opsize:=_size;
  439. insentry:=nil;
  440. LastInsOffset:=-1;
  441. InsOffset:=0;
  442. InsSize:=0;
  443. end;
  444. constructor taicpu.op_none(op : tasmop);
  445. begin
  446. inherited create(op);
  447. init(S_NO);
  448. end;
  449. constructor taicpu.op_none(op : tasmop;_size : topsize);
  450. begin
  451. inherited create(op);
  452. init(_size);
  453. end;
  454. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  455. begin
  456. inherited create(op);
  457. init(_size);
  458. ops:=1;
  459. loadreg(0,_op1);
  460. end;
  461. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  462. begin
  463. inherited create(op);
  464. init(_size);
  465. ops:=1;
  466. loadconst(0,_op1);
  467. end;
  468. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  469. begin
  470. inherited create(op);
  471. init(_size);
  472. ops:=1;
  473. loadref(0,_op1);
  474. end;
  475. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  476. begin
  477. inherited create(op);
  478. init(_size);
  479. ops:=2;
  480. loadreg(0,_op1);
  481. loadreg(1,_op2);
  482. end;
  483. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  484. begin
  485. inherited create(op);
  486. init(_size);
  487. ops:=2;
  488. loadreg(0,_op1);
  489. loadconst(1,_op2);
  490. end;
  491. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  492. begin
  493. inherited create(op);
  494. init(_size);
  495. ops:=2;
  496. loadreg(0,_op1);
  497. loadref(1,_op2);
  498. end;
  499. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  500. begin
  501. inherited create(op);
  502. init(_size);
  503. ops:=2;
  504. loadconst(0,_op1);
  505. loadreg(1,_op2);
  506. end;
  507. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  508. begin
  509. inherited create(op);
  510. init(_size);
  511. ops:=2;
  512. loadconst(0,_op1);
  513. loadconst(1,_op2);
  514. end;
  515. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  516. begin
  517. inherited create(op);
  518. init(_size);
  519. ops:=2;
  520. loadconst(0,_op1);
  521. loadref(1,_op2);
  522. end;
  523. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  524. begin
  525. inherited create(op);
  526. init(_size);
  527. ops:=2;
  528. loadref(0,_op1);
  529. loadreg(1,_op2);
  530. end;
  531. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  532. begin
  533. inherited create(op);
  534. init(_size);
  535. ops:=3;
  536. loadreg(0,_op1);
  537. loadreg(1,_op2);
  538. loadreg(2,_op3);
  539. end;
  540. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  541. begin
  542. inherited create(op);
  543. init(_size);
  544. ops:=3;
  545. loadconst(0,_op1);
  546. loadreg(1,_op2);
  547. loadreg(2,_op3);
  548. end;
  549. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  550. begin
  551. inherited create(op);
  552. init(_size);
  553. ops:=3;
  554. loadreg(0,_op1);
  555. loadreg(1,_op2);
  556. loadref(2,_op3);
  557. end;
  558. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  559. begin
  560. inherited create(op);
  561. init(_size);
  562. ops:=3;
  563. loadconst(0,_op1);
  564. loadref(1,_op2);
  565. loadreg(2,_op3);
  566. end;
  567. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  568. begin
  569. inherited create(op);
  570. init(_size);
  571. ops:=3;
  572. loadconst(0,_op1);
  573. loadreg(1,_op2);
  574. loadref(2,_op3);
  575. end;
  576. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  577. begin
  578. inherited create(op);
  579. init(_size);
  580. condition:=cond;
  581. ops:=1;
  582. loadsymbol(0,_op1,0);
  583. end;
  584. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  585. begin
  586. inherited create(op);
  587. init(_size);
  588. ops:=1;
  589. loadsymbol(0,_op1,0);
  590. end;
  591. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  592. begin
  593. inherited create(op);
  594. init(_size);
  595. ops:=1;
  596. loadsymbol(0,_op1,_op1ofs);
  597. end;
  598. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  599. begin
  600. inherited create(op);
  601. init(_size);
  602. ops:=2;
  603. loadsymbol(0,_op1,_op1ofs);
  604. loadreg(1,_op2);
  605. end;
  606. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  607. begin
  608. inherited create(op);
  609. init(_size);
  610. ops:=2;
  611. loadsymbol(0,_op1,_op1ofs);
  612. loadref(1,_op2);
  613. end;
  614. function taicpu.GetString:string;
  615. var
  616. i : longint;
  617. s : string;
  618. addsize : boolean;
  619. begin
  620. s:='['+std_op2str[opcode];
  621. for i:=0 to ops-1 do
  622. begin
  623. with oper[i]^ do
  624. begin
  625. if i=0 then
  626. s:=s+' '
  627. else
  628. s:=s+',';
  629. { type }
  630. addsize:=false;
  631. if (ot and OT_XMMREG)=OT_XMMREG then
  632. s:=s+'xmmreg'
  633. else
  634. if (ot and OT_MMXREG)=OT_MMXREG then
  635. s:=s+'mmxreg'
  636. else
  637. if (ot and OT_FPUREG)=OT_FPUREG then
  638. s:=s+'fpureg'
  639. else
  640. if (ot and OT_REGISTER)=OT_REGISTER then
  641. begin
  642. s:=s+'reg';
  643. addsize:=true;
  644. end
  645. else
  646. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  647. begin
  648. s:=s+'imm';
  649. addsize:=true;
  650. end
  651. else
  652. if (ot and OT_MEMORY)=OT_MEMORY then
  653. begin
  654. s:=s+'mem';
  655. addsize:=true;
  656. end
  657. else
  658. s:=s+'???';
  659. { size }
  660. if addsize then
  661. begin
  662. if (ot and OT_BITS8)<>0 then
  663. s:=s+'8'
  664. else
  665. if (ot and OT_BITS16)<>0 then
  666. s:=s+'16'
  667. else
  668. if (ot and OT_BITS32)<>0 then
  669. s:=s+'32'
  670. else
  671. {$ifdef x86_64}
  672. if (ot and OT_BITS32)<>0 then
  673. s:=s+'64'
  674. else
  675. {$endif x86_64}
  676. s:=s+'??';
  677. { signed }
  678. if (ot and OT_SIGNED)<>0 then
  679. s:=s+'s';
  680. end;
  681. end;
  682. end;
  683. GetString:=s+']';
  684. end;
  685. procedure taicpu.Swapoperands;
  686. var
  687. p : POper;
  688. begin
  689. { Fix the operands which are in AT&T style and we need them in Intel style }
  690. case ops of
  691. 2 : begin
  692. { 0,1 -> 1,0 }
  693. p:=oper[0];
  694. oper[0]:=oper[1];
  695. oper[1]:=p;
  696. end;
  697. 3 : begin
  698. { 0,1,2 -> 2,1,0 }
  699. p:=oper[0];
  700. oper[0]:=oper[2];
  701. oper[2]:=p;
  702. end;
  703. end;
  704. end;
  705. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  706. begin
  707. if FOperandOrder<>order then
  708. begin
  709. Swapoperands;
  710. FOperandOrder:=order;
  711. end;
  712. end;
  713. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  714. begin
  715. o.typ:=toptype(ppufile.getbyte);
  716. o.ot:=ppufile.getlongint;
  717. case o.typ of
  718. top_reg :
  719. ppufile.getdata(o.reg,sizeof(Tregister));
  720. top_ref :
  721. begin
  722. new(o.ref);
  723. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  724. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  725. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  726. o.ref^.scalefactor:=ppufile.getbyte;
  727. o.ref^.offset:=ppufile.getaint;
  728. o.ref^.symbol:=ppufile.getasmsymbol;
  729. o.ref^.relsymbol:=ppufile.getasmsymbol;
  730. end;
  731. top_const :
  732. o.val:=ppufile.getaint;
  733. top_local :
  734. begin
  735. new(o.localoper);
  736. with o.localoper^ do
  737. begin
  738. ppufile.getderef(localsymderef);
  739. localsymofs:=ppufile.getaint;
  740. localindexreg:=tregister(ppufile.getlongint);
  741. localscale:=ppufile.getbyte;
  742. localgetoffset:=(ppufile.getbyte<>0);
  743. end;
  744. end;
  745. end;
  746. end;
  747. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  748. begin
  749. ppufile.putbyte(byte(o.typ));
  750. ppufile.putlongint(o.ot);
  751. case o.typ of
  752. top_reg :
  753. ppufile.putdata(o.reg,sizeof(Tregister));
  754. top_ref :
  755. begin
  756. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  757. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  758. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  759. ppufile.putbyte(o.ref^.scalefactor);
  760. ppufile.putaint(o.ref^.offset);
  761. ppufile.putasmsymbol(o.ref^.symbol);
  762. ppufile.putasmsymbol(o.ref^.relsymbol);
  763. end;
  764. top_const :
  765. ppufile.putaint(o.val);
  766. top_local :
  767. begin
  768. with o.localoper^ do
  769. begin
  770. ppufile.putderef(localsymderef);
  771. ppufile.putaint(localsymofs);
  772. ppufile.putlongint(longint(localindexreg));
  773. ppufile.putbyte(localscale);
  774. ppufile.putbyte(byte(localgetoffset));
  775. end;
  776. end;
  777. end;
  778. end;
  779. procedure taicpu.ppubuildderefimploper(var o:toper);
  780. begin
  781. case o.typ of
  782. top_local :
  783. o.localoper^.localsymderef.build(tlocalvarsym(o.localoper^.localsym));
  784. end;
  785. end;
  786. procedure taicpu.ppuderefoper(var o:toper);
  787. begin
  788. case o.typ of
  789. top_ref :
  790. begin
  791. end;
  792. top_local :
  793. o.localoper^.localsym:=tlocalvarsym(o.localoper^.localsymderef.resolve);
  794. end;
  795. end;
  796. procedure taicpu.CheckNonCommutativeOpcodes;
  797. begin
  798. { we need ATT order }
  799. SetOperandOrder(op_att);
  800. if (
  801. (ops=2) and
  802. (oper[0]^.typ=top_reg) and
  803. (oper[1]^.typ=top_reg) and
  804. { if the first is ST and the second is also a register
  805. it is necessarily ST1 .. ST7 }
  806. ((oper[0]^.reg=NR_ST) or
  807. (oper[0]^.reg=NR_ST0))
  808. ) or
  809. { ((ops=1) and
  810. (oper[0]^.typ=top_reg) and
  811. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  812. (ops=0) then
  813. begin
  814. if opcode=A_FSUBR then
  815. opcode:=A_FSUB
  816. else if opcode=A_FSUB then
  817. opcode:=A_FSUBR
  818. else if opcode=A_FDIVR then
  819. opcode:=A_FDIV
  820. else if opcode=A_FDIV then
  821. opcode:=A_FDIVR
  822. else if opcode=A_FSUBRP then
  823. opcode:=A_FSUBP
  824. else if opcode=A_FSUBP then
  825. opcode:=A_FSUBRP
  826. else if opcode=A_FDIVRP then
  827. opcode:=A_FDIVP
  828. else if opcode=A_FDIVP then
  829. opcode:=A_FDIVRP;
  830. end;
  831. if (
  832. (ops=1) and
  833. (oper[0]^.typ=top_reg) and
  834. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  835. (oper[0]^.reg<>NR_ST)
  836. ) then
  837. begin
  838. if opcode=A_FSUBRP then
  839. opcode:=A_FSUBP
  840. else if opcode=A_FSUBP then
  841. opcode:=A_FSUBRP
  842. else if opcode=A_FDIVRP then
  843. opcode:=A_FDIVP
  844. else if opcode=A_FDIVP then
  845. opcode:=A_FDIVRP;
  846. end;
  847. end;
  848. {*****************************************************************************
  849. Assembler
  850. *****************************************************************************}
  851. type
  852. ea=packed record
  853. sib_present : boolean;
  854. bytes : byte;
  855. size : byte;
  856. modrm : byte;
  857. sib : byte;
  858. end;
  859. procedure taicpu.create_ot(objdata:TObjData);
  860. {
  861. this function will also fix some other fields which only needs to be once
  862. }
  863. var
  864. i,l,relsize : longint;
  865. currsym : TObjSymbol;
  866. begin
  867. if ops=0 then
  868. exit;
  869. { update oper[].ot field }
  870. for i:=0 to ops-1 do
  871. with oper[i]^ do
  872. begin
  873. case typ of
  874. top_reg :
  875. begin
  876. ot:=reg_ot_table[findreg_by_number(reg)];
  877. end;
  878. top_ref :
  879. begin
  880. if ref^.refaddr=addr_no then
  881. begin
  882. { create ot field }
  883. if (ot and OT_SIZE_MASK)=0 then
  884. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  885. else
  886. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  887. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  888. ot:=ot or OT_MEM_OFFS;
  889. { fix scalefactor }
  890. if (ref^.index=NR_NO) then
  891. ref^.scalefactor:=0
  892. else
  893. if (ref^.scalefactor=0) then
  894. ref^.scalefactor:=1;
  895. end
  896. else
  897. begin
  898. if assigned(objdata) then
  899. begin
  900. currsym:=objdata.symbolref(ref^.symbol);
  901. l:=ref^.offset;
  902. if assigned(currsym) then
  903. inc(l,currsym.address);
  904. { when it is a forward jump we need to compensate the
  905. offset of the instruction since the previous time,
  906. because the symbol address is then still using the
  907. 'old-style' addressing.
  908. For backwards jumps this is not required because the
  909. address of the symbol is already adjusted to the
  910. new offset }
  911. if (l>InsOffset) and (LastInsOffset<>-1) then
  912. inc(l,InsOffset-LastInsOffset);
  913. { instruction size will then always become 2 (PFV) }
  914. relsize:=(InsOffset+2)-l;
  915. if (relsize>=-128) and (relsize<=127) and
  916. (
  917. not assigned(currsym) or
  918. (currsym.objsection=objdata.currobjsec)
  919. ) then
  920. ot:=OT_IMM8 or OT_SHORT
  921. else
  922. ot:=OT_IMM32 or OT_NEAR;
  923. end
  924. else
  925. ot:=OT_IMM32 or OT_NEAR;
  926. end;
  927. end;
  928. top_local :
  929. begin
  930. if (ot and OT_SIZE_MASK)=0 then
  931. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  932. else
  933. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  934. end;
  935. top_const :
  936. begin
  937. if opsize=S_NO then
  938. message(asmr_e_invalid_opcode_and_operand);
  939. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  940. ot:=OT_IMM8 or OT_SIGNED
  941. else
  942. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  943. end;
  944. top_none :
  945. begin
  946. { generated when there was an error in the
  947. assembler reader. It never happends when generating
  948. assembler }
  949. end;
  950. else
  951. internalerror(200402261);
  952. end;
  953. end;
  954. end;
  955. function taicpu.InsEnd:longint;
  956. begin
  957. InsEnd:=InsOffset+InsSize;
  958. end;
  959. function taicpu.Matches(p:PInsEntry):boolean;
  960. { * IF_SM stands for Size Match: any operand whose size is not
  961. * explicitly specified by the template is `really' intended to be
  962. * the same size as the first size-specified operand.
  963. * Non-specification is tolerated in the input instruction, but
  964. * _wrong_ specification is not.
  965. *
  966. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  967. * three-operand instructions such as SHLD: it implies that the
  968. * first two operands must match in size, but that the third is
  969. * required to be _unspecified_.
  970. *
  971. * IF_SB invokes Size Byte: operands with unspecified size in the
  972. * template are really bytes, and so no non-byte specification in
  973. * the input instruction will be tolerated. IF_SW similarly invokes
  974. * Size Word, and IF_SD invokes Size Doubleword.
  975. *
  976. * (The default state if neither IF_SM nor IF_SM2 is specified is
  977. * that any operand with unspecified size in the template is
  978. * required to have unspecified size in the instruction too...)
  979. }
  980. var
  981. insot,
  982. insflags,
  983. currot,
  984. i,j,asize,oprs : longint;
  985. siz : array[0..2] of longint;
  986. begin
  987. result:=false;
  988. { Check the opcode and operands }
  989. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  990. exit;
  991. for i:=0 to p^.ops-1 do
  992. begin
  993. insot:=p^.optypes[i];
  994. currot:=oper[i]^.ot;
  995. { Check the operand flags }
  996. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  997. exit;
  998. { Check if the passed operand size matches with one of
  999. the supported operand sizes }
  1000. if ((insot and OT_SIZE_MASK)<>0) and
  1001. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1002. exit;
  1003. end;
  1004. { Check operand sizes }
  1005. insflags:=p^.flags;
  1006. if insflags and IF_SMASK<>0 then
  1007. begin
  1008. { as default an untyped size can get all the sizes, this is different
  1009. from nasm, but else we need to do a lot checking which opcodes want
  1010. size or not with the automatic size generation }
  1011. asize:=-1;
  1012. if (insflags and IF_SB)<>0 then
  1013. asize:=OT_BITS8
  1014. else if (insflags and IF_SW)<>0 then
  1015. asize:=OT_BITS16
  1016. else if (insflags and IF_SD)<>0 then
  1017. asize:=OT_BITS32;
  1018. if (insflags and IF_ARMASK)<>0 then
  1019. begin
  1020. siz[0]:=0;
  1021. siz[1]:=0;
  1022. siz[2]:=0;
  1023. if (insflags and IF_AR0)<>0 then
  1024. siz[0]:=asize
  1025. else if (insflags and IF_AR1)<>0 then
  1026. siz[1]:=asize
  1027. else if (insflags and IF_AR2)<>0 then
  1028. siz[2]:=asize;
  1029. end
  1030. else
  1031. begin
  1032. siz[0]:=asize;
  1033. siz[1]:=asize;
  1034. siz[2]:=asize;
  1035. end;
  1036. if (insflags and (IF_SM or IF_SM2))<>0 then
  1037. begin
  1038. if (insflags and IF_SM2)<>0 then
  1039. oprs:=2
  1040. else
  1041. oprs:=p^.ops;
  1042. for i:=0 to oprs-1 do
  1043. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1044. begin
  1045. for j:=0 to oprs-1 do
  1046. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1047. break;
  1048. end;
  1049. end
  1050. else
  1051. oprs:=2;
  1052. { Check operand sizes }
  1053. for i:=0 to p^.ops-1 do
  1054. begin
  1055. insot:=p^.optypes[i];
  1056. currot:=oper[i]^.ot;
  1057. if ((insot and OT_SIZE_MASK)=0) and
  1058. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1059. { Immediates can always include smaller size }
  1060. ((currot and OT_IMMEDIATE)=0) and
  1061. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1062. exit;
  1063. end;
  1064. end;
  1065. result:=true;
  1066. end;
  1067. procedure taicpu.ResetPass1;
  1068. begin
  1069. { we need to reset everything here, because the choosen insentry
  1070. can be invalid for a new situation where the previously optimized
  1071. insentry is not correct }
  1072. InsEntry:=nil;
  1073. InsSize:=0;
  1074. LastInsOffset:=-1;
  1075. end;
  1076. procedure taicpu.ResetPass2;
  1077. begin
  1078. { we are here in a second pass, check if the instruction can be optimized }
  1079. if assigned(InsEntry) and
  1080. ((InsEntry^.flags and IF_PASS2)<>0) then
  1081. begin
  1082. InsEntry:=nil;
  1083. InsSize:=0;
  1084. end;
  1085. LastInsOffset:=-1;
  1086. end;
  1087. function taicpu.CheckIfValid:boolean;
  1088. begin
  1089. result:=FindInsEntry(nil);
  1090. end;
  1091. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1092. var
  1093. i : longint;
  1094. begin
  1095. result:=false;
  1096. { Things which may only be done once, not when a second pass is done to
  1097. optimize }
  1098. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1099. begin
  1100. { We need intel style operands }
  1101. SetOperandOrder(op_intel);
  1102. { create the .ot fields }
  1103. create_ot(objdata);
  1104. { set the file postion }
  1105. aktfilepos:=fileinfo;
  1106. end
  1107. else
  1108. begin
  1109. { we've already an insentry so it's valid }
  1110. result:=true;
  1111. exit;
  1112. end;
  1113. { Lookup opcode in the table }
  1114. InsSize:=-1;
  1115. i:=instabcache^[opcode];
  1116. if i=-1 then
  1117. begin
  1118. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1119. exit;
  1120. end;
  1121. insentry:=@instab[i];
  1122. while (insentry^.opcode=opcode) do
  1123. begin
  1124. if matches(insentry) then
  1125. begin
  1126. result:=true;
  1127. exit;
  1128. end;
  1129. inc(insentry);
  1130. end;
  1131. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1132. { No instruction found, set insentry to nil and inssize to -1 }
  1133. insentry:=nil;
  1134. inssize:=-1;
  1135. end;
  1136. function taicpu.Pass1(objdata:TObjData):longint;
  1137. begin
  1138. Pass1:=0;
  1139. { Save the old offset and set the new offset }
  1140. InsOffset:=ObjData.CurrObjSec.Size;
  1141. { Error? }
  1142. if (Insentry=nil) and (InsSize=-1) then
  1143. exit;
  1144. { set the file postion }
  1145. aktfilepos:=fileinfo;
  1146. { Get InsEntry }
  1147. if FindInsEntry(ObjData) then
  1148. begin
  1149. { Calculate instruction size }
  1150. InsSize:=calcsize(insentry);
  1151. if segprefix<>NR_NO then
  1152. inc(InsSize);
  1153. { Fix opsize if size if forced }
  1154. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1155. begin
  1156. if (insentry^.flags and IF_ARMASK)=0 then
  1157. begin
  1158. if (insentry^.flags and IF_SB)<>0 then
  1159. begin
  1160. if opsize=S_NO then
  1161. opsize:=S_B;
  1162. end
  1163. else if (insentry^.flags and IF_SW)<>0 then
  1164. begin
  1165. if opsize=S_NO then
  1166. opsize:=S_W;
  1167. end
  1168. else if (insentry^.flags and IF_SD)<>0 then
  1169. begin
  1170. if opsize=S_NO then
  1171. opsize:=S_L;
  1172. end;
  1173. end;
  1174. end;
  1175. LastInsOffset:=InsOffset;
  1176. Pass1:=InsSize;
  1177. exit;
  1178. end;
  1179. LastInsOffset:=-1;
  1180. end;
  1181. procedure taicpu.Pass2(objdata:TObjData);
  1182. var
  1183. c : longint;
  1184. begin
  1185. { error in pass1 ? }
  1186. if insentry=nil then
  1187. exit;
  1188. aktfilepos:=fileinfo;
  1189. { Segment override }
  1190. if (segprefix<>NR_NO) then
  1191. begin
  1192. case segprefix of
  1193. NR_CS : c:=$2e;
  1194. NR_DS : c:=$3e;
  1195. NR_ES : c:=$26;
  1196. NR_FS : c:=$64;
  1197. NR_GS : c:=$65;
  1198. NR_SS : c:=$36;
  1199. end;
  1200. objdata.writebytes(c,1);
  1201. { fix the offset for GenNode }
  1202. inc(InsOffset);
  1203. end;
  1204. { Generate the instruction }
  1205. GenCode(objdata);
  1206. end;
  1207. function taicpu.needaddrprefix(opidx:byte):boolean;
  1208. begin
  1209. result:=(oper[opidx]^.typ=top_ref) and
  1210. (oper[opidx]^.ref^.refaddr=addr_no) and
  1211. (
  1212. (
  1213. (oper[opidx]^.ref^.index<>NR_NO) and
  1214. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1215. ) or
  1216. (
  1217. (oper[opidx]^.ref^.base<>NR_NO) and
  1218. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1219. )
  1220. );
  1221. end;
  1222. function regval(r:Tregister):byte;
  1223. const
  1224. {$ifdef x86_64}
  1225. opcode_table:array[tregisterindex] of tregisterindex = (
  1226. {$i r8664op.inc}
  1227. );
  1228. {$else x86_64}
  1229. opcode_table:array[tregisterindex] of tregisterindex = (
  1230. {$i r386op.inc}
  1231. );
  1232. {$endif x86_64}
  1233. var
  1234. regidx : tregisterindex;
  1235. begin
  1236. regidx:=findreg_by_number(r);
  1237. if regidx<>0 then
  1238. result:=opcode_table[regidx]
  1239. else
  1240. begin
  1241. Message1(asmw_e_invalid_register,generic_regname(r));
  1242. result:=0;
  1243. end;
  1244. end;
  1245. {$ifdef x86_64}
  1246. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1247. var
  1248. sym : tasmsymbol;
  1249. md,s,rv : byte;
  1250. base,index,scalefactor,
  1251. o : longint;
  1252. ir,br : Tregister;
  1253. isub,bsub : tsubregister;
  1254. begin
  1255. process_ea:=false;
  1256. {Register ?}
  1257. if (input.typ=top_reg) then
  1258. begin
  1259. rv:=regval(input.reg);
  1260. output.sib_present:=false;
  1261. output.bytes:=0;
  1262. output.modrm:=$c0 or (rfield shl 3) or rv;
  1263. output.size:=1;
  1264. process_ea:=true;
  1265. exit;
  1266. end;
  1267. {No register, so memory reference.}
  1268. if (input.typ<>top_ref) then
  1269. internalerror(200409262);
  1270. ir:=input.ref^.index;
  1271. br:=input.ref^.base;
  1272. isub:=getsubreg(ir);
  1273. bsub:=getsubreg(br);
  1274. s:=input.ref^.scalefactor;
  1275. o:=input.ref^.offset;
  1276. sym:=input.ref^.symbol;
  1277. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1278. ((br<>NR_NO) and (getregtype(br)<>R_INTREGISTER)) then
  1279. internalerror(200301081);
  1280. { it's direct address }
  1281. if (br=NR_NO) and (ir=NR_NO) then
  1282. begin
  1283. { it's a pure offset }
  1284. output.sib_present:=false;
  1285. output.bytes:=4;
  1286. output.modrm:=5 or (rfield shl 3);
  1287. end
  1288. else
  1289. { it's an indirection }
  1290. begin
  1291. { 16 bit address? }
  1292. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1293. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1294. {$ifdef x86_64}
  1295. message(asmw_e_16bit_32bit_not_supported);
  1296. {$else x86_64}
  1297. message(asmw_e_16bit_not_supported);
  1298. {$endif x86_64}
  1299. {$ifdef OPTEA}
  1300. { make single reg base }
  1301. if (br=NR_NO) and (s=1) then
  1302. begin
  1303. br:=ir;
  1304. ir:=NR_NO;
  1305. end;
  1306. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1307. if (br=NR_NO) and
  1308. (((s=2) and (ir<>NR_ESP)) or
  1309. (s=3) or (s=5) or (s=9)) then
  1310. begin
  1311. br:=ir;
  1312. dec(s);
  1313. end;
  1314. { swap ESP into base if scalefactor is 1 }
  1315. if (s=1) and (ir=NR_ESP) then
  1316. begin
  1317. ir:=br;
  1318. br:=NR_ESP;
  1319. end;
  1320. {$endif OPTEA}
  1321. { wrong, for various reasons }
  1322. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1323. exit;
  1324. { base }
  1325. case br of
  1326. NR_RAX : base:=0;
  1327. NR_RCX : base:=1;
  1328. NR_RDX : base:=2;
  1329. NR_RBX : base:=3;
  1330. NR_RSP : base:=4;
  1331. NR_NO,
  1332. NR_RBP : base:=5;
  1333. NR_RSI : base:=6;
  1334. NR_RDI : base:=7;
  1335. else
  1336. exit;
  1337. end;
  1338. { index }
  1339. case ir of
  1340. NR_EAX : index:=0;
  1341. NR_ECX : index:=1;
  1342. NR_EDX : index:=2;
  1343. NR_EBX : index:=3;
  1344. NR_NO : index:=4;
  1345. NR_EBP : index:=5;
  1346. NR_ESI : index:=6;
  1347. NR_EDI : index:=7;
  1348. else
  1349. exit;
  1350. end;
  1351. case s of
  1352. 0,
  1353. 1 : scalefactor:=0;
  1354. 2 : scalefactor:=1;
  1355. 4 : scalefactor:=2;
  1356. 8 : scalefactor:=3;
  1357. else
  1358. exit;
  1359. end;
  1360. if (br=NR_NO) or
  1361. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1362. md:=0
  1363. else
  1364. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1365. md:=1
  1366. else
  1367. md:=2;
  1368. if (br=NR_NO) or (md=2) then
  1369. output.bytes:=4
  1370. else
  1371. output.bytes:=md;
  1372. { SIB needed ? }
  1373. if (ir=NR_NO) and (br<>NR_ESP) then
  1374. begin
  1375. output.sib_present:=false;
  1376. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1377. end
  1378. else
  1379. begin
  1380. output.sib_present:=true;
  1381. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1382. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1383. end;
  1384. end;
  1385. if output.sib_present then
  1386. output.size:=2+output.bytes
  1387. else
  1388. output.size:=1+output.bytes;
  1389. process_ea:=true;
  1390. end;
  1391. {$else x86_64}
  1392. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1393. var
  1394. sym : tasmsymbol;
  1395. md,s,rv : byte;
  1396. base,index,scalefactor,
  1397. o : longint;
  1398. ir,br : Tregister;
  1399. isub,bsub : tsubregister;
  1400. begin
  1401. process_ea:=false;
  1402. {Register ?}
  1403. if (input.typ=top_reg) then
  1404. begin
  1405. rv:=regval(input.reg);
  1406. output.sib_present:=false;
  1407. output.bytes:=0;
  1408. output.modrm:=$c0 or (rfield shl 3) or rv;
  1409. output.size:=1;
  1410. process_ea:=true;
  1411. exit;
  1412. end;
  1413. {No register, so memory reference.}
  1414. if (input.typ<>top_ref) then
  1415. internalerror(200409262);
  1416. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1417. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1418. internalerror(200301081);
  1419. ir:=input.ref^.index;
  1420. br:=input.ref^.base;
  1421. isub:=getsubreg(ir);
  1422. bsub:=getsubreg(br);
  1423. s:=input.ref^.scalefactor;
  1424. o:=input.ref^.offset;
  1425. sym:=input.ref^.symbol;
  1426. { it's direct address }
  1427. if (br=NR_NO) and (ir=NR_NO) then
  1428. begin
  1429. { it's a pure offset }
  1430. output.sib_present:=false;
  1431. output.bytes:=4;
  1432. output.modrm:=5 or (rfield shl 3);
  1433. end
  1434. else
  1435. { it's an indirection }
  1436. begin
  1437. { 16 bit address? }
  1438. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1439. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1440. message(asmw_e_16bit_not_supported);
  1441. {$ifdef OPTEA}
  1442. { make single reg base }
  1443. if (br=NR_NO) and (s=1) then
  1444. begin
  1445. br:=ir;
  1446. ir:=NR_NO;
  1447. end;
  1448. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1449. if (br=NR_NO) and
  1450. (((s=2) and (ir<>NR_ESP)) or
  1451. (s=3) or (s=5) or (s=9)) then
  1452. begin
  1453. br:=ir;
  1454. dec(s);
  1455. end;
  1456. { swap ESP into base if scalefactor is 1 }
  1457. if (s=1) and (ir=NR_ESP) then
  1458. begin
  1459. ir:=br;
  1460. br:=NR_ESP;
  1461. end;
  1462. {$endif OPTEA}
  1463. { wrong, for various reasons }
  1464. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1465. exit;
  1466. { base }
  1467. case br of
  1468. NR_EAX : base:=0;
  1469. NR_ECX : base:=1;
  1470. NR_EDX : base:=2;
  1471. NR_EBX : base:=3;
  1472. NR_ESP : base:=4;
  1473. NR_NO,
  1474. NR_EBP : base:=5;
  1475. NR_ESI : base:=6;
  1476. NR_EDI : base:=7;
  1477. else
  1478. exit;
  1479. end;
  1480. { index }
  1481. case ir of
  1482. NR_EAX : index:=0;
  1483. NR_ECX : index:=1;
  1484. NR_EDX : index:=2;
  1485. NR_EBX : index:=3;
  1486. NR_NO : index:=4;
  1487. NR_EBP : index:=5;
  1488. NR_ESI : index:=6;
  1489. NR_EDI : index:=7;
  1490. else
  1491. exit;
  1492. end;
  1493. case s of
  1494. 0,
  1495. 1 : scalefactor:=0;
  1496. 2 : scalefactor:=1;
  1497. 4 : scalefactor:=2;
  1498. 8 : scalefactor:=3;
  1499. else
  1500. exit;
  1501. end;
  1502. if (br=NR_NO) or
  1503. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1504. md:=0
  1505. else
  1506. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1507. md:=1
  1508. else
  1509. md:=2;
  1510. if (br=NR_NO) or (md=2) then
  1511. output.bytes:=4
  1512. else
  1513. output.bytes:=md;
  1514. { SIB needed ? }
  1515. if (ir=NR_NO) and (br<>NR_ESP) then
  1516. begin
  1517. output.sib_present:=false;
  1518. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1519. end
  1520. else
  1521. begin
  1522. output.sib_present:=true;
  1523. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1524. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1525. end;
  1526. end;
  1527. if output.sib_present then
  1528. output.size:=2+output.bytes
  1529. else
  1530. output.size:=1+output.bytes;
  1531. process_ea:=true;
  1532. end;
  1533. {$endif x86_64}
  1534. function taicpu.calcsize(p:PInsEntry):shortint;
  1535. var
  1536. codes : pchar;
  1537. c : byte;
  1538. len : shortint;
  1539. ea_data : ea;
  1540. begin
  1541. len:=0;
  1542. codes:=@p^.code;
  1543. repeat
  1544. c:=ord(codes^);
  1545. inc(codes);
  1546. case c of
  1547. 0 :
  1548. break;
  1549. 1,2,3 :
  1550. begin
  1551. inc(codes,c);
  1552. inc(len,c);
  1553. end;
  1554. 8,9,10,11 :
  1555. begin
  1556. inc(codes);
  1557. inc(len);
  1558. end;
  1559. 4,5,6,7 :
  1560. begin
  1561. if opsize=S_W then
  1562. inc(len,2)
  1563. else
  1564. inc(len);
  1565. end;
  1566. 15,
  1567. 12,13,14,
  1568. 16,17,18,
  1569. 20,21,22,
  1570. 40,41,42 :
  1571. inc(len);
  1572. 24,25,26,
  1573. 31,
  1574. 48,49,50 :
  1575. inc(len,2);
  1576. 28,29,30, { we don't have 16 bit immediates code }
  1577. 32,33,34,
  1578. 52,53,54,
  1579. 56,57,58 :
  1580. inc(len,4);
  1581. 192,193,194 :
  1582. if NeedAddrPrefix(c-192) then
  1583. inc(len);
  1584. 208,209,210 :
  1585. begin
  1586. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1587. OT_BITS16,
  1588. OT_BITS64 :
  1589. inc(len);
  1590. end;
  1591. end;
  1592. 212,
  1593. 214 :
  1594. inc(len);
  1595. 200,
  1596. 201,
  1597. 202,
  1598. 213,
  1599. 215,
  1600. 217,218: ;
  1601. 219,220 :
  1602. inc(len);
  1603. 64..191 :
  1604. begin
  1605. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1606. Message(asmw_e_invalid_effective_address)
  1607. else
  1608. inc(len,ea_data.size);
  1609. end;
  1610. else
  1611. InternalError(200603141);
  1612. end;
  1613. until false;
  1614. calcsize:=len;
  1615. end;
  1616. procedure taicpu.GenCode(objdata:TObjData);
  1617. {
  1618. * the actual codes (C syntax, i.e. octal):
  1619. * \0 - terminates the code. (Unless it's a literal of course.)
  1620. * \1, \2, \3 - that many literal bytes follow in the code stream
  1621. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1622. * (POP is never used for CS) depending on operand 0
  1623. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1624. * on operand 0
  1625. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1626. * to the register value of operand 0, 1 or 2
  1627. * \13 - a literal byte follows in the code stream, to be added
  1628. * to the condition code value of the instruction.
  1629. * \17 - encodes the literal byte 0. (Some compilers don't take
  1630. * kindly to a zero byte in the _middle_ of a compile time
  1631. * string constant, so I had to put this hack in.)
  1632. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1633. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1634. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1635. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1636. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1637. * assembly mode or the address-size override on the operand
  1638. * \37 - a word constant, from the _segment_ part of operand 0
  1639. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1640. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1641. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1642. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1643. * assembly mode or the address-size override on the operand
  1644. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1645. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1646. * field the register value of operand b.
  1647. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1648. * field equal to digit b.
  1649. * \300,\301,\302 - might be an 0x67 or 0x48 byte, depending on the address size of
  1650. * the memory reference in operand x.
  1651. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1652. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1653. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1654. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  1655. * size of operand x.
  1656. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1657. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1658. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1659. * \327 - indicates that this instruction is only valid when the
  1660. * operand size is the default (instruction to disassembler,
  1661. * generates no code in the assembler)
  1662. }
  1663. var
  1664. currval : longint;
  1665. currsym : tobjsymbol;
  1666. procedure getvalsym(opidx:longint);
  1667. begin
  1668. case oper[opidx]^.typ of
  1669. top_ref :
  1670. begin
  1671. currval:=oper[opidx]^.ref^.offset;
  1672. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  1673. end;
  1674. top_const :
  1675. begin
  1676. currval:=longint(oper[opidx]^.val);
  1677. currsym:=nil;
  1678. end;
  1679. else
  1680. Message(asmw_e_immediate_or_reference_expected);
  1681. end;
  1682. end;
  1683. const
  1684. CondVal:array[TAsmCond] of byte=($0,
  1685. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1686. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1687. $0, $A, $A, $B, $8, $4);
  1688. var
  1689. c : byte;
  1690. pb,
  1691. codes : pchar;
  1692. bytes : array[0..3] of byte;
  1693. rfield,
  1694. data,s,opidx : longint;
  1695. ea_data : ea;
  1696. begin
  1697. { safety check }
  1698. if objdata.currobjsec.size<>insoffset then
  1699. internalerror(200130121);
  1700. { load data to write }
  1701. codes:=insentry^.code;
  1702. { Force word push/pop for registers }
  1703. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1704. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1705. begin
  1706. bytes[0]:=$66;
  1707. objdata.writebytes(bytes,1);
  1708. end;
  1709. repeat
  1710. c:=ord(codes^);
  1711. inc(codes);
  1712. case c of
  1713. 0 :
  1714. break;
  1715. 1,2,3 :
  1716. begin
  1717. objdata.writebytes(codes^,c);
  1718. inc(codes,c);
  1719. end;
  1720. 4,6 :
  1721. begin
  1722. case oper[0]^.reg of
  1723. NR_CS:
  1724. bytes[0]:=$e;
  1725. NR_NO,
  1726. NR_DS:
  1727. bytes[0]:=$1e;
  1728. NR_ES:
  1729. bytes[0]:=$6;
  1730. NR_SS:
  1731. bytes[0]:=$16;
  1732. else
  1733. internalerror(777004);
  1734. end;
  1735. if c=4 then
  1736. inc(bytes[0]);
  1737. objdata.writebytes(bytes,1);
  1738. end;
  1739. 5,7 :
  1740. begin
  1741. case oper[0]^.reg of
  1742. NR_FS:
  1743. bytes[0]:=$a0;
  1744. NR_GS:
  1745. bytes[0]:=$a8;
  1746. else
  1747. internalerror(777005);
  1748. end;
  1749. if c=5 then
  1750. inc(bytes[0]);
  1751. objdata.writebytes(bytes,1);
  1752. end;
  1753. 8,9,10 :
  1754. begin
  1755. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1756. inc(codes);
  1757. objdata.writebytes(bytes,1);
  1758. end;
  1759. 11 :
  1760. begin
  1761. bytes[0]:=ord(codes^)+condval[condition];
  1762. inc(codes);
  1763. objdata.writebytes(bytes,1);
  1764. end;
  1765. 15 :
  1766. begin
  1767. bytes[0]:=0;
  1768. objdata.writebytes(bytes,1);
  1769. end;
  1770. 12,13,14 :
  1771. begin
  1772. getvalsym(c-12);
  1773. if (currval<-128) or (currval>127) then
  1774. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1775. if assigned(currsym) then
  1776. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1777. else
  1778. objdata.writebytes(currval,1);
  1779. end;
  1780. 16,17,18 :
  1781. begin
  1782. getvalsym(c-16);
  1783. if (currval<-256) or (currval>255) then
  1784. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1785. if assigned(currsym) then
  1786. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1787. else
  1788. objdata.writebytes(currval,1);
  1789. end;
  1790. 20,21,22 :
  1791. begin
  1792. getvalsym(c-20);
  1793. if (currval<0) or (currval>255) then
  1794. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1795. if assigned(currsym) then
  1796. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1797. else
  1798. objdata.writebytes(currval,1);
  1799. end;
  1800. 24,25,26 :
  1801. begin
  1802. getvalsym(c-24);
  1803. if (currval<-65536) or (currval>65535) then
  1804. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1805. if assigned(currsym) then
  1806. objdata.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1807. else
  1808. objdata.writebytes(currval,2);
  1809. end;
  1810. 28,29,30 :
  1811. begin
  1812. getvalsym(c-28);
  1813. if assigned(currsym) then
  1814. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1815. else
  1816. objdata.writebytes(currval,4);
  1817. end;
  1818. 32,33,34 :
  1819. begin
  1820. getvalsym(c-32);
  1821. if assigned(currsym) then
  1822. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1823. else
  1824. objdata.writebytes(currval,4);
  1825. end;
  1826. 40,41,42 :
  1827. begin
  1828. getvalsym(c-40);
  1829. data:=currval-insend;
  1830. if assigned(currsym) then
  1831. inc(data,currsym.address);
  1832. if (data>127) or (data<-128) then
  1833. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1834. objdata.writebytes(data,1);
  1835. end;
  1836. 52,53,54 :
  1837. begin
  1838. getvalsym(c-52);
  1839. if assigned(currsym) then
  1840. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1841. else
  1842. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1843. end;
  1844. 56,57,58 :
  1845. begin
  1846. getvalsym(c-56);
  1847. if assigned(currsym) then
  1848. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1849. else
  1850. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1851. end;
  1852. 192,193,194 :
  1853. begin
  1854. if NeedAddrPrefix(c-192) then
  1855. begin
  1856. bytes[0]:=$67;
  1857. objdata.writebytes(bytes,1);
  1858. end;
  1859. end;
  1860. 200 :
  1861. begin
  1862. bytes[0]:=$67;
  1863. objdata.writebytes(bytes,1);
  1864. end;
  1865. 208,209,210 :
  1866. begin
  1867. case oper[c-208]^.ot and OT_SIZE_MASK of
  1868. OT_BITS16 :
  1869. begin
  1870. bytes[0]:=$66;
  1871. objdata.writebytes(bytes,1);
  1872. end;
  1873. OT_BITS64 :
  1874. begin
  1875. {$ifndef x86_64}
  1876. Message(asmw_e_64bit_not_supported);
  1877. {$endif x86_64}
  1878. bytes[0]:=$48;
  1879. objdata.writebytes(bytes,1);
  1880. end;
  1881. end;
  1882. end;
  1883. 212 :
  1884. begin
  1885. bytes[0]:=$66;
  1886. objdata.writebytes(bytes,1);
  1887. end;
  1888. 214 :
  1889. begin
  1890. {$ifndef x86_64}
  1891. Message(asmw_e_64bit_not_supported);
  1892. {$endif x86_64}
  1893. bytes[0]:=$48;
  1894. objdata.writebytes(bytes,1);
  1895. end;
  1896. 219 :
  1897. begin
  1898. bytes[0]:=$f3;
  1899. objdata.writebytes(bytes,1);
  1900. end;
  1901. 220 :
  1902. begin
  1903. bytes[0]:=$f2;
  1904. objdata.writebytes(bytes,1);
  1905. end;
  1906. 201,
  1907. 202,
  1908. 213,
  1909. 215,
  1910. 217,218 :
  1911. begin
  1912. { these are dissambler hints or 32 bit prefixes which
  1913. are not needed }
  1914. end;
  1915. 31,
  1916. 48,49,50 :
  1917. begin
  1918. InternalError(777006);
  1919. end
  1920. else
  1921. begin
  1922. if (c>=64) and (c<=191) then
  1923. begin
  1924. if (c<127) then
  1925. begin
  1926. if (oper[c and 7]^.typ=top_reg) then
  1927. rfield:=regval(oper[c and 7]^.reg)
  1928. else
  1929. rfield:=regval(oper[c and 7]^.ref^.base);
  1930. end
  1931. else
  1932. rfield:=c and 7;
  1933. opidx:=(c shr 3) and 7;
  1934. if not process_ea(oper[opidx]^,ea_data,rfield) then
  1935. Message(asmw_e_invalid_effective_address);
  1936. pb:=@bytes;
  1937. pb^:=chr(ea_data.modrm);
  1938. inc(pb);
  1939. if ea_data.sib_present then
  1940. begin
  1941. pb^:=chr(ea_data.sib);
  1942. inc(pb);
  1943. end;
  1944. s:=pb-pchar(@bytes);
  1945. objdata.writebytes(bytes,s);
  1946. case ea_data.bytes of
  1947. 0 : ;
  1948. 1 :
  1949. begin
  1950. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  1951. begin
  1952. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  1953. objdata.writereloc(oper[opidx]^.ref^.offset,1,currsym,RELOC_ABSOLUTE)
  1954. end
  1955. else
  1956. begin
  1957. bytes[0]:=oper[opidx]^.ref^.offset;
  1958. objdata.writebytes(bytes,1);
  1959. end;
  1960. inc(s);
  1961. end;
  1962. 2,4 :
  1963. begin
  1964. objdata.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,
  1965. objdata.symbolref(oper[opidx]^.ref^.symbol),RELOC_ABSOLUTE);
  1966. inc(s,ea_data.bytes);
  1967. end;
  1968. end;
  1969. end
  1970. else
  1971. InternalError(777007);
  1972. end;
  1973. end;
  1974. until false;
  1975. end;
  1976. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1977. begin
  1978. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  1979. (regtype = R_INTREGISTER) and
  1980. (ops=2) and
  1981. (oper[0]^.typ=top_reg) and
  1982. (oper[1]^.typ=top_reg) and
  1983. (oper[0]^.reg=oper[1]^.reg)
  1984. ) or
  1985. (((opcode=A_MOVSS) or (opcode=A_MOVSD)) and
  1986. (regtype = R_MMREGISTER) and
  1987. (ops=2) and
  1988. (oper[0]^.typ=top_reg) and
  1989. (oper[1]^.typ=top_reg) and
  1990. (oper[0]^.reg=oper[1]^.reg)
  1991. );
  1992. end;
  1993. procedure build_spilling_operation_type_table;
  1994. var
  1995. opcode : tasmop;
  1996. i : integer;
  1997. begin
  1998. new(operation_type_table);
  1999. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2000. for opcode:=low(tasmop) to high(tasmop) do
  2001. begin
  2002. for i:=1 to MaxInsChanges do
  2003. begin
  2004. case InsProp[opcode].Ch[i] of
  2005. Ch_Rop1 :
  2006. operation_type_table^[opcode,0]:=operand_read;
  2007. Ch_Wop1 :
  2008. operation_type_table^[opcode,0]:=operand_write;
  2009. Ch_RWop1,
  2010. Ch_Mop1 :
  2011. operation_type_table^[opcode,0]:=operand_readwrite;
  2012. Ch_Rop2 :
  2013. operation_type_table^[opcode,1]:=operand_read;
  2014. Ch_Wop2 :
  2015. operation_type_table^[opcode,1]:=operand_write;
  2016. Ch_RWop2,
  2017. Ch_Mop2 :
  2018. operation_type_table^[opcode,1]:=operand_readwrite;
  2019. Ch_Rop3 :
  2020. operation_type_table^[opcode,2]:=operand_read;
  2021. Ch_Wop3 :
  2022. operation_type_table^[opcode,2]:=operand_write;
  2023. Ch_RWop3,
  2024. Ch_Mop3 :
  2025. operation_type_table^[opcode,2]:=operand_readwrite;
  2026. end;
  2027. end;
  2028. end;
  2029. { Special cases that can't be decoded from the InsChanges flags }
  2030. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  2031. end;
  2032. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2033. begin
  2034. { the information in the instruction table is made for the string copy
  2035. operation MOVSD so hack here (FK)
  2036. }
  2037. if (opcode=A_MOVSD) and (ops=2) then
  2038. begin
  2039. case opnr of
  2040. 0:
  2041. result:=operand_read;
  2042. 1:
  2043. result:=operand_write;
  2044. else
  2045. internalerror(200506055);
  2046. end
  2047. end
  2048. else
  2049. result:=operation_type_table^[opcode,opnr];
  2050. end;
  2051. function spilling_create_load(const ref:treference;r:tregister): tai;
  2052. begin
  2053. case getregtype(r) of
  2054. R_INTREGISTER :
  2055. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
  2056. R_MMREGISTER :
  2057. case getsubreg(r) of
  2058. R_SUBMMD:
  2059. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  2060. R_SUBMMS:
  2061. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  2062. else
  2063. internalerror(200506043);
  2064. end;
  2065. else
  2066. internalerror(200401041);
  2067. end;
  2068. end;
  2069. function spilling_create_store(r:tregister; const ref:treference): tai;
  2070. begin
  2071. case getregtype(r) of
  2072. R_INTREGISTER :
  2073. result:=taicpu.op_reg_ref(A_MOV,reg2opsize(r),r,ref);
  2074. R_MMREGISTER :
  2075. case getsubreg(r) of
  2076. R_SUBMMD:
  2077. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  2078. R_SUBMMS:
  2079. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  2080. else
  2081. internalerror(200506042);
  2082. end;
  2083. else
  2084. internalerror(200401041);
  2085. end;
  2086. end;
  2087. {*****************************************************************************
  2088. Instruction table
  2089. *****************************************************************************}
  2090. procedure BuildInsTabCache;
  2091. var
  2092. i : longint;
  2093. begin
  2094. new(instabcache);
  2095. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2096. i:=0;
  2097. while (i<InsTabEntries) do
  2098. begin
  2099. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2100. InsTabCache^[InsTab[i].OPcode]:=i;
  2101. inc(i);
  2102. end;
  2103. end;
  2104. procedure InitAsm;
  2105. begin
  2106. build_spilling_operation_type_table;
  2107. if not assigned(instabcache) then
  2108. BuildInsTabCache;
  2109. end;
  2110. procedure DoneAsm;
  2111. begin
  2112. if assigned(operation_type_table) then
  2113. begin
  2114. dispose(operation_type_table);
  2115. operation_type_table:=nil;
  2116. end;
  2117. if assigned(instabcache) then
  2118. begin
  2119. dispose(instabcache);
  2120. instabcache:=nil;
  2121. end;
  2122. end;
  2123. begin
  2124. cai_align:=tai_align;
  2125. cai_cpu:=taicpu;
  2126. end.