aasmcpu.pas 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153
  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. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  928. ot:=OT_IMM8 or OT_SIGNED
  929. else
  930. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  931. end;
  932. top_none :
  933. begin
  934. { generated when there was an error in the
  935. assembler reader. It never happends when generating
  936. assembler }
  937. end;
  938. else
  939. internalerror(200402261);
  940. end;
  941. end;
  942. end;
  943. function taicpu.InsEnd:longint;
  944. begin
  945. InsEnd:=InsOffset+InsSize;
  946. end;
  947. function taicpu.Matches(p:PInsEntry):longint;
  948. { * IF_SM stands for Size Match: any operand whose size is not
  949. * explicitly specified by the template is `really' intended to be
  950. * the same size as the first size-specified operand.
  951. * Non-specification is tolerated in the input instruction, but
  952. * _wrong_ specification is not.
  953. *
  954. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  955. * three-operand instructions such as SHLD: it implies that the
  956. * first two operands must match in size, but that the third is
  957. * required to be _unspecified_.
  958. *
  959. * IF_SB invokes Size Byte: operands with unspecified size in the
  960. * template are really bytes, and so no non-byte specification in
  961. * the input instruction will be tolerated. IF_SW similarly invokes
  962. * Size Word, and IF_SD invokes Size Doubleword.
  963. *
  964. * (The default state if neither IF_SM nor IF_SM2 is specified is
  965. * that any operand with unspecified size in the template is
  966. * required to have unspecified size in the instruction too...)
  967. }
  968. var
  969. i,j,asize,oprs : longint;
  970. siz : array[0..2] of longint;
  971. begin
  972. Matches:=100;
  973. { Check the opcode and operands }
  974. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  975. begin
  976. Matches:=0;
  977. exit;
  978. end;
  979. { Check that no spurious colons or TOs are present }
  980. for i:=0 to p^.ops-1 do
  981. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  982. begin
  983. Matches:=0;
  984. exit;
  985. end;
  986. { Check that the operand flags all match up }
  987. for i:=0 to p^.ops-1 do
  988. begin
  989. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  990. ((p^.optypes[i] and OT_SIZE_MASK) and
  991. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  992. begin
  993. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  994. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  995. begin
  996. Matches:=0;
  997. exit;
  998. end
  999. else
  1000. Matches:=1;
  1001. end;
  1002. end;
  1003. { Check operand sizes }
  1004. { as default an untyped size can get all the sizes, this is different
  1005. from nasm, but else we need to do a lot checking which opcodes want
  1006. size or not with the automatic size generation }
  1007. asize:=longint($ffffffff);
  1008. if (p^.flags and IF_SB)<>0 then
  1009. asize:=OT_BITS8
  1010. else if (p^.flags and IF_SW)<>0 then
  1011. asize:=OT_BITS16
  1012. else if (p^.flags and IF_SD)<>0 then
  1013. asize:=OT_BITS32;
  1014. if (p^.flags and IF_ARMASK)<>0 then
  1015. begin
  1016. siz[0]:=0;
  1017. siz[1]:=0;
  1018. siz[2]:=0;
  1019. if (p^.flags and IF_AR0)<>0 then
  1020. siz[0]:=asize
  1021. else if (p^.flags and IF_AR1)<>0 then
  1022. siz[1]:=asize
  1023. else if (p^.flags and IF_AR2)<>0 then
  1024. siz[2]:=asize;
  1025. end
  1026. else
  1027. begin
  1028. { we can leave because the size for all operands is forced to be
  1029. the same
  1030. but not if IF_SB IF_SW or IF_SD is set PM }
  1031. if asize=-1 then
  1032. exit;
  1033. siz[0]:=asize;
  1034. siz[1]:=asize;
  1035. siz[2]:=asize;
  1036. end;
  1037. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1038. begin
  1039. if (p^.flags and IF_SM2)<>0 then
  1040. oprs:=2
  1041. else
  1042. oprs:=p^.ops;
  1043. for i:=0 to oprs-1 do
  1044. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1045. begin
  1046. for j:=0 to oprs-1 do
  1047. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1048. break;
  1049. end;
  1050. end
  1051. else
  1052. oprs:=2;
  1053. { Check operand sizes }
  1054. for i:=0 to p^.ops-1 do
  1055. begin
  1056. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1057. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1058. { Immediates can always include smaller size }
  1059. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1060. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1061. Matches:=2;
  1062. end;
  1063. end;
  1064. procedure taicpu.ResetPass1;
  1065. begin
  1066. { we need to reset everything here, because the choosen insentry
  1067. can be invalid for a new situation where the previously optimized
  1068. insentry is not correct }
  1069. InsEntry:=nil;
  1070. InsSize:=0;
  1071. LastInsOffset:=-1;
  1072. end;
  1073. procedure taicpu.ResetPass2;
  1074. begin
  1075. { we are here in a second pass, check if the instruction can be optimized }
  1076. if assigned(InsEntry) and
  1077. ((InsEntry^.flags and IF_PASS2)<>0) then
  1078. begin
  1079. InsEntry:=nil;
  1080. InsSize:=0;
  1081. end;
  1082. LastInsOffset:=-1;
  1083. end;
  1084. function taicpu.CheckIfValid:boolean;
  1085. begin
  1086. result:=FindInsEntry;
  1087. end;
  1088. function taicpu.FindInsentry:boolean;
  1089. var
  1090. i : longint;
  1091. begin
  1092. result:=false;
  1093. { Things which may only be done once, not when a second pass is done to
  1094. optimize }
  1095. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1096. begin
  1097. { We need intel style operands }
  1098. SetOperandOrder(op_intel);
  1099. { create the .ot fields }
  1100. create_ot;
  1101. { set the file postion }
  1102. aktfilepos:=fileinfo;
  1103. end
  1104. else
  1105. begin
  1106. { we've already an insentry so it's valid }
  1107. result:=true;
  1108. exit;
  1109. end;
  1110. { Lookup opcode in the table }
  1111. InsSize:=-1;
  1112. i:=instabcache^[opcode];
  1113. if i=-1 then
  1114. begin
  1115. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1116. exit;
  1117. end;
  1118. insentry:=@instab[i];
  1119. while (insentry^.opcode=opcode) do
  1120. begin
  1121. if matches(insentry)=100 then
  1122. begin
  1123. result:=true;
  1124. exit;
  1125. end;
  1126. inc(i);
  1127. insentry:=@instab[i];
  1128. end;
  1129. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1130. { No instruction found, set insentry to nil and inssize to -1 }
  1131. insentry:=nil;
  1132. inssize:=-1;
  1133. end;
  1134. function taicpu.Pass1(offset:longint):longint;
  1135. begin
  1136. Pass1:=0;
  1137. { Save the old offset and set the new offset }
  1138. InsOffset:=Offset;
  1139. { Error? }
  1140. if (Insentry=nil) and (InsSize=-1) then
  1141. exit;
  1142. { set the file postion }
  1143. aktfilepos:=fileinfo;
  1144. { Get InsEntry }
  1145. if FindInsEntry then
  1146. begin
  1147. { Calculate instruction size }
  1148. InsSize:=calcsize(insentry);
  1149. if segprefix<>NR_NO then
  1150. inc(InsSize);
  1151. { Fix opsize if size if forced }
  1152. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1153. begin
  1154. if (insentry^.flags and IF_ARMASK)=0 then
  1155. begin
  1156. if (insentry^.flags and IF_SB)<>0 then
  1157. begin
  1158. if opsize=S_NO then
  1159. opsize:=S_B;
  1160. end
  1161. else if (insentry^.flags and IF_SW)<>0 then
  1162. begin
  1163. if opsize=S_NO then
  1164. opsize:=S_W;
  1165. end
  1166. else if (insentry^.flags and IF_SD)<>0 then
  1167. begin
  1168. if opsize=S_NO then
  1169. opsize:=S_L;
  1170. end;
  1171. end;
  1172. end;
  1173. LastInsOffset:=InsOffset;
  1174. Pass1:=InsSize;
  1175. exit;
  1176. end;
  1177. LastInsOffset:=-1;
  1178. end;
  1179. procedure taicpu.Pass2(objdata:TAsmObjectData);
  1180. var
  1181. c : longint;
  1182. begin
  1183. { error in pass1 ? }
  1184. if insentry=nil then
  1185. exit;
  1186. aktfilepos:=fileinfo;
  1187. { Segment override }
  1188. if (segprefix<>NR_NO) then
  1189. begin
  1190. case segprefix of
  1191. NR_CS : c:=$2e;
  1192. NR_DS : c:=$3e;
  1193. NR_ES : c:=$26;
  1194. NR_FS : c:=$64;
  1195. NR_GS : c:=$65;
  1196. NR_SS : c:=$36;
  1197. end;
  1198. objdata.writebytes(c,1);
  1199. { fix the offset for GenNode }
  1200. inc(InsOffset);
  1201. end;
  1202. { Generate the instruction }
  1203. GenCode(objdata);
  1204. end;
  1205. function taicpu.needaddrprefix(opidx:byte):boolean;
  1206. begin
  1207. result:=(oper[opidx]^.typ=top_ref) and
  1208. (oper[opidx]^.ref^.refaddr=addr_no) and
  1209. (
  1210. (
  1211. (oper[opidx]^.ref^.index<>NR_NO) and
  1212. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBD)
  1213. ) or
  1214. (
  1215. (oper[opidx]^.ref^.base<>NR_NO) and
  1216. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBD)
  1217. )
  1218. );
  1219. end;
  1220. function regval(r:Tregister):byte;
  1221. const
  1222. {$ifdef x86_64}
  1223. opcode_table:array[tregisterindex] of tregisterindex = (
  1224. {$i r8664op.inc}
  1225. );
  1226. {$else x86_64}
  1227. opcode_table:array[tregisterindex] of tregisterindex = (
  1228. {$i r386op.inc}
  1229. );
  1230. {$endif x86_64}
  1231. var
  1232. regidx : tregisterindex;
  1233. begin
  1234. regidx:=findreg_by_number(r);
  1235. if regidx<>0 then
  1236. result:=opcode_table[regidx]
  1237. else
  1238. begin
  1239. Message1(asmw_e_invalid_register,generic_regname(r));
  1240. result:=0;
  1241. end;
  1242. end;
  1243. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1244. var
  1245. sym : tasmsymbol;
  1246. md,s,rv : byte;
  1247. base,index,scalefactor,
  1248. o : longint;
  1249. ir,br : Tregister;
  1250. isub,bsub : tsubregister;
  1251. begin
  1252. process_ea:=false;
  1253. {Register ?}
  1254. if (input.typ=top_reg) then
  1255. begin
  1256. rv:=regval(input.reg);
  1257. output.sib_present:=false;
  1258. output.bytes:=0;
  1259. output.modrm:=$c0 or (rfield shl 3) or rv;
  1260. output.size:=1;
  1261. process_ea:=true;
  1262. exit;
  1263. end;
  1264. {No register, so memory reference.}
  1265. if (input.typ<>top_ref) then
  1266. internalerror(200409262);
  1267. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1268. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1269. internalerror(200301081);
  1270. ir:=input.ref^.index;
  1271. br:=input.ref^.base;
  1272. isub:=getsubreg(ir);
  1273. bsub:=getsubreg(br);
  1274. s:=input.ref^.scalefactor;
  1275. o:=input.ref^.offset;
  1276. sym:=input.ref^.symbol;
  1277. { it's direct address }
  1278. if (br=NR_NO) and (ir=NR_NO) then
  1279. begin
  1280. { it's a pure offset }
  1281. output.sib_present:=false;
  1282. output.bytes:=4;
  1283. output.modrm:=5 or (rfield shl 3);
  1284. end
  1285. else
  1286. { it's an indirection }
  1287. begin
  1288. { 16 bit address? }
  1289. if ((ir<>NR_NO) and (isub<>R_SUBD)) or
  1290. ((br<>NR_NO) and (bsub<>R_SUBD)) then
  1291. message(asmw_e_16bit_not_supported);
  1292. {$ifdef OPTEA}
  1293. { make single reg base }
  1294. if (br=NR_NO) and (s=1) then
  1295. begin
  1296. br:=ir;
  1297. ir:=NR_NO;
  1298. end;
  1299. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1300. if (br=NR_NO) and
  1301. (((s=2) and (ir<>NR_ESP)) or
  1302. (s=3) or (s=5) or (s=9)) then
  1303. begin
  1304. br:=ir;
  1305. dec(s);
  1306. end;
  1307. { swap ESP into base if scalefactor is 1 }
  1308. if (s=1) and (ir=NR_ESP) then
  1309. begin
  1310. ir:=br;
  1311. br:=NR_ESP;
  1312. end;
  1313. {$endif OPTEA}
  1314. { wrong, for various reasons }
  1315. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1316. exit;
  1317. { base }
  1318. case br of
  1319. NR_EAX : base:=0;
  1320. NR_ECX : base:=1;
  1321. NR_EDX : base:=2;
  1322. NR_EBX : base:=3;
  1323. NR_ESP : base:=4;
  1324. NR_NO,
  1325. NR_EBP : base:=5;
  1326. NR_ESI : base:=6;
  1327. NR_EDI : base:=7;
  1328. else
  1329. exit;
  1330. end;
  1331. { index }
  1332. case ir of
  1333. NR_EAX : index:=0;
  1334. NR_ECX : index:=1;
  1335. NR_EDX : index:=2;
  1336. NR_EBX : index:=3;
  1337. NR_NO : index:=4;
  1338. NR_EBP : index:=5;
  1339. NR_ESI : index:=6;
  1340. NR_EDI : index:=7;
  1341. else
  1342. exit;
  1343. end;
  1344. case s of
  1345. 0,
  1346. 1 : scalefactor:=0;
  1347. 2 : scalefactor:=1;
  1348. 4 : scalefactor:=2;
  1349. 8 : scalefactor:=3;
  1350. else
  1351. exit;
  1352. end;
  1353. if (br=NR_NO) or
  1354. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1355. md:=0
  1356. else
  1357. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1358. md:=1
  1359. else
  1360. md:=2;
  1361. if (br=NR_NO) or (md=2) then
  1362. output.bytes:=4
  1363. else
  1364. output.bytes:=md;
  1365. { SIB needed ? }
  1366. if (ir=NR_NO) and (br<>NR_ESP) then
  1367. begin
  1368. output.sib_present:=false;
  1369. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1370. end
  1371. else
  1372. begin
  1373. output.sib_present:=true;
  1374. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1375. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1376. end;
  1377. end;
  1378. if output.sib_present then
  1379. output.size:=2+output.bytes
  1380. else
  1381. output.size:=1+output.bytes;
  1382. process_ea:=true;
  1383. end;
  1384. function taicpu.calcsize(p:PInsEntry):shortint;
  1385. var
  1386. codes : pchar;
  1387. c : byte;
  1388. len : shortint;
  1389. ea_data : ea;
  1390. begin
  1391. len:=0;
  1392. codes:=@p^.code;
  1393. repeat
  1394. c:=ord(codes^);
  1395. inc(codes);
  1396. case c of
  1397. 0 :
  1398. break;
  1399. 1,2,3 :
  1400. begin
  1401. inc(codes,c);
  1402. inc(len,c);
  1403. end;
  1404. 8,9,10 :
  1405. begin
  1406. inc(codes);
  1407. inc(len);
  1408. end;
  1409. 4,5,6,7 :
  1410. begin
  1411. if opsize=S_W then
  1412. inc(len,2)
  1413. else
  1414. inc(len);
  1415. end;
  1416. 15,
  1417. 12,13,14,
  1418. 16,17,18,
  1419. 20,21,22,
  1420. 40,41,42 :
  1421. inc(len);
  1422. 24,25,26,
  1423. 31,
  1424. 48,49,50 :
  1425. inc(len,2);
  1426. 28,29,30, { we don't have 16 bit immediates code }
  1427. 32,33,34,
  1428. 52,53,54,
  1429. 56,57,58 :
  1430. inc(len,4);
  1431. 192,193,194 :
  1432. if NeedAddrPrefix(c-192) then
  1433. inc(len);
  1434. 208,
  1435. 210 :
  1436. inc(len);
  1437. 200,
  1438. 201,
  1439. 202,
  1440. 209,
  1441. 211,
  1442. 217,218: ;
  1443. 219,220 :
  1444. inc(len);
  1445. 216 :
  1446. begin
  1447. inc(codes);
  1448. inc(len);
  1449. end;
  1450. 224,225,226 :
  1451. begin
  1452. InternalError(777002);
  1453. end;
  1454. else
  1455. begin
  1456. if (c>=64) and (c<=191) then
  1457. begin
  1458. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1459. Message(asmw_e_invalid_effective_address)
  1460. else
  1461. inc(len,ea_data.size);
  1462. end
  1463. else
  1464. InternalError(777003);
  1465. end;
  1466. end;
  1467. until false;
  1468. calcsize:=len;
  1469. end;
  1470. procedure taicpu.GenCode(objdata:TAsmObjectData);
  1471. {
  1472. * the actual codes (C syntax, i.e. octal):
  1473. * \0 - terminates the code. (Unless it's a literal of course.)
  1474. * \1, \2, \3 - that many literal bytes follow in the code stream
  1475. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1476. * (POP is never used for CS) depending on operand 0
  1477. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1478. * on operand 0
  1479. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1480. * to the register value of operand 0, 1 or 2
  1481. * \17 - encodes the literal byte 0. (Some compilers don't take
  1482. * kindly to a zero byte in the _middle_ of a compile time
  1483. * string constant, so I had to put this hack in.)
  1484. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1485. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1486. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1487. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1488. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1489. * assembly mode or the address-size override on the operand
  1490. * \37 - a word constant, from the _segment_ part of operand 0
  1491. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1492. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1493. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1494. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1495. * assembly mode or the address-size override on the operand
  1496. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1497. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1498. * field the register value of operand b.
  1499. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1500. * field equal to digit b.
  1501. * \30x - might be an 0x67 byte, depending on the address size of
  1502. * the memory reference in operand x.
  1503. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1504. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1505. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1506. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1507. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1508. * \322 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1509. * \323 - indicates that this instruction is only valid when the
  1510. * operand size is the default (instruction to disassembler,
  1511. * generates no code in the assembler)
  1512. * \330 - a literal byte follows in the code stream, to be added
  1513. * to the condition code value of the instruction.
  1514. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1515. * Operand 0 had better be a segmentless constant.
  1516. }
  1517. var
  1518. currval : longint;
  1519. currsym : tasmsymbol;
  1520. procedure getvalsym(opidx:longint);
  1521. begin
  1522. case oper[opidx]^.typ of
  1523. top_ref :
  1524. begin
  1525. currval:=oper[opidx]^.ref^.offset;
  1526. currsym:=oper[opidx]^.ref^.symbol;
  1527. end;
  1528. top_const :
  1529. begin
  1530. currval:=longint(oper[opidx]^.val);
  1531. currsym:=nil;
  1532. end;
  1533. else
  1534. Message(asmw_e_immediate_or_reference_expected);
  1535. end;
  1536. end;
  1537. const
  1538. CondVal:array[TAsmCond] of byte=($0,
  1539. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1540. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1541. $0, $A, $A, $B, $8, $4);
  1542. var
  1543. c : byte;
  1544. pb,
  1545. codes : pchar;
  1546. bytes : array[0..3] of byte;
  1547. rfield,
  1548. data,s,opidx : longint;
  1549. ea_data : ea;
  1550. begin
  1551. {$ifdef EXTDEBUG}
  1552. { safety check }
  1553. if objdata.currsec.datasize<>insoffset then
  1554. internalerror(200130121);
  1555. {$endif EXTDEBUG}
  1556. { load data to write }
  1557. codes:=insentry^.code;
  1558. { Force word push/pop for registers }
  1559. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1560. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1561. begin
  1562. bytes[0]:=$66;
  1563. objdata.writebytes(bytes,1);
  1564. end;
  1565. repeat
  1566. c:=ord(codes^);
  1567. inc(codes);
  1568. case c of
  1569. 0 :
  1570. break;
  1571. 1,2,3 :
  1572. begin
  1573. objdata.writebytes(codes^,c);
  1574. inc(codes,c);
  1575. end;
  1576. 4,6 :
  1577. begin
  1578. case oper[0]^.reg of
  1579. NR_CS:
  1580. bytes[0]:=$e;
  1581. NR_NO,
  1582. NR_DS:
  1583. bytes[0]:=$1e;
  1584. NR_ES:
  1585. bytes[0]:=$6;
  1586. NR_SS:
  1587. bytes[0]:=$16;
  1588. else
  1589. internalerror(777004);
  1590. end;
  1591. if c=4 then
  1592. inc(bytes[0]);
  1593. objdata.writebytes(bytes,1);
  1594. end;
  1595. 5,7 :
  1596. begin
  1597. case oper[0]^.reg of
  1598. NR_FS:
  1599. bytes[0]:=$a0;
  1600. NR_GS:
  1601. bytes[0]:=$a8;
  1602. else
  1603. internalerror(777005);
  1604. end;
  1605. if c=5 then
  1606. inc(bytes[0]);
  1607. objdata.writebytes(bytes,1);
  1608. end;
  1609. 8,9,10 :
  1610. begin
  1611. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1612. inc(codes);
  1613. objdata.writebytes(bytes,1);
  1614. end;
  1615. 15 :
  1616. begin
  1617. bytes[0]:=0;
  1618. objdata.writebytes(bytes,1);
  1619. end;
  1620. 12,13,14 :
  1621. begin
  1622. getvalsym(c-12);
  1623. if (currval<-128) or (currval>127) then
  1624. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1625. if assigned(currsym) then
  1626. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1627. else
  1628. objdata.writebytes(currval,1);
  1629. end;
  1630. 16,17,18 :
  1631. begin
  1632. getvalsym(c-16);
  1633. if (currval<-256) or (currval>255) then
  1634. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1635. if assigned(currsym) then
  1636. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1637. else
  1638. objdata.writebytes(currval,1);
  1639. end;
  1640. 20,21,22 :
  1641. begin
  1642. getvalsym(c-20);
  1643. if (currval<0) or (currval>255) then
  1644. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1645. if assigned(currsym) then
  1646. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1647. else
  1648. objdata.writebytes(currval,1);
  1649. end;
  1650. 24,25,26 :
  1651. begin
  1652. getvalsym(c-24);
  1653. if (currval<-65536) or (currval>65535) then
  1654. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1655. if assigned(currsym) then
  1656. objdata.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1657. else
  1658. objdata.writebytes(currval,2);
  1659. end;
  1660. 28,29,30 :
  1661. begin
  1662. getvalsym(c-28);
  1663. if assigned(currsym) then
  1664. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1665. else
  1666. objdata.writebytes(currval,4);
  1667. end;
  1668. 32,33,34 :
  1669. begin
  1670. getvalsym(c-32);
  1671. if assigned(currsym) then
  1672. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1673. else
  1674. objdata.writebytes(currval,4);
  1675. end;
  1676. 40,41,42 :
  1677. begin
  1678. getvalsym(c-40);
  1679. data:=currval-insend;
  1680. if assigned(currsym) then
  1681. inc(data,currsym.address);
  1682. if (data>127) or (data<-128) then
  1683. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1684. objdata.writebytes(data,1);
  1685. end;
  1686. 52,53,54 :
  1687. begin
  1688. getvalsym(c-52);
  1689. if assigned(currsym) then
  1690. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1691. else
  1692. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1693. end;
  1694. 56,57,58 :
  1695. begin
  1696. getvalsym(c-56);
  1697. if assigned(currsym) then
  1698. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1699. else
  1700. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1701. end;
  1702. 192,193,194 :
  1703. begin
  1704. if NeedAddrPrefix(c-192) then
  1705. begin
  1706. bytes[0]:=$67;
  1707. objdata.writebytes(bytes,1);
  1708. end;
  1709. end;
  1710. 200 :
  1711. begin
  1712. bytes[0]:=$67;
  1713. objdata.writebytes(bytes,1);
  1714. end;
  1715. 208 :
  1716. begin
  1717. bytes[0]:=$66;
  1718. objdata.writebytes(bytes,1);
  1719. end;
  1720. 210 :
  1721. begin
  1722. bytes[0]:=$48;
  1723. objdata.writebytes(bytes,1);
  1724. end;
  1725. 216 :
  1726. begin
  1727. bytes[0]:=ord(codes^)+condval[condition];
  1728. inc(codes);
  1729. objdata.writebytes(bytes,1);
  1730. end;
  1731. 201,
  1732. 202,
  1733. 209,
  1734. 211,
  1735. 217,218 :
  1736. begin
  1737. { these are dissambler hints or 32 bit prefixes which
  1738. are not needed }
  1739. end;
  1740. 219 :
  1741. begin
  1742. bytes[0]:=$f3;
  1743. objdata.writebytes(bytes,1);
  1744. end;
  1745. 220 :
  1746. begin
  1747. bytes[0]:=$f2;
  1748. objdata.writebytes(bytes,1);
  1749. end;
  1750. 31,
  1751. 48,49,50,
  1752. 224,225,226 :
  1753. begin
  1754. InternalError(777006);
  1755. end
  1756. else
  1757. begin
  1758. if (c>=64) and (c<=191) then
  1759. begin
  1760. if (c<127) then
  1761. begin
  1762. if (oper[c and 7]^.typ=top_reg) then
  1763. rfield:=regval(oper[c and 7]^.reg)
  1764. else
  1765. rfield:=regval(oper[c and 7]^.ref^.base);
  1766. end
  1767. else
  1768. rfield:=c and 7;
  1769. opidx:=(c shr 3) and 7;
  1770. if not process_ea(oper[opidx]^,ea_data,rfield) then
  1771. Message(asmw_e_invalid_effective_address);
  1772. pb:=@bytes;
  1773. pb^:=chr(ea_data.modrm);
  1774. inc(pb);
  1775. if ea_data.sib_present then
  1776. begin
  1777. pb^:=chr(ea_data.sib);
  1778. inc(pb);
  1779. end;
  1780. s:=pb-pchar(@bytes);
  1781. objdata.writebytes(bytes,s);
  1782. case ea_data.bytes of
  1783. 0 : ;
  1784. 1 :
  1785. begin
  1786. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  1787. objdata.writereloc(oper[opidx]^.ref^.offset,1,oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE)
  1788. else
  1789. begin
  1790. bytes[0]:=oper[opidx]^.ref^.offset;
  1791. objdata.writebytes(bytes,1);
  1792. end;
  1793. inc(s);
  1794. end;
  1795. 2,4 :
  1796. begin
  1797. objdata.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,
  1798. oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE);
  1799. inc(s,ea_data.bytes);
  1800. end;
  1801. end;
  1802. end
  1803. else
  1804. InternalError(777007);
  1805. end;
  1806. end;
  1807. until false;
  1808. end;
  1809. {$endif NOAG386BIN}
  1810. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1811. begin
  1812. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  1813. (regtype = R_INTREGISTER) and
  1814. (ops=2) and
  1815. (oper[0]^.typ=top_reg) and
  1816. (oper[1]^.typ=top_reg) and
  1817. (oper[0]^.reg=oper[1]^.reg)
  1818. ) or
  1819. (((opcode=A_MOVSS) or (opcode=A_MOVSD)) and
  1820. (regtype = R_MMREGISTER) and
  1821. (ops=2) and
  1822. (oper[0]^.typ=top_reg) and
  1823. (oper[1]^.typ=top_reg) and
  1824. (oper[0]^.reg=oper[1]^.reg)
  1825. );
  1826. end;
  1827. procedure build_spilling_operation_type_table;
  1828. var
  1829. opcode : tasmop;
  1830. i : integer;
  1831. begin
  1832. new(operation_type_table);
  1833. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  1834. for opcode:=low(tasmop) to high(tasmop) do
  1835. begin
  1836. for i:=1 to MaxInsChanges do
  1837. begin
  1838. case InsProp[opcode].Ch[i] of
  1839. Ch_Rop1 :
  1840. operation_type_table^[opcode,0]:=operand_read;
  1841. Ch_Wop1 :
  1842. operation_type_table^[opcode,0]:=operand_write;
  1843. Ch_RWop1,
  1844. Ch_Mop1 :
  1845. operation_type_table^[opcode,0]:=operand_readwrite;
  1846. Ch_Rop2 :
  1847. operation_type_table^[opcode,1]:=operand_read;
  1848. Ch_Wop2 :
  1849. operation_type_table^[opcode,1]:=operand_write;
  1850. Ch_RWop2,
  1851. Ch_Mop2 :
  1852. operation_type_table^[opcode,1]:=operand_readwrite;
  1853. Ch_Rop3 :
  1854. operation_type_table^[opcode,2]:=operand_read;
  1855. Ch_Wop3 :
  1856. operation_type_table^[opcode,2]:=operand_write;
  1857. Ch_RWop3,
  1858. Ch_Mop3 :
  1859. operation_type_table^[opcode,2]:=operand_readwrite;
  1860. end;
  1861. end;
  1862. end;
  1863. { Special cases that can't be decoded from the InsChanges flags }
  1864. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  1865. end;
  1866. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  1867. begin
  1868. { the information in the instruction table is made for the string copy
  1869. operation MOVSD so hack here (FK)
  1870. }
  1871. if (opcode=A_MOVSD) and (ops=2) then
  1872. begin
  1873. case opnr of
  1874. 0:
  1875. result:=operand_read;
  1876. 1:
  1877. result:=operand_write;
  1878. else
  1879. internalerror(200506055);
  1880. end
  1881. end
  1882. else
  1883. result:=operation_type_table^[opcode,opnr];
  1884. end;
  1885. function spilling_create_load(const ref:treference;r:tregister): tai;
  1886. begin
  1887. case getregtype(r) of
  1888. R_INTREGISTER :
  1889. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
  1890. R_MMREGISTER :
  1891. case getsubreg(r) of
  1892. R_SUBMMD:
  1893. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  1894. R_SUBMMS:
  1895. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  1896. else
  1897. internalerror(200506043);
  1898. end;
  1899. else
  1900. internalerror(200401041);
  1901. end;
  1902. end;
  1903. function spilling_create_store(r:tregister; const ref:treference): tai;
  1904. begin
  1905. case getregtype(r) of
  1906. R_INTREGISTER :
  1907. result:=taicpu.op_reg_ref(A_MOV,reg2opsize(r),r,ref);
  1908. R_MMREGISTER :
  1909. case getsubreg(r) of
  1910. R_SUBMMD:
  1911. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  1912. R_SUBMMS:
  1913. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  1914. else
  1915. internalerror(200506042);
  1916. end;
  1917. else
  1918. internalerror(200401041);
  1919. end;
  1920. end;
  1921. {*****************************************************************************
  1922. Instruction table
  1923. *****************************************************************************}
  1924. procedure BuildInsTabCache;
  1925. {$ifndef NOAG386BIN}
  1926. var
  1927. i : longint;
  1928. {$endif}
  1929. begin
  1930. {$ifndef NOAG386BIN}
  1931. new(instabcache);
  1932. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1933. i:=0;
  1934. while (i<InsTabEntries) do
  1935. begin
  1936. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1937. InsTabCache^[InsTab[i].OPcode]:=i;
  1938. inc(i);
  1939. end;
  1940. {$endif NOAG386BIN}
  1941. end;
  1942. procedure InitAsm;
  1943. begin
  1944. build_spilling_operation_type_table;
  1945. {$ifndef NOAG386BIN}
  1946. if not assigned(instabcache) then
  1947. BuildInsTabCache;
  1948. {$endif NOAG386BIN}
  1949. end;
  1950. procedure DoneAsm;
  1951. begin
  1952. if assigned(operation_type_table) then
  1953. begin
  1954. dispose(operation_type_table);
  1955. operation_type_table:=nil;
  1956. end;
  1957. {$ifndef NOAG386BIN}
  1958. if assigned(instabcache) then
  1959. begin
  1960. dispose(instabcache);
  1961. instabcache:=nil;
  1962. end;
  1963. {$endif NOAG386BIN}
  1964. end;
  1965. begin
  1966. cai_align:=tai_align;
  1967. cai_cpu:=taicpu;
  1968. end.