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