aasmcpu.pas 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120
  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. ),
  317. (OT_NONE,
  318. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  319. OT_BITS16,OT_BITS32,OT_BITS64,
  320. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  321. OT_BITS64,
  322. OT_NEAR,OT_FAR,OT_SHORT,
  323. OT_NONE
  324. ),
  325. (OT_NONE,
  326. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  327. OT_BITS16,OT_BITS32,OT_BITS64,
  328. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  329. OT_BITS64,
  330. OT_NEAR,OT_FAR,OT_SHORT,
  331. OT_NONE
  332. )
  333. );
  334. reg_ot_table : array[tregisterindex] of longint = (
  335. {$i r8664ot.inc}
  336. );
  337. {$else x86_64}
  338. { Intel style operands ! }
  339. opsize_2_type:array[0..2,topsize] of longint=(
  340. (OT_NONE,
  341. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  342. OT_BITS16,OT_BITS32,OT_BITS64,
  343. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  344. OT_BITS64,
  345. OT_NEAR,OT_FAR,OT_SHORT,
  346. OT_NONE
  347. ),
  348. (OT_NONE,
  349. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  350. OT_BITS16,OT_BITS32,OT_BITS64,
  351. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  352. OT_BITS64,
  353. OT_NEAR,OT_FAR,OT_SHORT,
  354. OT_NONE
  355. ),
  356. (OT_NONE,
  357. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  358. OT_BITS16,OT_BITS32,OT_BITS64,
  359. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  360. OT_BITS64,
  361. OT_NEAR,OT_FAR,OT_SHORT,
  362. OT_NONE
  363. )
  364. );
  365. reg_ot_table : array[tregisterindex] of longint = (
  366. {$i r386ot.inc}
  367. );
  368. {$endif x86_64}
  369. { Operation type for spilling code }
  370. type
  371. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  372. var
  373. operation_type_table : ^toperation_type_table;
  374. {****************************************************************************
  375. TAI_ALIGN
  376. ****************************************************************************}
  377. constructor tai_align.create(b: byte);
  378. begin
  379. inherited create(b);
  380. reg:=NR_ECX;
  381. end;
  382. constructor tai_align.create_op(b: byte; _op: byte);
  383. begin
  384. inherited create_op(b,_op);
  385. reg:=NR_NO;
  386. end;
  387. function tai_align.calculatefillbuf(var buf : tfillbuffer):pchar;
  388. const
  389. alignarray:array[0..5] of string[8]=(
  390. #$8D#$B4#$26#$00#$00#$00#$00,
  391. #$8D#$B6#$00#$00#$00#$00,
  392. #$8D#$74#$26#$00,
  393. #$8D#$76#$00,
  394. #$89#$F6,
  395. #$90
  396. );
  397. var
  398. bufptr : pchar;
  399. j : longint;
  400. begin
  401. inherited calculatefillbuf(buf);
  402. if not use_op then
  403. begin
  404. bufptr:=pchar(@buf);
  405. while (fillsize>0) do
  406. begin
  407. for j:=0 to 5 do
  408. if (fillsize>=length(alignarray[j])) then
  409. break;
  410. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  411. inc(bufptr,length(alignarray[j]));
  412. dec(fillsize,length(alignarray[j]));
  413. end;
  414. end;
  415. calculatefillbuf:=pchar(@buf);
  416. end;
  417. {*****************************************************************************
  418. Taicpu Constructors
  419. *****************************************************************************}
  420. procedure taicpu.changeopsize(siz:topsize);
  421. begin
  422. opsize:=siz;
  423. end;
  424. procedure taicpu.init(_size : topsize);
  425. begin
  426. { default order is att }
  427. FOperandOrder:=op_att;
  428. segprefix:=NR_NO;
  429. opsize:=_size;
  430. {$ifndef NOAG386BIN}
  431. insentry:=nil;
  432. LastInsOffset:=-1;
  433. InsOffset:=0;
  434. InsSize:=0;
  435. {$endif}
  436. end;
  437. constructor taicpu.op_none(op : tasmop);
  438. begin
  439. inherited create(op);
  440. init(S_NO);
  441. end;
  442. constructor taicpu.op_none(op : tasmop;_size : topsize);
  443. begin
  444. inherited create(op);
  445. init(_size);
  446. end;
  447. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  448. begin
  449. inherited create(op);
  450. init(_size);
  451. ops:=1;
  452. loadreg(0,_op1);
  453. end;
  454. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  455. begin
  456. inherited create(op);
  457. init(_size);
  458. ops:=1;
  459. loadconst(0,_op1);
  460. end;
  461. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  462. begin
  463. inherited create(op);
  464. init(_size);
  465. ops:=1;
  466. loadref(0,_op1);
  467. end;
  468. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  469. begin
  470. inherited create(op);
  471. init(_size);
  472. ops:=2;
  473. loadreg(0,_op1);
  474. loadreg(1,_op2);
  475. end;
  476. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  477. begin
  478. inherited create(op);
  479. init(_size);
  480. ops:=2;
  481. loadreg(0,_op1);
  482. loadconst(1,_op2);
  483. end;
  484. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  485. begin
  486. inherited create(op);
  487. init(_size);
  488. ops:=2;
  489. loadreg(0,_op1);
  490. loadref(1,_op2);
  491. end;
  492. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  493. begin
  494. inherited create(op);
  495. init(_size);
  496. ops:=2;
  497. loadconst(0,_op1);
  498. loadreg(1,_op2);
  499. end;
  500. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  501. begin
  502. inherited create(op);
  503. init(_size);
  504. ops:=2;
  505. loadconst(0,_op1);
  506. loadconst(1,_op2);
  507. end;
  508. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  509. begin
  510. inherited create(op);
  511. init(_size);
  512. ops:=2;
  513. loadconst(0,_op1);
  514. loadref(1,_op2);
  515. end;
  516. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  517. begin
  518. inherited create(op);
  519. init(_size);
  520. ops:=2;
  521. loadref(0,_op1);
  522. loadreg(1,_op2);
  523. end;
  524. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  525. begin
  526. inherited create(op);
  527. init(_size);
  528. ops:=3;
  529. loadreg(0,_op1);
  530. loadreg(1,_op2);
  531. loadreg(2,_op3);
  532. end;
  533. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  534. begin
  535. inherited create(op);
  536. init(_size);
  537. ops:=3;
  538. loadconst(0,_op1);
  539. loadreg(1,_op2);
  540. loadreg(2,_op3);
  541. end;
  542. constructor taicpu.op_reg_reg_ref(op : tasmop;_size : topsize;_op1,_op2 : tregister;const _op3 : treference);
  543. begin
  544. inherited create(op);
  545. init(_size);
  546. ops:=3;
  547. loadreg(0,_op1);
  548. loadreg(1,_op2);
  549. loadref(2,_op3);
  550. end;
  551. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  552. begin
  553. inherited create(op);
  554. init(_size);
  555. ops:=3;
  556. loadconst(0,_op1);
  557. loadref(1,_op2);
  558. loadreg(2,_op3);
  559. end;
  560. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  561. begin
  562. inherited create(op);
  563. init(_size);
  564. ops:=3;
  565. loadconst(0,_op1);
  566. loadreg(1,_op2);
  567. loadref(2,_op3);
  568. end;
  569. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  570. begin
  571. inherited create(op);
  572. init(_size);
  573. condition:=cond;
  574. ops:=1;
  575. loadsymbol(0,_op1,0);
  576. end;
  577. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  578. begin
  579. inherited create(op);
  580. init(_size);
  581. ops:=1;
  582. loadsymbol(0,_op1,0);
  583. end;
  584. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  585. begin
  586. inherited create(op);
  587. init(_size);
  588. ops:=1;
  589. loadsymbol(0,_op1,_op1ofs);
  590. end;
  591. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  592. begin
  593. inherited create(op);
  594. init(_size);
  595. ops:=2;
  596. loadsymbol(0,_op1,_op1ofs);
  597. loadreg(1,_op2);
  598. end;
  599. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  600. begin
  601. inherited create(op);
  602. init(_size);
  603. ops:=2;
  604. loadsymbol(0,_op1,_op1ofs);
  605. loadref(1,_op2);
  606. end;
  607. function taicpu.GetString:string;
  608. var
  609. i : longint;
  610. s : string;
  611. addsize : boolean;
  612. begin
  613. s:='['+std_op2str[opcode];
  614. for i:=0 to ops-1 do
  615. begin
  616. with oper[i]^ do
  617. begin
  618. if i=0 then
  619. s:=s+' '
  620. else
  621. s:=s+',';
  622. { type }
  623. addsize:=false;
  624. if (ot and OT_XMMREG)=OT_XMMREG then
  625. s:=s+'xmmreg'
  626. else
  627. if (ot and OT_MMXREG)=OT_MMXREG then
  628. s:=s+'mmxreg'
  629. else
  630. if (ot and OT_FPUREG)=OT_FPUREG then
  631. s:=s+'fpureg'
  632. else
  633. if (ot and OT_REGISTER)=OT_REGISTER then
  634. begin
  635. s:=s+'reg';
  636. addsize:=true;
  637. end
  638. else
  639. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  640. begin
  641. s:=s+'imm';
  642. addsize:=true;
  643. end
  644. else
  645. if (ot and OT_MEMORY)=OT_MEMORY then
  646. begin
  647. s:=s+'mem';
  648. addsize:=true;
  649. end
  650. else
  651. s:=s+'???';
  652. { size }
  653. if addsize then
  654. begin
  655. if (ot and OT_BITS8)<>0 then
  656. s:=s+'8'
  657. else
  658. if (ot and OT_BITS16)<>0 then
  659. s:=s+'16'
  660. else
  661. if (ot and OT_BITS32)<>0 then
  662. s:=s+'32'
  663. else
  664. s:=s+'??';
  665. { signed }
  666. if (ot and OT_SIGNED)<>0 then
  667. s:=s+'s';
  668. end;
  669. end;
  670. end;
  671. GetString:=s+']';
  672. end;
  673. procedure taicpu.Swapoperands;
  674. var
  675. p : POper;
  676. begin
  677. { Fix the operands which are in AT&T style and we need them in Intel style }
  678. case ops of
  679. 2 : begin
  680. { 0,1 -> 1,0 }
  681. p:=oper[0];
  682. oper[0]:=oper[1];
  683. oper[1]:=p;
  684. end;
  685. 3 : begin
  686. { 0,1,2 -> 2,1,0 }
  687. p:=oper[0];
  688. oper[0]:=oper[2];
  689. oper[2]:=p;
  690. end;
  691. end;
  692. end;
  693. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  694. begin
  695. if FOperandOrder<>order then
  696. begin
  697. Swapoperands;
  698. FOperandOrder:=order;
  699. end;
  700. end;
  701. procedure taicpu.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  702. begin
  703. o.typ:=toptype(ppufile.getbyte);
  704. o.ot:=ppufile.getlongint;
  705. case o.typ of
  706. top_reg :
  707. ppufile.getdata(o.reg,sizeof(Tregister));
  708. top_ref :
  709. begin
  710. new(o.ref);
  711. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  712. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  713. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  714. o.ref^.scalefactor:=ppufile.getbyte;
  715. o.ref^.offset:=ppufile.getaint;
  716. o.ref^.symbol:=ppufile.getasmsymbol;
  717. o.ref^.relsymbol:=ppufile.getasmsymbol;
  718. end;
  719. top_const :
  720. o.val:=ppufile.getaint;
  721. top_local :
  722. begin
  723. new(o.localoper);
  724. with o.localoper^ do
  725. begin
  726. ppufile.getderef(localsymderef);
  727. localsymofs:=ppufile.getaint;
  728. localindexreg:=tregister(ppufile.getlongint);
  729. localscale:=ppufile.getbyte;
  730. localgetoffset:=(ppufile.getbyte<>0);
  731. end;
  732. end;
  733. end;
  734. end;
  735. procedure taicpu.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  736. begin
  737. ppufile.putbyte(byte(o.typ));
  738. ppufile.putlongint(o.ot);
  739. case o.typ of
  740. top_reg :
  741. ppufile.putdata(o.reg,sizeof(Tregister));
  742. top_ref :
  743. begin
  744. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  745. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  746. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  747. ppufile.putbyte(o.ref^.scalefactor);
  748. ppufile.putaint(o.ref^.offset);
  749. ppufile.putasmsymbol(o.ref^.symbol);
  750. ppufile.putasmsymbol(o.ref^.relsymbol);
  751. end;
  752. top_const :
  753. ppufile.putaint(o.val);
  754. top_local :
  755. begin
  756. with o.localoper^ do
  757. begin
  758. ppufile.putderef(localsymderef);
  759. ppufile.putaint(localsymofs);
  760. ppufile.putlongint(longint(localindexreg));
  761. ppufile.putbyte(localscale);
  762. ppufile.putbyte(byte(localgetoffset));
  763. end;
  764. end;
  765. end;
  766. end;
  767. procedure taicpu.ppubuildderefimploper(var o:toper);
  768. begin
  769. case o.typ of
  770. top_local :
  771. o.localoper^.localsymderef.build(tlocalvarsym(o.localoper^.localsym));
  772. end;
  773. end;
  774. procedure taicpu.ppuderefoper(var o:toper);
  775. begin
  776. case o.typ of
  777. top_ref :
  778. begin
  779. if assigned(o.ref^.symbol) then
  780. objectlibrary.derefasmsymbol(o.ref^.symbol);
  781. if assigned(o.ref^.relsymbol) then
  782. objectlibrary.derefasmsymbol(o.ref^.relsymbol);
  783. end;
  784. top_local :
  785. o.localoper^.localsym:=tlocalvarsym(o.localoper^.localsymderef.resolve);
  786. end;
  787. end;
  788. procedure taicpu.CheckNonCommutativeOpcodes;
  789. begin
  790. { we need ATT order }
  791. SetOperandOrder(op_att);
  792. if (
  793. (ops=2) and
  794. (oper[0]^.typ=top_reg) and
  795. (oper[1]^.typ=top_reg) and
  796. { if the first is ST and the second is also a register
  797. it is necessarily ST1 .. ST7 }
  798. ((oper[0]^.reg=NR_ST) or
  799. (oper[0]^.reg=NR_ST0))
  800. ) or
  801. { ((ops=1) and
  802. (oper[0]^.typ=top_reg) and
  803. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  804. (ops=0) then
  805. begin
  806. if opcode=A_FSUBR then
  807. opcode:=A_FSUB
  808. else if opcode=A_FSUB then
  809. opcode:=A_FSUBR
  810. else if opcode=A_FDIVR then
  811. opcode:=A_FDIV
  812. else if opcode=A_FDIV then
  813. opcode:=A_FDIVR
  814. else if opcode=A_FSUBRP then
  815. opcode:=A_FSUBP
  816. else if opcode=A_FSUBP then
  817. opcode:=A_FSUBRP
  818. else if opcode=A_FDIVRP then
  819. opcode:=A_FDIVP
  820. else if opcode=A_FDIVP then
  821. opcode:=A_FDIVRP;
  822. end;
  823. if (
  824. (ops=1) and
  825. (oper[0]^.typ=top_reg) and
  826. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  827. (oper[0]^.reg<>NR_ST)
  828. ) then
  829. begin
  830. if opcode=A_FSUBRP then
  831. opcode:=A_FSUBP
  832. else if opcode=A_FSUBP then
  833. opcode:=A_FSUBRP
  834. else if opcode=A_FDIVRP then
  835. opcode:=A_FDIVP
  836. else if opcode=A_FDIVP then
  837. opcode:=A_FDIVRP;
  838. end;
  839. end;
  840. {*****************************************************************************
  841. Assembler
  842. *****************************************************************************}
  843. {$ifndef NOAG386BIN}
  844. type
  845. ea=packed record
  846. sib_present : boolean;
  847. bytes : byte;
  848. size : byte;
  849. modrm : byte;
  850. sib : byte;
  851. end;
  852. procedure taicpu.create_ot;
  853. {
  854. this function will also fix some other fields which only needs to be once
  855. }
  856. var
  857. i,l,relsize : longint;
  858. begin
  859. if ops=0 then
  860. exit;
  861. { update oper[].ot field }
  862. for i:=0 to ops-1 do
  863. with oper[i]^ do
  864. begin
  865. case typ of
  866. top_reg :
  867. begin
  868. ot:=reg_ot_table[findreg_by_number(reg)];
  869. end;
  870. top_ref :
  871. begin
  872. if ref^.refaddr=addr_no then
  873. begin
  874. { create ot field }
  875. if (ot and OT_SIZE_MASK)=0 then
  876. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  877. else
  878. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  879. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  880. ot:=ot or OT_MEM_OFFS;
  881. { fix scalefactor }
  882. if (ref^.index=NR_NO) then
  883. ref^.scalefactor:=0
  884. else
  885. if (ref^.scalefactor=0) then
  886. ref^.scalefactor:=1;
  887. end
  888. else
  889. begin
  890. l:=ref^.offset;
  891. if assigned(ref^.symbol) then
  892. inc(l,ref^.symbol.address);
  893. { when it is a forward jump we need to compensate the
  894. offset of the instruction since the previous time,
  895. because the symbol address is then still using the
  896. 'old-style' addressing.
  897. For backwards jumps this is not required because the
  898. address of the symbol is already adjusted to the
  899. new offset }
  900. if (l>InsOffset) and (LastInsOffset<>-1) then
  901. inc(l,InsOffset-LastInsOffset);
  902. { instruction size will then always become 2 (PFV) }
  903. relsize:=(InsOffset+2)-l;
  904. if (not assigned(ref^.symbol) or
  905. ((ref^.symbol.currbind<>AB_EXTERNAL) and (ref^.symbol.address<>0))) and
  906. (relsize>=-128) and (relsize<=127) then
  907. ot:=OT_IMM32 or OT_SHORT
  908. else
  909. ot:=OT_IMM32 or OT_NEAR;
  910. end;
  911. end;
  912. top_local :
  913. begin
  914. if (ot and OT_SIZE_MASK)=0 then
  915. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  916. else
  917. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  918. end;
  919. top_const :
  920. begin
  921. if opsize=S_NO then
  922. message(asmr_e_invalid_opcode_and_operand);
  923. if (opsize<>S_W) and (longint(val)>=-128) and (val<=127) then
  924. ot:=OT_IMM8 or OT_SIGNED
  925. else
  926. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  927. end;
  928. top_none :
  929. begin
  930. { generated when there was an error in the
  931. assembler reader. It never happends when generating
  932. assembler }
  933. end;
  934. else
  935. internalerror(200402261);
  936. end;
  937. end;
  938. end;
  939. function taicpu.InsEnd:longint;
  940. begin
  941. InsEnd:=InsOffset+InsSize;
  942. end;
  943. function taicpu.Matches(p:PInsEntry):longint;
  944. { * IF_SM stands for Size Match: any operand whose size is not
  945. * explicitly specified by the template is `really' intended to be
  946. * the same size as the first size-specified operand.
  947. * Non-specification is tolerated in the input instruction, but
  948. * _wrong_ specification is not.
  949. *
  950. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  951. * three-operand instructions such as SHLD: it implies that the
  952. * first two operands must match in size, but that the third is
  953. * required to be _unspecified_.
  954. *
  955. * IF_SB invokes Size Byte: operands with unspecified size in the
  956. * template are really bytes, and so no non-byte specification in
  957. * the input instruction will be tolerated. IF_SW similarly invokes
  958. * Size Word, and IF_SD invokes Size Doubleword.
  959. *
  960. * (The default state if neither IF_SM nor IF_SM2 is specified is
  961. * that any operand with unspecified size in the template is
  962. * required to have unspecified size in the instruction too...)
  963. }
  964. var
  965. i,j,asize,oprs : longint;
  966. siz : array[0..2] of longint;
  967. begin
  968. Matches:=100;
  969. { Check the opcode and operands }
  970. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  971. begin
  972. Matches:=0;
  973. exit;
  974. end;
  975. { Check that no spurious colons or TOs are present }
  976. for i:=0 to p^.ops-1 do
  977. if (oper[i]^.ot and (not p^.optypes[i]) and (OT_COLON or OT_TO))<>0 then
  978. begin
  979. Matches:=0;
  980. exit;
  981. end;
  982. { Check that the operand flags all match up }
  983. for i:=0 to p^.ops-1 do
  984. begin
  985. if ((p^.optypes[i] and (not oper[i]^.ot)) or
  986. ((p^.optypes[i] and OT_SIZE_MASK) and
  987. ((p^.optypes[i] xor oper[i]^.ot) and OT_SIZE_MASK)))<>0 then
  988. begin
  989. if ((p^.optypes[i] and (not oper[i]^.ot) and OT_NON_SIZE) or
  990. (oper[i]^.ot and OT_SIZE_MASK))<>0 then
  991. begin
  992. Matches:=0;
  993. exit;
  994. end
  995. else
  996. Matches:=1;
  997. end;
  998. end;
  999. { Check operand sizes }
  1000. { as default an untyped size can get all the sizes, this is different
  1001. from nasm, but else we need to do a lot checking which opcodes want
  1002. size or not with the automatic size generation }
  1003. asize:=longint($ffffffff);
  1004. if (p^.flags and IF_SB)<>0 then
  1005. asize:=OT_BITS8
  1006. else if (p^.flags and IF_SW)<>0 then
  1007. asize:=OT_BITS16
  1008. else if (p^.flags and IF_SD)<>0 then
  1009. asize:=OT_BITS32;
  1010. if (p^.flags and IF_ARMASK)<>0 then
  1011. begin
  1012. siz[0]:=0;
  1013. siz[1]:=0;
  1014. siz[2]:=0;
  1015. if (p^.flags and IF_AR0)<>0 then
  1016. siz[0]:=asize
  1017. else if (p^.flags and IF_AR1)<>0 then
  1018. siz[1]:=asize
  1019. else if (p^.flags and IF_AR2)<>0 then
  1020. siz[2]:=asize;
  1021. end
  1022. else
  1023. begin
  1024. { we can leave because the size for all operands is forced to be
  1025. the same
  1026. but not if IF_SB IF_SW or IF_SD is set PM }
  1027. if asize=-1 then
  1028. exit;
  1029. siz[0]:=asize;
  1030. siz[1]:=asize;
  1031. siz[2]:=asize;
  1032. end;
  1033. if (p^.flags and (IF_SM or IF_SM2))<>0 then
  1034. begin
  1035. if (p^.flags and IF_SM2)<>0 then
  1036. oprs:=2
  1037. else
  1038. oprs:=p^.ops;
  1039. for i:=0 to oprs-1 do
  1040. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1041. begin
  1042. for j:=0 to oprs-1 do
  1043. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1044. break;
  1045. end;
  1046. end
  1047. else
  1048. oprs:=2;
  1049. { Check operand sizes }
  1050. for i:=0 to p^.ops-1 do
  1051. begin
  1052. if ((p^.optypes[i] and OT_SIZE_MASK)=0) and
  1053. ((oper[i]^.ot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1054. { Immediates can always include smaller size }
  1055. ((oper[i]^.ot and OT_IMMEDIATE)=0) and
  1056. (((p^.optypes[i] and OT_SIZE_MASK) or siz[i])<(oper[i]^.ot and OT_SIZE_MASK)) then
  1057. Matches:=2;
  1058. end;
  1059. end;
  1060. procedure taicpu.ResetPass1;
  1061. begin
  1062. { we need to reset everything here, because the choosen insentry
  1063. can be invalid for a new situation where the previously optimized
  1064. insentry is not correct }
  1065. InsEntry:=nil;
  1066. InsSize:=0;
  1067. LastInsOffset:=-1;
  1068. end;
  1069. procedure taicpu.ResetPass2;
  1070. begin
  1071. { we are here in a second pass, check if the instruction can be optimized }
  1072. if assigned(InsEntry) and
  1073. ((InsEntry^.flags and IF_PASS2)<>0) then
  1074. begin
  1075. InsEntry:=nil;
  1076. InsSize:=0;
  1077. end;
  1078. LastInsOffset:=-1;
  1079. end;
  1080. function taicpu.CheckIfValid:boolean;
  1081. begin
  1082. result:=FindInsEntry;
  1083. end;
  1084. function taicpu.FindInsentry:boolean;
  1085. var
  1086. i : longint;
  1087. begin
  1088. result:=false;
  1089. { Things which may only be done once, not when a second pass is done to
  1090. optimize }
  1091. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1092. begin
  1093. { We need intel style operands }
  1094. SetOperandOrder(op_intel);
  1095. { create the .ot fields }
  1096. create_ot;
  1097. { set the file postion }
  1098. aktfilepos:=fileinfo;
  1099. end
  1100. else
  1101. begin
  1102. { we've already an insentry so it's valid }
  1103. result:=true;
  1104. exit;
  1105. end;
  1106. { Lookup opcode in the table }
  1107. InsSize:=-1;
  1108. i:=instabcache^[opcode];
  1109. if i=-1 then
  1110. begin
  1111. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1112. exit;
  1113. end;
  1114. insentry:=@instab[i];
  1115. while (insentry^.opcode=opcode) do
  1116. begin
  1117. if matches(insentry)=100 then
  1118. begin
  1119. result:=true;
  1120. exit;
  1121. end;
  1122. inc(i);
  1123. insentry:=@instab[i];
  1124. end;
  1125. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1126. { No instruction found, set insentry to nil and inssize to -1 }
  1127. insentry:=nil;
  1128. inssize:=-1;
  1129. end;
  1130. function taicpu.Pass1(offset:longint):longint;
  1131. begin
  1132. Pass1:=0;
  1133. { Save the old offset and set the new offset }
  1134. InsOffset:=Offset;
  1135. { Error? }
  1136. if (Insentry=nil) and (InsSize=-1) then
  1137. exit;
  1138. { set the file postion }
  1139. aktfilepos:=fileinfo;
  1140. { Get InsEntry }
  1141. if FindInsEntry then
  1142. begin
  1143. { Calculate instruction size }
  1144. InsSize:=calcsize(insentry);
  1145. if segprefix<>NR_NO then
  1146. inc(InsSize);
  1147. { Fix opsize if size if forced }
  1148. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1149. begin
  1150. if (insentry^.flags and IF_ARMASK)=0 then
  1151. begin
  1152. if (insentry^.flags and IF_SB)<>0 then
  1153. begin
  1154. if opsize=S_NO then
  1155. opsize:=S_B;
  1156. end
  1157. else if (insentry^.flags and IF_SW)<>0 then
  1158. begin
  1159. if opsize=S_NO then
  1160. opsize:=S_W;
  1161. end
  1162. else if (insentry^.flags and IF_SD)<>0 then
  1163. begin
  1164. if opsize=S_NO then
  1165. opsize:=S_L;
  1166. end;
  1167. end;
  1168. end;
  1169. LastInsOffset:=InsOffset;
  1170. Pass1:=InsSize;
  1171. exit;
  1172. end;
  1173. LastInsOffset:=-1;
  1174. end;
  1175. procedure taicpu.Pass2(objdata:TAsmObjectData);
  1176. var
  1177. c : longint;
  1178. begin
  1179. { error in pass1 ? }
  1180. if insentry=nil then
  1181. exit;
  1182. aktfilepos:=fileinfo;
  1183. { Segment override }
  1184. if (segprefix<>NR_NO) then
  1185. begin
  1186. case segprefix of
  1187. NR_CS : c:=$2e;
  1188. NR_DS : c:=$3e;
  1189. NR_ES : c:=$26;
  1190. NR_FS : c:=$64;
  1191. NR_GS : c:=$65;
  1192. NR_SS : c:=$36;
  1193. end;
  1194. objdata.writebytes(c,1);
  1195. { fix the offset for GenNode }
  1196. inc(InsOffset);
  1197. end;
  1198. { Generate the instruction }
  1199. GenCode(objdata);
  1200. end;
  1201. function taicpu.needaddrprefix(opidx:byte):boolean;
  1202. begin
  1203. result:=(oper[opidx]^.typ=top_ref) and
  1204. (oper[opidx]^.ref^.refaddr=addr_no) and
  1205. (
  1206. (
  1207. (oper[opidx]^.ref^.index<>NR_NO) and
  1208. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBD)
  1209. ) or
  1210. (
  1211. (oper[opidx]^.ref^.base<>NR_NO) and
  1212. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBD)
  1213. )
  1214. );
  1215. end;
  1216. function regval(r:Tregister):byte;
  1217. const
  1218. {$ifdef x86_64}
  1219. opcode_table:array[tregisterindex] of tregisterindex = (
  1220. {$i r8664op.inc}
  1221. );
  1222. {$else x86_64}
  1223. opcode_table:array[tregisterindex] of tregisterindex = (
  1224. {$i r386op.inc}
  1225. );
  1226. {$endif x86_64}
  1227. var
  1228. regidx : tregisterindex;
  1229. begin
  1230. regidx:=findreg_by_number(r);
  1231. if regidx<>0 then
  1232. result:=opcode_table[regidx]
  1233. else
  1234. begin
  1235. Message1(asmw_e_invalid_register,generic_regname(r));
  1236. result:=0;
  1237. end;
  1238. end;
  1239. function process_ea(const input:toper;var output:ea;rfield:longint):boolean;
  1240. var
  1241. sym : tasmsymbol;
  1242. md,s,rv : byte;
  1243. base,index,scalefactor,
  1244. o : longint;
  1245. ir,br : Tregister;
  1246. isub,bsub : tsubregister;
  1247. begin
  1248. process_ea:=false;
  1249. {Register ?}
  1250. if (input.typ=top_reg) then
  1251. begin
  1252. rv:=regval(input.reg);
  1253. output.sib_present:=false;
  1254. output.bytes:=0;
  1255. output.modrm:=$c0 or (rfield shl 3) or rv;
  1256. output.size:=1;
  1257. process_ea:=true;
  1258. exit;
  1259. end;
  1260. {No register, so memory reference.}
  1261. if (input.typ<>top_ref) then
  1262. internalerror(200409262);
  1263. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1264. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1265. internalerror(200301081);
  1266. ir:=input.ref^.index;
  1267. br:=input.ref^.base;
  1268. isub:=getsubreg(ir);
  1269. bsub:=getsubreg(br);
  1270. s:=input.ref^.scalefactor;
  1271. o:=input.ref^.offset;
  1272. sym:=input.ref^.symbol;
  1273. { it's direct address }
  1274. if (br=NR_NO) and (ir=NR_NO) then
  1275. begin
  1276. { it's a pure offset }
  1277. output.sib_present:=false;
  1278. output.bytes:=4;
  1279. output.modrm:=5 or (rfield shl 3);
  1280. end
  1281. else
  1282. { it's an indirection }
  1283. begin
  1284. { 16 bit address? }
  1285. if ((ir<>NR_NO) and (isub<>R_SUBD)) or
  1286. ((br<>NR_NO) and (bsub<>R_SUBD)) then
  1287. message(asmw_e_16bit_not_supported);
  1288. {$ifdef OPTEA}
  1289. { make single reg base }
  1290. if (br=NR_NO) and (s=1) then
  1291. begin
  1292. br:=ir;
  1293. ir:=NR_NO;
  1294. end;
  1295. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1296. if (br=NR_NO) and
  1297. (((s=2) and (ir<>NR_ESP)) or
  1298. (s=3) or (s=5) or (s=9)) then
  1299. begin
  1300. br:=ir;
  1301. dec(s);
  1302. end;
  1303. { swap ESP into base if scalefactor is 1 }
  1304. if (s=1) and (ir=NR_ESP) then
  1305. begin
  1306. ir:=br;
  1307. br:=NR_ESP;
  1308. end;
  1309. {$endif OPTEA}
  1310. { wrong, for various reasons }
  1311. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1312. exit;
  1313. { base }
  1314. case br of
  1315. NR_EAX : base:=0;
  1316. NR_ECX : base:=1;
  1317. NR_EDX : base:=2;
  1318. NR_EBX : base:=3;
  1319. NR_ESP : base:=4;
  1320. NR_NO,
  1321. NR_EBP : base:=5;
  1322. NR_ESI : base:=6;
  1323. NR_EDI : base:=7;
  1324. else
  1325. exit;
  1326. end;
  1327. { index }
  1328. case ir of
  1329. NR_EAX : index:=0;
  1330. NR_ECX : index:=1;
  1331. NR_EDX : index:=2;
  1332. NR_EBX : index:=3;
  1333. NR_NO : index:=4;
  1334. NR_EBP : index:=5;
  1335. NR_ESI : index:=6;
  1336. NR_EDI : index:=7;
  1337. else
  1338. exit;
  1339. end;
  1340. case s of
  1341. 0,
  1342. 1 : scalefactor:=0;
  1343. 2 : scalefactor:=1;
  1344. 4 : scalefactor:=2;
  1345. 8 : scalefactor:=3;
  1346. else
  1347. exit;
  1348. end;
  1349. if (br=NR_NO) or
  1350. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1351. md:=0
  1352. else
  1353. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1354. md:=1
  1355. else
  1356. md:=2;
  1357. if (br=NR_NO) or (md=2) then
  1358. output.bytes:=4
  1359. else
  1360. output.bytes:=md;
  1361. { SIB needed ? }
  1362. if (ir=NR_NO) and (br<>NR_ESP) then
  1363. begin
  1364. output.sib_present:=false;
  1365. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1366. end
  1367. else
  1368. begin
  1369. output.sib_present:=true;
  1370. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1371. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1372. end;
  1373. end;
  1374. if output.sib_present then
  1375. output.size:=2+output.bytes
  1376. else
  1377. output.size:=1+output.bytes;
  1378. process_ea:=true;
  1379. end;
  1380. function taicpu.calcsize(p:PInsEntry):shortint;
  1381. var
  1382. codes : pchar;
  1383. c : byte;
  1384. len : shortint;
  1385. ea_data : ea;
  1386. begin
  1387. len:=0;
  1388. codes:=@p^.code;
  1389. repeat
  1390. c:=ord(codes^);
  1391. inc(codes);
  1392. case c of
  1393. 0 :
  1394. break;
  1395. 1,2,3 :
  1396. begin
  1397. inc(codes,c);
  1398. inc(len,c);
  1399. end;
  1400. 8,9,10 :
  1401. begin
  1402. inc(codes);
  1403. inc(len);
  1404. end;
  1405. 4,5,6,7 :
  1406. begin
  1407. if opsize=S_W then
  1408. inc(len,2)
  1409. else
  1410. inc(len);
  1411. end;
  1412. 15,
  1413. 12,13,14,
  1414. 16,17,18,
  1415. 20,21,22,
  1416. 40,41,42 :
  1417. inc(len);
  1418. 24,25,26,
  1419. 31,
  1420. 48,49,50 :
  1421. inc(len,2);
  1422. 28,29,30, { we don't have 16 bit immediates code }
  1423. 32,33,34,
  1424. 52,53,54,
  1425. 56,57,58 :
  1426. inc(len,4);
  1427. 192,193,194 :
  1428. if NeedAddrPrefix(c-192) then
  1429. inc(len);
  1430. 208,
  1431. 210 :
  1432. inc(len);
  1433. 200,
  1434. 201,
  1435. 202,
  1436. 209,
  1437. 211,
  1438. 217,218: ;
  1439. 219,220 :
  1440. inc(len);
  1441. 216 :
  1442. begin
  1443. inc(codes);
  1444. inc(len);
  1445. end;
  1446. 224,225,226 :
  1447. begin
  1448. InternalError(777002);
  1449. end;
  1450. else
  1451. begin
  1452. if (c>=64) and (c<=191) then
  1453. begin
  1454. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1455. Message(asmw_e_invalid_effective_address)
  1456. else
  1457. inc(len,ea_data.size);
  1458. end
  1459. else
  1460. InternalError(777003);
  1461. end;
  1462. end;
  1463. until false;
  1464. calcsize:=len;
  1465. end;
  1466. procedure taicpu.GenCode(objdata:TAsmObjectData);
  1467. {
  1468. * the actual codes (C syntax, i.e. octal):
  1469. * \0 - terminates the code. (Unless it's a literal of course.)
  1470. * \1, \2, \3 - that many literal bytes follow in the code stream
  1471. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1472. * (POP is never used for CS) depending on operand 0
  1473. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1474. * on operand 0
  1475. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1476. * to the register value of operand 0, 1 or 2
  1477. * \17 - encodes the literal byte 0. (Some compilers don't take
  1478. * kindly to a zero byte in the _middle_ of a compile time
  1479. * string constant, so I had to put this hack in.)
  1480. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1481. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1482. * \24, \25, \26 - an unsigned byte immediate operand, from operand 0, 1 or 2
  1483. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1484. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1485. * assembly mode or the address-size override on the operand
  1486. * \37 - a word constant, from the _segment_ part of operand 0
  1487. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1488. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1489. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1490. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1491. * assembly mode or the address-size override on the operand
  1492. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1493. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1494. * field the register value of operand b.
  1495. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1496. * field equal to digit b.
  1497. * \30x - might be an 0x67 byte, depending on the address size of
  1498. * the memory reference in operand x.
  1499. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1500. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1501. * \312 - indicates fixed 64-bit address size, i.e. optional 0x48.
  1502. * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1503. * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1504. * \322 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1505. * \323 - indicates that this instruction is only valid when the
  1506. * operand size is the default (instruction to disassembler,
  1507. * generates no code in the assembler)
  1508. * \330 - a literal byte follows in the code stream, to be added
  1509. * to the condition code value of the instruction.
  1510. * \340 - reserve <operand 0> bytes of uninitialised storage.
  1511. * Operand 0 had better be a segmentless constant.
  1512. }
  1513. var
  1514. currval : longint;
  1515. currsym : tasmsymbol;
  1516. procedure getvalsym(opidx:longint);
  1517. begin
  1518. case oper[opidx]^.typ of
  1519. top_ref :
  1520. begin
  1521. currval:=oper[opidx]^.ref^.offset;
  1522. currsym:=oper[opidx]^.ref^.symbol;
  1523. end;
  1524. top_const :
  1525. begin
  1526. currval:=longint(oper[opidx]^.val);
  1527. currsym:=nil;
  1528. end;
  1529. else
  1530. Message(asmw_e_immediate_or_reference_expected);
  1531. end;
  1532. end;
  1533. const
  1534. CondVal:array[TAsmCond] of byte=($0,
  1535. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  1536. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  1537. $0, $A, $A, $B, $8, $4);
  1538. var
  1539. c : byte;
  1540. pb,
  1541. codes : pchar;
  1542. bytes : array[0..3] of byte;
  1543. rfield,
  1544. data,s,opidx : longint;
  1545. ea_data : ea;
  1546. begin
  1547. {$ifdef EXTDEBUG}
  1548. { safety check }
  1549. if objdata.currsec.datasize<>insoffset then
  1550. internalerror(200130121);
  1551. {$endif EXTDEBUG}
  1552. { load data to write }
  1553. codes:=insentry^.code;
  1554. { Force word push/pop for registers }
  1555. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  1556. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  1557. begin
  1558. bytes[0]:=$66;
  1559. objdata.writebytes(bytes,1);
  1560. end;
  1561. repeat
  1562. c:=ord(codes^);
  1563. inc(codes);
  1564. case c of
  1565. 0 :
  1566. break;
  1567. 1,2,3 :
  1568. begin
  1569. objdata.writebytes(codes^,c);
  1570. inc(codes,c);
  1571. end;
  1572. 4,6 :
  1573. begin
  1574. case oper[0]^.reg of
  1575. NR_CS:
  1576. bytes[0]:=$e;
  1577. NR_NO,
  1578. NR_DS:
  1579. bytes[0]:=$1e;
  1580. NR_ES:
  1581. bytes[0]:=$6;
  1582. NR_SS:
  1583. bytes[0]:=$16;
  1584. else
  1585. internalerror(777004);
  1586. end;
  1587. if c=4 then
  1588. inc(bytes[0]);
  1589. objdata.writebytes(bytes,1);
  1590. end;
  1591. 5,7 :
  1592. begin
  1593. case oper[0]^.reg of
  1594. NR_FS:
  1595. bytes[0]:=$a0;
  1596. NR_GS:
  1597. bytes[0]:=$a8;
  1598. else
  1599. internalerror(777005);
  1600. end;
  1601. if c=5 then
  1602. inc(bytes[0]);
  1603. objdata.writebytes(bytes,1);
  1604. end;
  1605. 8,9,10 :
  1606. begin
  1607. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  1608. inc(codes);
  1609. objdata.writebytes(bytes,1);
  1610. end;
  1611. 15 :
  1612. begin
  1613. bytes[0]:=0;
  1614. objdata.writebytes(bytes,1);
  1615. end;
  1616. 12,13,14 :
  1617. begin
  1618. getvalsym(c-12);
  1619. if (currval<-128) or (currval>127) then
  1620. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  1621. if assigned(currsym) then
  1622. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1623. else
  1624. objdata.writebytes(currval,1);
  1625. end;
  1626. 16,17,18 :
  1627. begin
  1628. getvalsym(c-16);
  1629. if (currval<-256) or (currval>255) then
  1630. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  1631. if assigned(currsym) then
  1632. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1633. else
  1634. objdata.writebytes(currval,1);
  1635. end;
  1636. 20,21,22 :
  1637. begin
  1638. getvalsym(c-20);
  1639. if (currval<0) or (currval>255) then
  1640. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  1641. if assigned(currsym) then
  1642. objdata.writereloc(currval,1,currsym,RELOC_ABSOLUTE)
  1643. else
  1644. objdata.writebytes(currval,1);
  1645. end;
  1646. 24,25,26 :
  1647. begin
  1648. getvalsym(c-24);
  1649. if (currval<-65536) or (currval>65535) then
  1650. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  1651. if assigned(currsym) then
  1652. objdata.writereloc(currval,2,currsym,RELOC_ABSOLUTE)
  1653. else
  1654. objdata.writebytes(currval,2);
  1655. end;
  1656. 28,29,30 :
  1657. begin
  1658. getvalsym(c-28);
  1659. if assigned(currsym) then
  1660. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1661. else
  1662. objdata.writebytes(currval,4);
  1663. end;
  1664. 32,33,34 :
  1665. begin
  1666. getvalsym(c-32);
  1667. if assigned(currsym) then
  1668. objdata.writereloc(currval,4,currsym,RELOC_ABSOLUTE)
  1669. else
  1670. objdata.writebytes(currval,4);
  1671. end;
  1672. 40,41,42 :
  1673. begin
  1674. getvalsym(c-40);
  1675. data:=currval-insend;
  1676. if assigned(currsym) then
  1677. inc(data,currsym.address);
  1678. if (data>127) or (data<-128) then
  1679. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  1680. objdata.writebytes(data,1);
  1681. end;
  1682. 52,53,54 :
  1683. begin
  1684. getvalsym(c-52);
  1685. if assigned(currsym) then
  1686. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1687. else
  1688. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1689. end;
  1690. 56,57,58 :
  1691. begin
  1692. getvalsym(c-56);
  1693. if assigned(currsym) then
  1694. objdata.writereloc(currval,4,currsym,RELOC_RELATIVE)
  1695. else
  1696. objdata.writereloc(currval-insend,4,nil,RELOC_ABSOLUTE)
  1697. end;
  1698. 192,193,194 :
  1699. begin
  1700. if NeedAddrPrefix(c-192) then
  1701. begin
  1702. bytes[0]:=$67;
  1703. objdata.writebytes(bytes,1);
  1704. end;
  1705. end;
  1706. 200 :
  1707. begin
  1708. bytes[0]:=$67;
  1709. objdata.writebytes(bytes,1);
  1710. end;
  1711. 208 :
  1712. begin
  1713. bytes[0]:=$66;
  1714. objdata.writebytes(bytes,1);
  1715. end;
  1716. 210 :
  1717. begin
  1718. bytes[0]:=$48;
  1719. objdata.writebytes(bytes,1);
  1720. end;
  1721. 216 :
  1722. begin
  1723. bytes[0]:=ord(codes^)+condval[condition];
  1724. inc(codes);
  1725. objdata.writebytes(bytes,1);
  1726. end;
  1727. 201,
  1728. 202,
  1729. 209,
  1730. 211,
  1731. 217,218 :
  1732. begin
  1733. { these are dissambler hints or 32 bit prefixes which
  1734. are not needed }
  1735. end;
  1736. 219 :
  1737. begin
  1738. bytes[0]:=$f3;
  1739. objdata.writebytes(bytes,1);
  1740. end;
  1741. 220 :
  1742. begin
  1743. bytes[0]:=$f2;
  1744. objdata.writebytes(bytes,1);
  1745. end;
  1746. 31,
  1747. 48,49,50,
  1748. 224,225,226 :
  1749. begin
  1750. InternalError(777006);
  1751. end
  1752. else
  1753. begin
  1754. if (c>=64) and (c<=191) then
  1755. begin
  1756. if (c<127) then
  1757. begin
  1758. if (oper[c and 7]^.typ=top_reg) then
  1759. rfield:=regval(oper[c and 7]^.reg)
  1760. else
  1761. rfield:=regval(oper[c and 7]^.ref^.base);
  1762. end
  1763. else
  1764. rfield:=c and 7;
  1765. opidx:=(c shr 3) and 7;
  1766. if not process_ea(oper[opidx]^,ea_data,rfield) then
  1767. Message(asmw_e_invalid_effective_address);
  1768. pb:=@bytes;
  1769. pb^:=chr(ea_data.modrm);
  1770. inc(pb);
  1771. if ea_data.sib_present then
  1772. begin
  1773. pb^:=chr(ea_data.sib);
  1774. inc(pb);
  1775. end;
  1776. s:=pb-pchar(@bytes);
  1777. objdata.writebytes(bytes,s);
  1778. case ea_data.bytes of
  1779. 0 : ;
  1780. 1 :
  1781. begin
  1782. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  1783. objdata.writereloc(oper[opidx]^.ref^.offset,1,oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE)
  1784. else
  1785. begin
  1786. bytes[0]:=oper[opidx]^.ref^.offset;
  1787. objdata.writebytes(bytes,1);
  1788. end;
  1789. inc(s);
  1790. end;
  1791. 2,4 :
  1792. begin
  1793. objdata.writereloc(oper[opidx]^.ref^.offset,ea_data.bytes,
  1794. oper[opidx]^.ref^.symbol,RELOC_ABSOLUTE);
  1795. inc(s,ea_data.bytes);
  1796. end;
  1797. end;
  1798. end
  1799. else
  1800. InternalError(777007);
  1801. end;
  1802. end;
  1803. until false;
  1804. end;
  1805. {$endif NOAG386BIN}
  1806. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  1807. begin
  1808. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  1809. (regtype = R_INTREGISTER) and
  1810. (ops=2) and
  1811. (oper[0]^.typ=top_reg) and
  1812. (oper[1]^.typ=top_reg) and
  1813. (oper[0]^.reg=oper[1]^.reg)
  1814. ) or
  1815. (((opcode=A_MOVSS) or (opcode=A_MOVSD)) and
  1816. (regtype = R_MMREGISTER) 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. );
  1822. end;
  1823. procedure build_spilling_operation_type_table;
  1824. var
  1825. opcode : tasmop;
  1826. i : integer;
  1827. begin
  1828. new(operation_type_table);
  1829. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  1830. for opcode:=low(tasmop) to high(tasmop) do
  1831. begin
  1832. for i:=1 to MaxInsChanges do
  1833. begin
  1834. case InsProp[opcode].Ch[i] of
  1835. Ch_Rop1 :
  1836. operation_type_table^[opcode,0]:=operand_read;
  1837. Ch_Wop1 :
  1838. operation_type_table^[opcode,0]:=operand_write;
  1839. Ch_RWop1,
  1840. Ch_Mop1 :
  1841. operation_type_table^[opcode,0]:=operand_readwrite;
  1842. Ch_Rop2 :
  1843. operation_type_table^[opcode,1]:=operand_read;
  1844. Ch_Wop2 :
  1845. operation_type_table^[opcode,1]:=operand_write;
  1846. Ch_RWop2,
  1847. Ch_Mop2 :
  1848. operation_type_table^[opcode,1]:=operand_readwrite;
  1849. Ch_Rop3 :
  1850. operation_type_table^[opcode,2]:=operand_read;
  1851. Ch_Wop3 :
  1852. operation_type_table^[opcode,2]:=operand_write;
  1853. Ch_RWop3,
  1854. Ch_Mop3 :
  1855. operation_type_table^[opcode,2]:=operand_readwrite;
  1856. end;
  1857. end;
  1858. end;
  1859. { Special cases that can't be decoded from the InsChanges flags }
  1860. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  1861. end;
  1862. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  1863. begin
  1864. result:=operation_type_table^[opcode,opnr];
  1865. end;
  1866. function spilling_create_load(const ref:treference;r:tregister): tai;
  1867. begin
  1868. case getregtype(r) of
  1869. R_INTREGISTER :
  1870. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),ref,r);
  1871. R_MMREGISTER :
  1872. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  1873. else
  1874. internalerror(200401041);
  1875. end;
  1876. end;
  1877. function spilling_create_store(r:tregister; const ref:treference): tai;
  1878. begin
  1879. case getregtype(r) of
  1880. R_INTREGISTER :
  1881. result:=taicpu.op_reg_ref(A_MOV,reg2opsize(r),r,ref);
  1882. R_MMREGISTER :
  1883. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  1884. else
  1885. internalerror(200401041);
  1886. end;
  1887. end;
  1888. {*****************************************************************************
  1889. Instruction table
  1890. *****************************************************************************}
  1891. procedure BuildInsTabCache;
  1892. {$ifndef NOAG386BIN}
  1893. var
  1894. i : longint;
  1895. {$endif}
  1896. begin
  1897. {$ifndef NOAG386BIN}
  1898. new(instabcache);
  1899. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  1900. i:=0;
  1901. while (i<InsTabEntries) do
  1902. begin
  1903. if InsTabCache^[InsTab[i].OPcode]=-1 then
  1904. InsTabCache^[InsTab[i].OPcode]:=i;
  1905. inc(i);
  1906. end;
  1907. {$endif NOAG386BIN}
  1908. end;
  1909. procedure InitAsm;
  1910. begin
  1911. build_spilling_operation_type_table;
  1912. {$ifndef NOAG386BIN}
  1913. if not assigned(instabcache) then
  1914. BuildInsTabCache;
  1915. {$endif NOAG386BIN}
  1916. end;
  1917. procedure DoneAsm;
  1918. begin
  1919. if assigned(operation_type_table) then
  1920. begin
  1921. dispose(operation_type_table);
  1922. operation_type_table:=nil;
  1923. end;
  1924. {$ifndef NOAG386BIN}
  1925. if assigned(instabcache) then
  1926. begin
  1927. dispose(instabcache);
  1928. instabcache:=nil;
  1929. end;
  1930. {$endif NOAG386BIN}
  1931. end;
  1932. begin
  1933. cai_align:=tai_align;
  1934. cai_cpu:=taicpu;
  1935. end.