2
0

aasmcpu.pas 69 KB

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