aasmcpu.pas 114 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380
  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,verbose,
  25. cpubase,
  26. cgbase,cgutils,
  27. symtype,
  28. aasmbase,aasmtai,aasmdata,aasmsym,
  29. ogbase;
  30. const
  31. { "mov reg,reg" source operand number }
  32. O_MOV_SOURCE = 0;
  33. { "mov reg,reg" destination operand number }
  34. O_MOV_DEST = 1;
  35. { Operand types }
  36. OT_NONE = $00000000;
  37. { Bits 0..7: sizes }
  38. OT_BITS8 = $00000001;
  39. OT_BITS16 = $00000002;
  40. OT_BITS32 = $00000004;
  41. OT_BITS64 = $00000008; { x86_64 and FPU }
  42. OT_BITS128 = $10000000; { 16 byte SSE }
  43. OT_BITS256 = $20000000; { 32 byte AVX }
  44. OT_BITS80 = $00000010; { FPU only }
  45. OT_FAR = $00000020; { this means 16:16 or 16:32, like in CALL/JMP }
  46. OT_NEAR = $00000040;
  47. OT_SHORT = $00000080;
  48. { TODO: FAR/NEAR/SHORT are sizes too, they should be included into size mask,
  49. but this requires adjusting the opcode table }
  50. OT_SIZE_MASK = $3000001F; { all the size attributes }
  51. OT_NON_SIZE = longint(not OT_SIZE_MASK);
  52. { Bits 8..11: modifiers }
  53. OT_SIGNED = $00000100; { the operand need to be signed -128-127 }
  54. OT_TO = $00000200; { reverse effect in FADD, FSUB &c }
  55. OT_COLON = $00000400; { operand is followed by a colon }
  56. OT_MODIFIER_MASK = $00000F00;
  57. { Bits 12..15: type of operand }
  58. OT_REGISTER = $00001000;
  59. OT_IMMEDIATE = $00002000;
  60. OT_MEMORY = $0000C000; { always includes 'OT_REGMEM' bit as well }
  61. OT_REGMEM = $00008000; { for r/m, ie EA, operands }
  62. OT_TYPE_MASK = OT_REGISTER or OT_IMMEDIATE or OT_MEMORY or OT_REGMEM;
  63. OT_REGNORM = OT_REGISTER or OT_REGMEM; { 'normal' reg, qualifies as EA }
  64. { Bits 20..22, 24..26: register classes
  65. otf_* consts are not used alone, only to build other constants. }
  66. otf_reg_cdt = $00100000;
  67. otf_reg_gpr = $00200000;
  68. otf_reg_sreg = $00400000;
  69. otf_reg_fpu = $01000000;
  70. otf_reg_mmx = $02000000;
  71. otf_reg_xmm = $04000000;
  72. otf_reg_ymm = $08000000;
  73. { Bits 16..19: subclasses, meaning depends on classes field }
  74. otf_sub0 = $00010000;
  75. otf_sub1 = $00020000;
  76. otf_sub2 = $00040000;
  77. otf_sub3 = $00080000;
  78. OT_REG_SMASK = otf_sub0 or otf_sub1 or otf_sub2 or otf_sub3;
  79. OT_REG_TYPMASK = otf_reg_cdt or otf_reg_gpr or otf_reg_sreg or otf_reg_fpu or otf_reg_mmx or otf_reg_xmm or otf_reg_ymm;
  80. { register class 0: CRx, DRx and TRx }
  81. {$ifdef x86_64}
  82. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS64;
  83. {$else x86_64}
  84. OT_REG_CDT = OT_REGISTER or otf_reg_cdt or OT_BITS32;
  85. {$endif x86_64}
  86. OT_REG_CREG = OT_REG_CDT or otf_sub0; { CRn }
  87. OT_REG_DREG = OT_REG_CDT or otf_sub1; { DRn }
  88. OT_REG_TREG = OT_REG_CDT or otf_sub2; { TRn }
  89. OT_REG_CR4 = OT_REG_CDT or otf_sub3; { CR4 (Pentium only) }
  90. { register class 1: general-purpose registers }
  91. OT_REG_GPR = OT_REGNORM or otf_reg_gpr;
  92. OT_RM_GPR = OT_REGMEM or otf_reg_gpr;
  93. OT_REG8 = OT_REG_GPR or OT_BITS8; { 8-bit GPR }
  94. OT_REG16 = OT_REG_GPR or OT_BITS16;
  95. OT_REG32 = OT_REG_GPR or OT_BITS32;
  96. OT_REG64 = OT_REG_GPR or OT_BITS64;
  97. { GPR subclass 0: accumulator: AL, AX, EAX or RAX }
  98. OT_REG_ACCUM = OT_REG_GPR or otf_sub0;
  99. OT_REG_AL = OT_REG_ACCUM or OT_BITS8;
  100. OT_REG_AX = OT_REG_ACCUM or OT_BITS16;
  101. OT_REG_EAX = OT_REG_ACCUM or OT_BITS32;
  102. {$ifdef x86_64}
  103. OT_REG_RAX = OT_REG_ACCUM or OT_BITS64;
  104. {$endif x86_64}
  105. { GPR subclass 1: counter: CL, CX, ECX or RCX }
  106. OT_REG_COUNT = OT_REG_GPR or otf_sub1;
  107. OT_REG_CL = OT_REG_COUNT or OT_BITS8;
  108. OT_REG_CX = OT_REG_COUNT or OT_BITS16;
  109. OT_REG_ECX = OT_REG_COUNT or OT_BITS32;
  110. {$ifdef x86_64}
  111. OT_REG_RCX = OT_REG_COUNT or OT_BITS64;
  112. {$endif x86_64}
  113. { GPR subclass 2: data register: DL, DX, EDX or RDX }
  114. OT_REG_DX = OT_REG_GPR or otf_sub2 or OT_BITS16;
  115. OT_REG_EDX = OT_REG_GPR or otf_sub2 or OT_BITS32;
  116. { register class 2: Segment registers }
  117. OT_REG_SREG = OT_REGISTER or otf_reg_sreg or OT_BITS16;
  118. OT_REG_CS = OT_REG_SREG or otf_sub0; { CS }
  119. OT_REG_DESS = OT_REG_SREG or otf_sub1; { DS, ES, SS (non-CS 86 registers) }
  120. OT_REG_FSGS = OT_REG_SREG or otf_sub2; { FS, GS (386 extended registers) }
  121. { register class 3: FPU registers }
  122. OT_FPUREG = OT_REGISTER or otf_reg_fpu;
  123. OT_FPU0 = OT_FPUREG or otf_sub0; { FPU stack register zero }
  124. { register class 4: MMX (both reg and r/m) }
  125. OT_MMXREG = OT_REGNORM or otf_reg_mmx;
  126. OT_MMXRM = OT_REGMEM or otf_reg_mmx;
  127. { register class 5: XMM (both reg and r/m) }
  128. OT_XMMREG = OT_REGNORM or otf_reg_xmm;
  129. OT_XMMRM = OT_REGMEM or otf_reg_xmm;
  130. { register class 5: XMM (both reg and r/m) }
  131. OT_YMMREG = OT_REGNORM or otf_reg_ymm;
  132. OT_YMMRM = OT_REGMEM or otf_reg_ymm;
  133. { Memory operands }
  134. OT_MEM8 = OT_MEMORY or OT_BITS8;
  135. OT_MEM16 = OT_MEMORY or OT_BITS16;
  136. OT_MEM32 = OT_MEMORY or OT_BITS32;
  137. OT_MEM64 = OT_MEMORY or OT_BITS64;
  138. OT_MEM128 = OT_MEMORY or OT_BITS128;
  139. OT_MEM256 = OT_MEMORY or OT_BITS256;
  140. OT_MEM80 = OT_MEMORY or OT_BITS80;
  141. OT_MEM_OFFS = OT_MEMORY or otf_sub0; { special type of EA }
  142. { simple [address] offset }
  143. { Matches any type of r/m operand }
  144. OT_MEMORY_ANY = OT_MEMORY or OT_RM_GPR or OT_XMMRM or OT_MMXRM or OT_YMMRM;
  145. { Immediate operands }
  146. OT_IMM8 = OT_IMMEDIATE or OT_BITS8;
  147. OT_IMM16 = OT_IMMEDIATE or OT_BITS16;
  148. OT_IMM32 = OT_IMMEDIATE or OT_BITS32;
  149. OT_IMM64 = OT_IMMEDIATE or OT_BITS64;
  150. OT_ONENESS = otf_sub0; { special type of immediate operand }
  151. OT_UNITY = OT_IMMEDIATE or OT_ONENESS; { for shift/rotate instructions }
  152. { Size of the instruction table converted by nasmconv.pas }
  153. {$if defined(x86_64)}
  154. instabentries = {$i x8664nop.inc}
  155. {$elseif defined(i386)}
  156. instabentries = {$i i386nop.inc}
  157. {$elseif defined(i8086)}
  158. instabentries = {$i i8086nop.inc}
  159. {$endif}
  160. maxinfolen = 8;
  161. MaxInsChanges = 3; { Max things a instruction can change }
  162. type
  163. { What an instruction can change. Needed for optimizer and spilling code.
  164. Note: The order of this enumeration is should not be changed! }
  165. TInsChange = (Ch_None,
  166. {Read from a register}
  167. Ch_REAX, Ch_RECX, Ch_REDX, Ch_REBX, Ch_RESP, Ch_REBP, Ch_RESI, Ch_REDI,
  168. {write from a register}
  169. Ch_WEAX, Ch_WECX, Ch_WEDX, Ch_WEBX, Ch_WESP, Ch_WEBP, Ch_WESI, Ch_WEDI,
  170. {read and write from/to a register}
  171. Ch_RWEAX, Ch_RWECX, Ch_RWEDX, Ch_RWEBX, Ch_RWESP, Ch_RWEBP, Ch_RWESI, Ch_RWEDI,
  172. {modify the contents of a register with the purpose of using
  173. this changed content afterwards (add/sub/..., but e.g. not rep
  174. or movsd)}
  175. Ch_MEAX, Ch_MECX, Ch_MEDX, Ch_MEBX, Ch_MESP, Ch_MEBP, Ch_MESI, Ch_MEDI,
  176. Ch_CDirFlag {clear direction flag}, Ch_SDirFlag {set dir flag},
  177. Ch_RFlags, Ch_WFlags, Ch_RWFlags, Ch_FPU,
  178. Ch_Rop1, Ch_Wop1, Ch_RWop1,Ch_Mop1,
  179. Ch_Rop2, Ch_Wop2, Ch_RWop2,Ch_Mop2,
  180. Ch_Rop3, Ch_WOp3, Ch_RWOp3,Ch_Mop3,
  181. Ch_WMemEDI,
  182. Ch_All,
  183. { x86_64 registers }
  184. Ch_RRAX, Ch_RRCX, Ch_RRDX, Ch_RRBX, Ch_RRSP, Ch_RRBP, Ch_RRSI, Ch_RRDI,
  185. Ch_WRAX, Ch_WRCX, Ch_WRDX, Ch_WRBX, Ch_WRSP, Ch_WRBP, Ch_WRSI, Ch_WRDI,
  186. Ch_RWRAX, Ch_RWRCX, Ch_RWRDX, Ch_RWRBX, Ch_RWRSP, Ch_RWRBP, Ch_RWRSI, Ch_RWRDI,
  187. Ch_MRAX, Ch_MRCX, Ch_MRDX, Ch_MRBX, Ch_MRSP, Ch_MRBP, Ch_MRSI, Ch_MRDI
  188. );
  189. TInsProp = packed record
  190. Ch : Array[1..MaxInsChanges] of TInsChange;
  191. end;
  192. TMemRefSizeInfo = (msiUnkown, msiUnsupported, msiNoSize,
  193. msiMultiple, msiMultiple8, msiMultiple16, msiMultiple32,
  194. msiMultiple64, msiMultiple128, msiMultiple256,
  195. msiMemRegSize, msiMemRegx64y128, msiMemRegx64y256,
  196. msiMem8, msiMem16, msiMem32, msiMem64, msiMem128, msiMem256);
  197. TConstSizeInfo = (csiUnkown, csiMultiple, csiNoSize, csiMem8, csiMem16, csiMem32, csiMem64);
  198. TInsTabMemRefSizeInfoRec = record
  199. MemRefSize : TMemRefSizeInfo;
  200. ExistsSSEAVX: boolean;
  201. ConstSize : TConstSizeInfo;
  202. end;
  203. const
  204. MemRefMultiples: set of TMemRefSizeInfo = [msiMultiple, msiMultiple8,
  205. msiMultiple16, msiMultiple32,
  206. msiMultiple64, msiMultiple128,
  207. msiMultiple256];
  208. InsProp : array[tasmop] of TInsProp =
  209. {$if defined(x86_64)}
  210. {$i x8664pro.inc}
  211. {$elseif defined(i386)}
  212. {$i i386prop.inc}
  213. {$elseif defined(i8086)}
  214. {$i i8086prop.inc}
  215. {$endif}
  216. type
  217. TOperandOrder = (op_intel,op_att);
  218. tinsentry=packed record
  219. opcode : tasmop;
  220. ops : byte;
  221. optypes : array[0..max_operands-1] of longint;
  222. code : array[0..maxinfolen] of char;
  223. flags : int64;
  224. end;
  225. pinsentry=^tinsentry;
  226. { alignment for operator }
  227. tai_align = class(tai_align_abstract)
  228. reg : tregister;
  229. constructor create(b:byte);override;
  230. constructor create_op(b: byte; _op: byte);override;
  231. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;override;
  232. end;
  233. taicpu = class(tai_cpu_abstract_sym)
  234. opsize : topsize;
  235. constructor op_none(op : tasmop);
  236. constructor op_none(op : tasmop;_size : topsize);
  237. constructor op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  238. constructor op_const(op : tasmop;_size : topsize;_op1 : aint);
  239. constructor op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  240. constructor op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  241. constructor op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  242. constructor op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  243. constructor op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  244. constructor op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  245. constructor op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  246. constructor op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  247. constructor op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  248. constructor op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  249. constructor op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  250. constructor op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  251. constructor op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  252. { this is for Jmp instructions }
  253. constructor op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  254. constructor op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  255. constructor op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  256. constructor op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  257. constructor op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  258. procedure changeopsize(siz:topsize);
  259. function GetString:string;
  260. { This is a workaround for the GAS non commutative fpu instruction braindamage.
  261. Early versions of the UnixWare assembler had a bug where some fpu instructions
  262. were reversed and GAS still keeps this "feature" for compatibility.
  263. for details: http://sourceware.org/binutils/docs/as/i386_002dBugs.html#i386_002dBugs
  264. http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=372528
  265. http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax#Caveats
  266. Since FPC is "GAS centric" due to its history it generates instructions with the same operand order so
  267. when generating output for other assemblers, the opcodes must be fixed before writing them.
  268. This function returns the fixed opcodes. Changing the opcodes permanently is no good idea
  269. because in case of smartlinking assembler is generated twice so at the second run wrong
  270. assembler is generated.
  271. }
  272. function FixNonCommutativeOpcodes: tasmop;
  273. private
  274. FOperandOrder : TOperandOrder;
  275. procedure init(_size : topsize); { this need to be called by all constructor }
  276. public
  277. { the next will reset all instructions that can change in pass 2 }
  278. procedure ResetPass1;override;
  279. procedure ResetPass2;override;
  280. function CheckIfValid:boolean;
  281. function Pass1(objdata:TObjData):longint;override;
  282. procedure Pass2(objdata:TObjData);override;
  283. procedure SetOperandOrder(order:TOperandOrder);
  284. function is_same_reg_move(regtype: Tregistertype):boolean;override;
  285. { register spilling code }
  286. function spilling_get_operation_type(opnr: longint): topertype;override;
  287. private
  288. { next fields are filled in pass1, so pass2 is faster }
  289. insentry : PInsEntry;
  290. insoffset : longint;
  291. LastInsOffset : longint; { need to be public to be reset }
  292. inssize : shortint;
  293. {$ifdef x86_64}
  294. rex : byte;
  295. {$endif x86_64}
  296. function InsEnd:longint;
  297. procedure create_ot(objdata:TObjData);
  298. function Matches(p:PInsEntry):boolean;
  299. function calcsize(p:PInsEntry):shortint;
  300. procedure gencode(objdata:TObjData);
  301. function NeedAddrPrefix(opidx:byte):boolean;
  302. procedure Swapoperands;
  303. function FindInsentry(objdata:TObjData):boolean;
  304. end;
  305. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  306. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  307. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  308. procedure InitAsm;
  309. procedure DoneAsm;
  310. implementation
  311. uses
  312. cutils,
  313. globals,
  314. systems,
  315. procinfo,
  316. itcpugas,
  317. symsym,
  318. cpuinfo;
  319. {*****************************************************************************
  320. Instruction table
  321. *****************************************************************************}
  322. const
  323. {Instruction flags }
  324. IF_NONE = $00000000;
  325. IF_SM = $00000001; { size match first two operands }
  326. IF_SM2 = $00000002;
  327. IF_SB = $00000004; { unsized operands can't be non-byte }
  328. IF_SW = $00000008; { unsized operands can't be non-word }
  329. IF_SD = $00000010; { unsized operands can't be nondword }
  330. IF_SMASK = $0000001f;
  331. IF_AR0 = $00000020; { SB, SW, SD applies to argument 0 }
  332. IF_AR1 = $00000040; { SB, SW, SD applies to argument 1 }
  333. IF_AR2 = $00000060; { SB, SW, SD applies to argument 2 }
  334. IF_ARMASK = $00000060; { mask for unsized argument spec }
  335. IF_ARSHIFT = 5; { LSB of IF_ARMASK }
  336. IF_PRIV = $00000100; { it's a privileged instruction }
  337. IF_SMM = $00000200; { it's only valid in SMM }
  338. IF_PROT = $00000400; { it's protected mode only }
  339. IF_NOX86_64 = $00000800; { removed instruction in x86_64 }
  340. IF_UNDOC = $00001000; { it's an undocumented instruction }
  341. IF_FPU = $00002000; { it's an FPU instruction }
  342. IF_MMX = $00004000; { it's an MMX instruction }
  343. { it's a 3DNow! instruction }
  344. IF_3DNOW = $00008000;
  345. { it's a SSE (KNI, MMX2) instruction }
  346. IF_SSE = $00010000;
  347. { SSE2 instructions }
  348. IF_SSE2 = $00020000;
  349. { SSE3 instructions }
  350. IF_SSE3 = $00040000;
  351. { SSE64 instructions }
  352. IF_SSE64 = $00080000;
  353. { the mask for processor types }
  354. {IF_PMASK = longint($FF000000);}
  355. { the mask for disassembly "prefer" }
  356. {IF_PFMASK = longint($F001FF00);}
  357. { SVM instructions }
  358. IF_SVM = $00100000;
  359. { SSE4 instructions }
  360. IF_SSE4 = $00200000;
  361. { TODO: These flags were added to make x86ins.dat more readable.
  362. Values must be reassigned to make any other use of them. }
  363. IF_SSSE3 = $00200000;
  364. IF_SSE41 = $00200000;
  365. IF_SSE42 = $00200000;
  366. IF_AVX = $00200000;
  367. IF_BMI1 = $00200000;
  368. IF_BMI2 = $00200000;
  369. IF_PLEVEL = $0F000000; { mask for processor level }
  370. IF_8086 = $00000000; { 8086 instruction }
  371. IF_186 = $01000000; { 186+ instruction }
  372. IF_286 = $02000000; { 286+ instruction }
  373. IF_386 = $03000000; { 386+ instruction }
  374. IF_486 = $04000000; { 486+ instruction }
  375. IF_PENT = $05000000; { Pentium instruction }
  376. IF_P6 = $06000000; { P6 instruction }
  377. IF_KATMAI = $07000000; { Katmai instructions }
  378. IF_WILLAMETTE = $08000000; { Willamette instructions }
  379. IF_PRESCOTT = $09000000; { Prescott instructions }
  380. IF_X86_64 = $0a000000;
  381. IF_CYRIX = $0b000000; { Cyrix-specific instruction }
  382. IF_AMD = $0c000000; { AMD-specific instruction }
  383. IF_CENTAUR = $0d000000; { centaur-specific instruction }
  384. IF_SANDYBRIDGE = $0e000000; { Sandybridge-specific instruction }
  385. { added flags }
  386. IF_PRE = $40000000; { it's a prefix instruction }
  387. IF_PASS2 = $80000000; { if the instruction can change in a second pass }
  388. type
  389. TInsTabCache=array[TasmOp] of longint;
  390. PInsTabCache=^TInsTabCache;
  391. TInsTabMemRefSizeInfoCache=array[TasmOp] of TInsTabMemRefSizeInfoRec;
  392. PInsTabMemRefSizeInfoCache=^TInsTabMemRefSizeInfoCache;
  393. const
  394. {$if defined(x86_64)}
  395. InsTab:array[0..instabentries-1] of TInsEntry={$i x8664tab.inc}
  396. {$elseif defined(i386)}
  397. InsTab:array[0..instabentries-1] of TInsEntry={$i i386tab.inc}
  398. {$elseif defined(i8086)}
  399. InsTab:array[0..instabentries-1] of TInsEntry={$i i8086tab.inc}
  400. {$endif}
  401. var
  402. InsTabCache : PInsTabCache;
  403. InsTabMemRefSizeInfoCache: PInsTabMemRefSizeInfoCache;
  404. const
  405. {$if defined(x86_64)}
  406. { Intel style operands ! }
  407. opsize_2_type:array[0..2,topsize] of longint=(
  408. (OT_NONE,
  409. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,OT_BITS64,OT_BITS64,OT_BITS64,
  410. OT_BITS16,OT_BITS32,OT_BITS64,
  411. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  412. OT_BITS64,
  413. OT_NEAR,OT_FAR,OT_SHORT,
  414. OT_NONE,
  415. OT_BITS128,
  416. OT_BITS256
  417. ),
  418. (OT_NONE,
  419. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,OT_BITS8,OT_BITS16,OT_BITS32,
  420. OT_BITS16,OT_BITS32,OT_BITS64,
  421. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  422. OT_BITS64,
  423. OT_NEAR,OT_FAR,OT_SHORT,
  424. OT_NONE,
  425. OT_BITS128,
  426. OT_BITS256
  427. ),
  428. (OT_NONE,
  429. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,OT_NONE,
  430. OT_BITS16,OT_BITS32,OT_BITS64,
  431. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  432. OT_BITS64,
  433. OT_NEAR,OT_FAR,OT_SHORT,
  434. OT_NONE,
  435. OT_BITS128,
  436. OT_BITS256
  437. )
  438. );
  439. reg_ot_table : array[tregisterindex] of longint = (
  440. {$i r8664ot.inc}
  441. );
  442. {$elseif defined(i386)}
  443. { Intel style operands ! }
  444. opsize_2_type:array[0..2,topsize] of longint=(
  445. (OT_NONE,
  446. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  447. OT_BITS16,OT_BITS32,OT_BITS64,
  448. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  449. OT_BITS64,
  450. OT_NEAR,OT_FAR,OT_SHORT,
  451. OT_NONE,
  452. OT_BITS128,
  453. OT_BITS256
  454. ),
  455. (OT_NONE,
  456. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  457. OT_BITS16,OT_BITS32,OT_BITS64,
  458. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  459. OT_BITS64,
  460. OT_NEAR,OT_FAR,OT_SHORT,
  461. OT_NONE,
  462. OT_BITS128,
  463. OT_BITS256
  464. ),
  465. (OT_NONE,
  466. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  467. OT_BITS16,OT_BITS32,OT_BITS64,
  468. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  469. OT_BITS64,
  470. OT_NEAR,OT_FAR,OT_SHORT,
  471. OT_NONE,
  472. OT_BITS128,
  473. OT_BITS256
  474. )
  475. );
  476. reg_ot_table : array[tregisterindex] of longint = (
  477. {$i r386ot.inc}
  478. );
  479. {$elseif defined(i8086)}
  480. { Intel style operands ! }
  481. opsize_2_type:array[0..2,topsize] of longint=(
  482. (OT_NONE,
  483. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS16,OT_BITS32,OT_BITS32,
  484. OT_BITS16,OT_BITS32,OT_BITS64,
  485. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  486. OT_BITS64,
  487. OT_NEAR,OT_FAR,OT_SHORT,
  488. OT_NONE,
  489. OT_BITS128,
  490. OT_BITS256
  491. ),
  492. (OT_NONE,
  493. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_BITS8,OT_BITS8,OT_BITS16,
  494. OT_BITS16,OT_BITS32,OT_BITS64,
  495. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  496. OT_BITS64,
  497. OT_NEAR,OT_FAR,OT_SHORT,
  498. OT_NONE,
  499. OT_BITS128,
  500. OT_BITS256
  501. ),
  502. (OT_NONE,
  503. OT_BITS8,OT_BITS16,OT_BITS32,OT_BITS64,OT_NONE,OT_NONE,OT_NONE,
  504. OT_BITS16,OT_BITS32,OT_BITS64,
  505. OT_BITS32,OT_BITS64,OT_BITS80,OT_BITS64,OT_NONE,
  506. OT_BITS64,
  507. OT_NEAR,OT_FAR,OT_SHORT,
  508. OT_NONE,
  509. OT_BITS128,
  510. OT_BITS256
  511. )
  512. );
  513. reg_ot_table : array[tregisterindex] of longint = (
  514. {$i r8086ot.inc}
  515. );
  516. {$endif}
  517. function MemRefInfo(aAsmop: TAsmOp): TInsTabMemRefSizeInfoRec;
  518. begin
  519. result := InsTabMemRefSizeInfoCache^[aAsmop];
  520. end;
  521. { Operation type for spilling code }
  522. type
  523. toperation_type_table=array[tasmop,0..Max_Operands] of topertype;
  524. var
  525. operation_type_table : ^toperation_type_table;
  526. {****************************************************************************
  527. TAI_ALIGN
  528. ****************************************************************************}
  529. constructor tai_align.create(b: byte);
  530. begin
  531. inherited create(b);
  532. reg:=NR_ECX;
  533. end;
  534. constructor tai_align.create_op(b: byte; _op: byte);
  535. begin
  536. inherited create_op(b,_op);
  537. reg:=NR_NO;
  538. end;
  539. function tai_align.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  540. const
  541. {$ifdef x86_64}
  542. alignarray:array[0..3] of string[4]=(
  543. #$66#$66#$66#$90,
  544. #$66#$66#$90,
  545. #$66#$90,
  546. #$90
  547. );
  548. {$else x86_64}
  549. alignarray:array[0..5] of string[8]=(
  550. #$8D#$B4#$26#$00#$00#$00#$00,
  551. #$8D#$B6#$00#$00#$00#$00,
  552. #$8D#$74#$26#$00,
  553. #$8D#$76#$00,
  554. #$89#$F6,
  555. #$90);
  556. {$endif x86_64}
  557. var
  558. bufptr : pchar;
  559. j : longint;
  560. localsize: byte;
  561. begin
  562. inherited calculatefillbuf(buf,executable);
  563. if not(use_op) and executable then
  564. begin
  565. bufptr:=pchar(@buf);
  566. { fillsize may still be used afterwards, so don't modify }
  567. { e.g. writebytes(hp.calculatefillbuf(buf)^,hp.fillsize) }
  568. localsize:=fillsize;
  569. while (localsize>0) do
  570. begin
  571. for j:=low(alignarray) to high(alignarray) do
  572. if (localsize>=length(alignarray[j])) then
  573. break;
  574. move(alignarray[j][1],bufptr^,length(alignarray[j]));
  575. inc(bufptr,length(alignarray[j]));
  576. dec(localsize,length(alignarray[j]));
  577. end;
  578. end;
  579. calculatefillbuf:=pchar(@buf);
  580. end;
  581. {*****************************************************************************
  582. Taicpu Constructors
  583. *****************************************************************************}
  584. procedure taicpu.changeopsize(siz:topsize);
  585. begin
  586. opsize:=siz;
  587. end;
  588. procedure taicpu.init(_size : topsize);
  589. begin
  590. { default order is att }
  591. FOperandOrder:=op_att;
  592. segprefix:=NR_NO;
  593. opsize:=_size;
  594. insentry:=nil;
  595. LastInsOffset:=-1;
  596. InsOffset:=0;
  597. InsSize:=0;
  598. end;
  599. constructor taicpu.op_none(op : tasmop);
  600. begin
  601. inherited create(op);
  602. init(S_NO);
  603. end;
  604. constructor taicpu.op_none(op : tasmop;_size : topsize);
  605. begin
  606. inherited create(op);
  607. init(_size);
  608. end;
  609. constructor taicpu.op_reg(op : tasmop;_size : topsize;_op1 : tregister);
  610. begin
  611. inherited create(op);
  612. init(_size);
  613. ops:=1;
  614. loadreg(0,_op1);
  615. end;
  616. constructor taicpu.op_const(op : tasmop;_size : topsize;_op1 : aint);
  617. begin
  618. inherited create(op);
  619. init(_size);
  620. ops:=1;
  621. loadconst(0,_op1);
  622. end;
  623. constructor taicpu.op_ref(op : tasmop;_size : topsize;const _op1 : treference);
  624. begin
  625. inherited create(op);
  626. init(_size);
  627. ops:=1;
  628. loadref(0,_op1);
  629. end;
  630. constructor taicpu.op_reg_reg(op : tasmop;_size : topsize;_op1,_op2 : tregister);
  631. begin
  632. inherited create(op);
  633. init(_size);
  634. ops:=2;
  635. loadreg(0,_op1);
  636. loadreg(1,_op2);
  637. end;
  638. constructor taicpu.op_reg_const(op:tasmop; _size: topsize; _op1: tregister; _op2: aint);
  639. begin
  640. inherited create(op);
  641. init(_size);
  642. ops:=2;
  643. loadreg(0,_op1);
  644. loadconst(1,_op2);
  645. end;
  646. constructor taicpu.op_reg_ref(op : tasmop;_size : topsize;_op1 : tregister;const _op2 : treference);
  647. begin
  648. inherited create(op);
  649. init(_size);
  650. ops:=2;
  651. loadreg(0,_op1);
  652. loadref(1,_op2);
  653. end;
  654. constructor taicpu.op_const_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister);
  655. begin
  656. inherited create(op);
  657. init(_size);
  658. ops:=2;
  659. loadconst(0,_op1);
  660. loadreg(1,_op2);
  661. end;
  662. constructor taicpu.op_const_const(op : tasmop;_size : topsize;_op1,_op2 : aint);
  663. begin
  664. inherited create(op);
  665. init(_size);
  666. ops:=2;
  667. loadconst(0,_op1);
  668. loadconst(1,_op2);
  669. end;
  670. constructor taicpu.op_const_ref(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference);
  671. begin
  672. inherited create(op);
  673. init(_size);
  674. ops:=2;
  675. loadconst(0,_op1);
  676. loadref(1,_op2);
  677. end;
  678. constructor taicpu.op_ref_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2 : tregister);
  679. begin
  680. inherited create(op);
  681. init(_size);
  682. ops:=2;
  683. loadref(0,_op1);
  684. loadreg(1,_op2);
  685. end;
  686. constructor taicpu.op_reg_reg_reg(op : tasmop;_size : topsize;_op1,_op2,_op3 : tregister);
  687. begin
  688. inherited create(op);
  689. init(_size);
  690. ops:=3;
  691. loadreg(0,_op1);
  692. loadreg(1,_op2);
  693. loadreg(2,_op3);
  694. end;
  695. constructor taicpu.op_const_reg_reg(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;_op3 : tregister);
  696. begin
  697. inherited create(op);
  698. init(_size);
  699. ops:=3;
  700. loadconst(0,_op1);
  701. loadreg(1,_op2);
  702. loadreg(2,_op3);
  703. end;
  704. constructor taicpu.op_ref_reg_reg(op : tasmop;_size : topsize;const _op1 : treference;_op2,_op3 : tregister);
  705. begin
  706. inherited create(op);
  707. init(_size);
  708. ops:=3;
  709. loadref(0,_op1);
  710. loadreg(1,_op2);
  711. loadreg(2,_op3);
  712. end;
  713. constructor taicpu.op_const_ref_reg(op : tasmop;_size : topsize;_op1 : aint;const _op2 : treference;_op3 : tregister);
  714. begin
  715. inherited create(op);
  716. init(_size);
  717. ops:=3;
  718. loadconst(0,_op1);
  719. loadref(1,_op2);
  720. loadreg(2,_op3);
  721. end;
  722. constructor taicpu.op_const_reg_ref(op : tasmop;_size : topsize;_op1 : aint;_op2 : tregister;const _op3 : treference);
  723. begin
  724. inherited create(op);
  725. init(_size);
  726. ops:=3;
  727. loadconst(0,_op1);
  728. loadreg(1,_op2);
  729. loadref(2,_op3);
  730. end;
  731. constructor taicpu.op_cond_sym(op : tasmop;cond:TAsmCond;_size : topsize;_op1 : tasmsymbol);
  732. begin
  733. inherited create(op);
  734. init(_size);
  735. condition:=cond;
  736. ops:=1;
  737. loadsymbol(0,_op1,0);
  738. end;
  739. constructor taicpu.op_sym(op : tasmop;_size : topsize;_op1 : tasmsymbol);
  740. begin
  741. inherited create(op);
  742. init(_size);
  743. ops:=1;
  744. loadsymbol(0,_op1,0);
  745. end;
  746. constructor taicpu.op_sym_ofs(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint);
  747. begin
  748. inherited create(op);
  749. init(_size);
  750. ops:=1;
  751. loadsymbol(0,_op1,_op1ofs);
  752. end;
  753. constructor taicpu.op_sym_ofs_reg(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;_op2 : tregister);
  754. begin
  755. inherited create(op);
  756. init(_size);
  757. ops:=2;
  758. loadsymbol(0,_op1,_op1ofs);
  759. loadreg(1,_op2);
  760. end;
  761. constructor taicpu.op_sym_ofs_ref(op : tasmop;_size : topsize;_op1 : tasmsymbol;_op1ofs:longint;const _op2 : treference);
  762. begin
  763. inherited create(op);
  764. init(_size);
  765. ops:=2;
  766. loadsymbol(0,_op1,_op1ofs);
  767. loadref(1,_op2);
  768. end;
  769. function taicpu.GetString:string;
  770. var
  771. i : longint;
  772. s : string;
  773. addsize : boolean;
  774. begin
  775. s:='['+std_op2str[opcode];
  776. for i:=0 to ops-1 do
  777. begin
  778. with oper[i]^ do
  779. begin
  780. if i=0 then
  781. s:=s+' '
  782. else
  783. s:=s+',';
  784. { type }
  785. addsize:=false;
  786. if (ot and OT_XMMREG)=OT_XMMREG then
  787. s:=s+'xmmreg'
  788. else
  789. if (ot and OT_YMMREG)=OT_YMMREG then
  790. s:=s+'ymmreg'
  791. else
  792. if (ot and OT_MMXREG)=OT_MMXREG then
  793. s:=s+'mmxreg'
  794. else
  795. if (ot and OT_FPUREG)=OT_FPUREG then
  796. s:=s+'fpureg'
  797. else
  798. if (ot and OT_REGISTER)=OT_REGISTER then
  799. begin
  800. s:=s+'reg';
  801. addsize:=true;
  802. end
  803. else
  804. if (ot and OT_IMMEDIATE)=OT_IMMEDIATE then
  805. begin
  806. s:=s+'imm';
  807. addsize:=true;
  808. end
  809. else
  810. if (ot and OT_MEMORY)=OT_MEMORY then
  811. begin
  812. s:=s+'mem';
  813. addsize:=true;
  814. end
  815. else
  816. s:=s+'???';
  817. { size }
  818. if addsize then
  819. begin
  820. if (ot and OT_BITS8)<>0 then
  821. s:=s+'8'
  822. else
  823. if (ot and OT_BITS16)<>0 then
  824. s:=s+'16'
  825. else
  826. if (ot and OT_BITS32)<>0 then
  827. s:=s+'32'
  828. else
  829. if (ot and OT_BITS64)<>0 then
  830. s:=s+'64'
  831. else
  832. if (ot and OT_BITS128)<>0 then
  833. s:=s+'128'
  834. else
  835. if (ot and OT_BITS256)<>0 then
  836. s:=s+'256'
  837. else
  838. s:=s+'??';
  839. { signed }
  840. if (ot and OT_SIGNED)<>0 then
  841. s:=s+'s';
  842. end;
  843. end;
  844. end;
  845. GetString:=s+']';
  846. end;
  847. procedure taicpu.Swapoperands;
  848. var
  849. p : POper;
  850. begin
  851. { Fix the operands which are in AT&T style and we need them in Intel style }
  852. case ops of
  853. 0,1:
  854. ;
  855. 2 : begin
  856. { 0,1 -> 1,0 }
  857. p:=oper[0];
  858. oper[0]:=oper[1];
  859. oper[1]:=p;
  860. end;
  861. 3 : begin
  862. { 0,1,2 -> 2,1,0 }
  863. p:=oper[0];
  864. oper[0]:=oper[2];
  865. oper[2]:=p;
  866. end;
  867. 4 : begin
  868. { 0,1,2,3 -> 3,2,1,0 }
  869. p:=oper[0];
  870. oper[0]:=oper[3];
  871. oper[3]:=p;
  872. p:=oper[1];
  873. oper[1]:=oper[2];
  874. oper[2]:=p;
  875. end;
  876. else
  877. internalerror(201108141);
  878. end;
  879. end;
  880. procedure taicpu.SetOperandOrder(order:TOperandOrder);
  881. begin
  882. if FOperandOrder<>order then
  883. begin
  884. Swapoperands;
  885. FOperandOrder:=order;
  886. end;
  887. end;
  888. function taicpu.FixNonCommutativeOpcodes: tasmop;
  889. begin
  890. result:=opcode;
  891. { we need ATT order }
  892. SetOperandOrder(op_att);
  893. if (
  894. (ops=2) and
  895. (oper[0]^.typ=top_reg) and
  896. (oper[1]^.typ=top_reg) and
  897. { if the first is ST and the second is also a register
  898. it is necessarily ST1 .. ST7 }
  899. ((oper[0]^.reg=NR_ST) or
  900. (oper[0]^.reg=NR_ST0))
  901. ) or
  902. { ((ops=1) and
  903. (oper[0]^.typ=top_reg) and
  904. (oper[0]^.reg in [R_ST1..R_ST7])) or}
  905. (ops=0) then
  906. begin
  907. if opcode=A_FSUBR then
  908. result:=A_FSUB
  909. else if opcode=A_FSUB then
  910. result:=A_FSUBR
  911. else if opcode=A_FDIVR then
  912. result:=A_FDIV
  913. else if opcode=A_FDIV then
  914. result:=A_FDIVR
  915. else if opcode=A_FSUBRP then
  916. result:=A_FSUBP
  917. else if opcode=A_FSUBP then
  918. result:=A_FSUBRP
  919. else if opcode=A_FDIVRP then
  920. result:=A_FDIVP
  921. else if opcode=A_FDIVP then
  922. result:=A_FDIVRP;
  923. end;
  924. if (
  925. (ops=1) and
  926. (oper[0]^.typ=top_reg) and
  927. (getregtype(oper[0]^.reg)=R_FPUREGISTER) and
  928. (oper[0]^.reg<>NR_ST)
  929. ) then
  930. begin
  931. if opcode=A_FSUBRP then
  932. result:=A_FSUBP
  933. else if opcode=A_FSUBP then
  934. result:=A_FSUBRP
  935. else if opcode=A_FDIVRP then
  936. result:=A_FDIVP
  937. else if opcode=A_FDIVP then
  938. result:=A_FDIVRP;
  939. end;
  940. end;
  941. {*****************************************************************************
  942. Assembler
  943. *****************************************************************************}
  944. type
  945. ea = packed record
  946. sib_present : boolean;
  947. bytes : byte;
  948. size : byte;
  949. modrm : byte;
  950. sib : byte;
  951. {$ifdef x86_64}
  952. rex : byte;
  953. {$endif x86_64}
  954. end;
  955. procedure taicpu.create_ot(objdata:TObjData);
  956. {
  957. this function will also fix some other fields which only needs to be once
  958. }
  959. var
  960. i,l,relsize : longint;
  961. currsym : TObjSymbol;
  962. begin
  963. if ops=0 then
  964. exit;
  965. { update oper[].ot field }
  966. for i:=0 to ops-1 do
  967. with oper[i]^ do
  968. begin
  969. case typ of
  970. top_reg :
  971. begin
  972. ot:=reg_ot_table[findreg_by_number(reg)];
  973. end;
  974. top_ref :
  975. begin
  976. if (ref^.refaddr=addr_no)
  977. {$ifdef i386}
  978. or (
  979. (ref^.refaddr in [addr_pic]) and
  980. { allow any base for assembler blocks }
  981. ((assigned(current_procinfo) and
  982. (pi_has_assembler_block in current_procinfo.flags) and
  983. (ref^.base<>NR_NO)) or (ref^.base=NR_EBX))
  984. )
  985. {$endif i386}
  986. {$ifdef x86_64}
  987. or (
  988. (ref^.refaddr in [addr_pic,addr_pic_no_got]) and
  989. (ref^.base<>NR_NO)
  990. )
  991. {$endif x86_64}
  992. then
  993. begin
  994. { create ot field }
  995. if (ot and OT_SIZE_MASK)=0 then
  996. ot:=OT_MEMORY_ANY or opsize_2_type[i,opsize]
  997. else
  998. ot:=OT_MEMORY_ANY or (ot and OT_SIZE_MASK);
  999. if (ref^.base=NR_NO) and (ref^.index=NR_NO) then
  1000. ot:=ot or OT_MEM_OFFS;
  1001. { fix scalefactor }
  1002. if (ref^.index=NR_NO) then
  1003. ref^.scalefactor:=0
  1004. else
  1005. if (ref^.scalefactor=0) then
  1006. ref^.scalefactor:=1;
  1007. end
  1008. else
  1009. begin
  1010. { Jumps use a relative offset which can be 8bit,
  1011. for other opcodes we always need to generate the full
  1012. 32bit address }
  1013. if assigned(objdata) and
  1014. is_jmp then
  1015. begin
  1016. currsym:=objdata.symbolref(ref^.symbol);
  1017. l:=ref^.offset;
  1018. {$push}
  1019. {$r-}
  1020. if assigned(currsym) then
  1021. inc(l,currsym.address);
  1022. {$pop}
  1023. { when it is a forward jump we need to compensate the
  1024. offset of the instruction since the previous time,
  1025. because the symbol address is then still using the
  1026. 'old-style' addressing.
  1027. For backwards jumps this is not required because the
  1028. address of the symbol is already adjusted to the
  1029. new offset }
  1030. if (l>InsOffset) and (LastInsOffset<>-1) then
  1031. inc(l,InsOffset-LastInsOffset);
  1032. { instruction size will then always become 2 (PFV) }
  1033. relsize:=(InsOffset+2)-l;
  1034. if (relsize>=-128) and (relsize<=127) and
  1035. (
  1036. not assigned(currsym) or
  1037. (currsym.objsection=objdata.currobjsec)
  1038. ) then
  1039. ot:=OT_IMM8 or OT_SHORT
  1040. else
  1041. ot:=OT_IMM32 or OT_NEAR;
  1042. end
  1043. else
  1044. ot:=OT_IMM32 or OT_NEAR;
  1045. end;
  1046. end;
  1047. top_local :
  1048. begin
  1049. if (ot and OT_SIZE_MASK)=0 then
  1050. ot:=OT_MEMORY or opsize_2_type[i,opsize]
  1051. else
  1052. ot:=OT_MEMORY or (ot and OT_SIZE_MASK);
  1053. end;
  1054. top_const :
  1055. begin
  1056. // if opcode is a SSE or AVX-instruction then we need a
  1057. // special handling (opsize can different from const-size)
  1058. // (e.g. "pextrw reg/m16, xmmreg, imm8" =>> opsize (16 bit), const-size (8 bit)
  1059. if (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) and
  1060. (not(InsTabMemRefSizeInfoCache^[opcode].ConstSize in [csiMultiple, csiUnkown])) then
  1061. begin
  1062. case InsTabMemRefSizeInfoCache^[opcode].ConstSize of
  1063. csiNoSize: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE;
  1064. csiMem8: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS8;
  1065. csiMem16: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS16;
  1066. csiMem32: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS32;
  1067. csiMem64: ot := ot and (not(OT_SIZE_MASK)) or OT_IMMEDIATE or OT_BITS64;
  1068. end;
  1069. end
  1070. else
  1071. begin
  1072. { allow 2nd, 3rd or 4th operand being a constant and expect no size for shuf* etc. }
  1073. { further, allow AAD and AAM with imm. operand }
  1074. if (opsize=S_NO) and not((i in [1,2,3]) or ((i=0) and (opcode in [A_AAD,A_AAM]))) then
  1075. message(asmr_e_invalid_opcode_and_operand);
  1076. if (opsize<>S_W) and (aint(val)>=-128) and (val<=127) then
  1077. ot:=OT_IMM8 or OT_SIGNED
  1078. else
  1079. ot:=OT_IMMEDIATE or opsize_2_type[i,opsize];
  1080. if (val=1) and (i=1) then
  1081. ot := ot or OT_ONENESS;
  1082. end;
  1083. end;
  1084. top_none :
  1085. begin
  1086. { generated when there was an error in the
  1087. assembler reader. It never happends when generating
  1088. assembler }
  1089. end;
  1090. else
  1091. internalerror(200402261);
  1092. end;
  1093. end;
  1094. end;
  1095. function taicpu.InsEnd:longint;
  1096. begin
  1097. InsEnd:=InsOffset+InsSize;
  1098. end;
  1099. function taicpu.Matches(p:PInsEntry):boolean;
  1100. { * IF_SM stands for Size Match: any operand whose size is not
  1101. * explicitly specified by the template is `really' intended to be
  1102. * the same size as the first size-specified operand.
  1103. * Non-specification is tolerated in the input instruction, but
  1104. * _wrong_ specification is not.
  1105. *
  1106. * IF_SM2 invokes Size Match on only the first _two_ operands, for
  1107. * three-operand instructions such as SHLD: it implies that the
  1108. * first two operands must match in size, but that the third is
  1109. * required to be _unspecified_.
  1110. *
  1111. * IF_SB invokes Size Byte: operands with unspecified size in the
  1112. * template are really bytes, and so no non-byte specification in
  1113. * the input instruction will be tolerated. IF_SW similarly invokes
  1114. * Size Word, and IF_SD invokes Size Doubleword.
  1115. *
  1116. * (The default state if neither IF_SM nor IF_SM2 is specified is
  1117. * that any operand with unspecified size in the template is
  1118. * required to have unspecified size in the instruction too...)
  1119. }
  1120. var
  1121. insot,
  1122. currot,
  1123. i,j,asize,oprs : longint;
  1124. insflags:cardinal;
  1125. siz : array[0..max_operands-1] of longint;
  1126. begin
  1127. result:=false;
  1128. { Check the opcode and operands }
  1129. if (p^.opcode<>opcode) or (p^.ops<>ops) then
  1130. exit;
  1131. for i:=0 to p^.ops-1 do
  1132. begin
  1133. insot:=p^.optypes[i];
  1134. currot:=oper[i]^.ot;
  1135. { Check the operand flags }
  1136. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1137. exit;
  1138. { Check if the passed operand size matches with one of
  1139. the supported operand sizes }
  1140. if ((insot and OT_SIZE_MASK)<>0) and
  1141. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1142. exit;
  1143. end;
  1144. { Check operand sizes }
  1145. insflags:=p^.flags;
  1146. if insflags and IF_SMASK<>0 then
  1147. begin
  1148. { as default an untyped size can get all the sizes, this is different
  1149. from nasm, but else we need to do a lot checking which opcodes want
  1150. size or not with the automatic size generation }
  1151. asize:=-1;
  1152. if (insflags and IF_SB)<>0 then
  1153. asize:=OT_BITS8
  1154. else if (insflags and IF_SW)<>0 then
  1155. asize:=OT_BITS16
  1156. else if (insflags and IF_SD)<>0 then
  1157. asize:=OT_BITS32;
  1158. if (insflags and IF_ARMASK)<>0 then
  1159. begin
  1160. siz[0]:=-1;
  1161. siz[1]:=-1;
  1162. siz[2]:=-1;
  1163. siz[((insflags and IF_ARMASK) shr IF_ARSHIFT)-1]:=asize;
  1164. end
  1165. else
  1166. begin
  1167. siz[0]:=asize;
  1168. siz[1]:=asize;
  1169. siz[2]:=asize;
  1170. end;
  1171. if (insflags and (IF_SM or IF_SM2))<>0 then
  1172. begin
  1173. if (insflags and IF_SM2)<>0 then
  1174. oprs:=2
  1175. else
  1176. oprs:=p^.ops;
  1177. for i:=0 to oprs-1 do
  1178. if ((p^.optypes[i] and OT_SIZE_MASK) <> 0) then
  1179. begin
  1180. for j:=0 to oprs-1 do
  1181. siz[j]:=p^.optypes[i] and OT_SIZE_MASK;
  1182. break;
  1183. end;
  1184. end
  1185. else
  1186. oprs:=2;
  1187. { Check operand sizes }
  1188. for i:=0 to p^.ops-1 do
  1189. begin
  1190. insot:=p^.optypes[i];
  1191. currot:=oper[i]^.ot;
  1192. if ((insot and OT_SIZE_MASK)=0) and
  1193. ((currot and OT_SIZE_MASK and (not siz[i]))<>0) and
  1194. { Immediates can always include smaller size }
  1195. ((currot and OT_IMMEDIATE)=0) and
  1196. (((insot and OT_SIZE_MASK) or siz[i])<(currot and OT_SIZE_MASK)) then
  1197. exit;
  1198. end;
  1199. end;
  1200. if (InsTabMemRefSizeInfoCache^[opcode].MemRefSize in MemRefMultiples) and
  1201. (InsTabMemRefSizeInfoCache^[opcode].ExistsSSEAVX) then
  1202. begin
  1203. for i:=0 to p^.ops-1 do
  1204. begin
  1205. insot:=p^.optypes[i];
  1206. if ((insot and OT_XMMRM) = OT_XMMRM) OR
  1207. ((insot and OT_YMMRM) = OT_YMMRM) then
  1208. begin
  1209. if (insot and OT_SIZE_MASK) = 0 then
  1210. begin
  1211. case insot and (OT_XMMRM or OT_YMMRM) of
  1212. OT_XMMRM: insot := insot or OT_BITS128;
  1213. OT_YMMRM: insot := insot or OT_BITS256;
  1214. end;
  1215. end;
  1216. end;
  1217. currot:=oper[i]^.ot;
  1218. { Check the operand flags }
  1219. if (insot and (not currot) and OT_NON_SIZE)<>0 then
  1220. exit;
  1221. { Check if the passed operand size matches with one of
  1222. the supported operand sizes }
  1223. if ((insot and OT_SIZE_MASK)<>0) and
  1224. ((insot and currot and OT_SIZE_MASK)<>(currot and OT_SIZE_MASK)) then
  1225. exit;
  1226. end;
  1227. end;
  1228. result:=true;
  1229. end;
  1230. procedure taicpu.ResetPass1;
  1231. begin
  1232. { we need to reset everything here, because the choosen insentry
  1233. can be invalid for a new situation where the previously optimized
  1234. insentry is not correct }
  1235. InsEntry:=nil;
  1236. InsSize:=0;
  1237. LastInsOffset:=-1;
  1238. end;
  1239. procedure taicpu.ResetPass2;
  1240. begin
  1241. { we are here in a second pass, check if the instruction can be optimized }
  1242. if assigned(InsEntry) and
  1243. ((InsEntry^.flags and IF_PASS2)<>0) then
  1244. begin
  1245. InsEntry:=nil;
  1246. InsSize:=0;
  1247. end;
  1248. LastInsOffset:=-1;
  1249. end;
  1250. function taicpu.CheckIfValid:boolean;
  1251. begin
  1252. result:=FindInsEntry(nil);
  1253. end;
  1254. function taicpu.FindInsentry(objdata:TObjData):boolean;
  1255. var
  1256. i : longint;
  1257. begin
  1258. result:=false;
  1259. { Things which may only be done once, not when a second pass is done to
  1260. optimize }
  1261. if (Insentry=nil) or ((InsEntry^.flags and IF_PASS2)<>0) then
  1262. begin
  1263. current_filepos:=fileinfo;
  1264. { We need intel style operands }
  1265. SetOperandOrder(op_intel);
  1266. { create the .ot fields }
  1267. create_ot(objdata);
  1268. { set the file postion }
  1269. end
  1270. else
  1271. begin
  1272. { we've already an insentry so it's valid }
  1273. result:=true;
  1274. exit;
  1275. end;
  1276. { Lookup opcode in the table }
  1277. InsSize:=-1;
  1278. i:=instabcache^[opcode];
  1279. if i=-1 then
  1280. begin
  1281. Message1(asmw_e_opcode_not_in_table,gas_op2str[opcode]);
  1282. exit;
  1283. end;
  1284. insentry:=@instab[i];
  1285. while (insentry^.opcode=opcode) do
  1286. begin
  1287. if matches(insentry) then
  1288. begin
  1289. result:=true;
  1290. exit;
  1291. end;
  1292. inc(insentry);
  1293. end;
  1294. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  1295. { No instruction found, set insentry to nil and inssize to -1 }
  1296. insentry:=nil;
  1297. inssize:=-1;
  1298. end;
  1299. function taicpu.Pass1(objdata:TObjData):longint;
  1300. begin
  1301. Pass1:=0;
  1302. { Save the old offset and set the new offset }
  1303. InsOffset:=ObjData.CurrObjSec.Size;
  1304. { Error? }
  1305. if (Insentry=nil) and (InsSize=-1) then
  1306. exit;
  1307. { set the file postion }
  1308. current_filepos:=fileinfo;
  1309. { Get InsEntry }
  1310. if FindInsEntry(ObjData) then
  1311. begin
  1312. { Calculate instruction size }
  1313. InsSize:=calcsize(insentry);
  1314. if segprefix<>NR_NO then
  1315. inc(InsSize);
  1316. { Fix opsize if size if forced }
  1317. if (insentry^.flags and (IF_SB or IF_SW or IF_SD))<>0 then
  1318. begin
  1319. if (insentry^.flags and IF_ARMASK)=0 then
  1320. begin
  1321. if (insentry^.flags and IF_SB)<>0 then
  1322. begin
  1323. if opsize=S_NO then
  1324. opsize:=S_B;
  1325. end
  1326. else if (insentry^.flags and IF_SW)<>0 then
  1327. begin
  1328. if opsize=S_NO then
  1329. opsize:=S_W;
  1330. end
  1331. else if (insentry^.flags and IF_SD)<>0 then
  1332. begin
  1333. if opsize=S_NO then
  1334. opsize:=S_L;
  1335. end;
  1336. end;
  1337. end;
  1338. LastInsOffset:=InsOffset;
  1339. Pass1:=InsSize;
  1340. exit;
  1341. end;
  1342. LastInsOffset:=-1;
  1343. end;
  1344. const
  1345. segprefixes: array[NR_ES..NR_GS] of Byte=(
  1346. // es cs ss ds fs gs
  1347. $26, $2E, $36, $3E, $64, $65
  1348. );
  1349. procedure taicpu.Pass2(objdata:TObjData);
  1350. begin
  1351. { error in pass1 ? }
  1352. if insentry=nil then
  1353. exit;
  1354. current_filepos:=fileinfo;
  1355. { Segment override }
  1356. if (segprefix>=NR_ES) and (segprefix<=NR_GS) then
  1357. begin
  1358. objdata.writebytes(segprefixes[segprefix],1);
  1359. { fix the offset for GenNode }
  1360. inc(InsOffset);
  1361. end
  1362. else if segprefix<>NR_NO then
  1363. InternalError(201001071);
  1364. { Generate the instruction }
  1365. GenCode(objdata);
  1366. end;
  1367. function taicpu.needaddrprefix(opidx:byte):boolean;
  1368. begin
  1369. result:=(oper[opidx]^.typ=top_ref) and
  1370. (oper[opidx]^.ref^.refaddr=addr_no) and
  1371. {$ifdef x86_64}
  1372. (oper[opidx]^.ref^.base<>NR_RIP) and
  1373. {$endif x86_64}
  1374. (
  1375. (
  1376. (oper[opidx]^.ref^.index<>NR_NO) and
  1377. (getsubreg(oper[opidx]^.ref^.index)<>R_SUBADDR)
  1378. ) or
  1379. (
  1380. (oper[opidx]^.ref^.base<>NR_NO) and
  1381. (getsubreg(oper[opidx]^.ref^.base)<>R_SUBADDR)
  1382. )
  1383. );
  1384. end;
  1385. procedure badreg(r:Tregister);
  1386. begin
  1387. Message1(asmw_e_invalid_register,generic_regname(r));
  1388. end;
  1389. function regval(r:Tregister):byte;
  1390. const
  1391. intsupreg2opcode: array[0..7] of byte=
  1392. // ax cx dx bx si di bp sp -- in x86reg.dat
  1393. // ax cx dx bx sp bp si di -- needed order
  1394. (0, 1, 2, 3, 6, 7, 5, 4);
  1395. maxsupreg: array[tregistertype] of tsuperregister=
  1396. {$ifdef x86_64}
  1397. (0, 16, 9, 8, 16, 32, 0);
  1398. {$else x86_64}
  1399. (0, 8, 9, 8, 8, 32, 0);
  1400. {$endif x86_64}
  1401. var
  1402. rs: tsuperregister;
  1403. rt: tregistertype;
  1404. begin
  1405. rs:=getsupreg(r);
  1406. rt:=getregtype(r);
  1407. if (rs>=maxsupreg[rt]) then
  1408. badreg(r);
  1409. result:=rs and 7;
  1410. if (rt=R_INTREGISTER) then
  1411. begin
  1412. if (rs<8) then
  1413. result:=intsupreg2opcode[rs];
  1414. if getsubreg(r)=R_SUBH then
  1415. inc(result,4);
  1416. end;
  1417. end;
  1418. {$ifdef x86_64}
  1419. function rexbits(r: tregister): byte;
  1420. begin
  1421. result:=0;
  1422. case getregtype(r) of
  1423. R_INTREGISTER:
  1424. if (getsupreg(r)>=RS_R8) then
  1425. { Either B,X or R bits can be set, depending on register role in instruction.
  1426. Set all three bits here, caller will discard unnecessary ones. }
  1427. result:=result or $47
  1428. else if (getsubreg(r)=R_SUBL) and
  1429. (getsupreg(r) in [RS_RDI,RS_RSI,RS_RBP,RS_RSP]) then
  1430. result:=result or $40
  1431. else if (getsubreg(r)=R_SUBH) then
  1432. { Not an actual REX bit, used to detect incompatible usage of
  1433. AH/BH/CH/DH }
  1434. result:=result or $80;
  1435. R_MMREGISTER:
  1436. if getsupreg(r)>=RS_XMM8 then
  1437. result:=result or $47;
  1438. end;
  1439. end;
  1440. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1441. var
  1442. sym : tasmsymbol;
  1443. md,s,rv : byte;
  1444. base,index,scalefactor,
  1445. o : longint;
  1446. ir,br : Tregister;
  1447. isub,bsub : tsubregister;
  1448. begin
  1449. process_ea:=false;
  1450. fillchar(output,sizeof(output),0);
  1451. {Register ?}
  1452. if (input.typ=top_reg) then
  1453. begin
  1454. rv:=regval(input.reg);
  1455. output.modrm:=$c0 or (rfield shl 3) or rv;
  1456. output.size:=1;
  1457. output.rex:=output.rex or (rexbits(input.reg) and $F1);
  1458. process_ea:=true;
  1459. exit;
  1460. end;
  1461. {No register, so memory reference.}
  1462. if input.typ<>top_ref then
  1463. internalerror(200409263);
  1464. ir:=input.ref^.index;
  1465. br:=input.ref^.base;
  1466. isub:=getsubreg(ir);
  1467. bsub:=getsubreg(br);
  1468. s:=input.ref^.scalefactor;
  1469. o:=input.ref^.offset;
  1470. sym:=input.ref^.symbol;
  1471. if ((ir<>NR_NO) and (getregtype(ir)<>R_INTREGISTER)) or
  1472. ((br<>NR_NO) and (br<>NR_RIP) and (getregtype(br)<>R_INTREGISTER)) then
  1473. internalerror(200301081);
  1474. { it's direct address }
  1475. if (br=NR_NO) and (ir=NR_NO) then
  1476. begin
  1477. output.sib_present:=true;
  1478. output.bytes:=4;
  1479. output.modrm:=4 or (rfield shl 3);
  1480. output.sib:=$25;
  1481. end
  1482. else if (br=NR_RIP) and (ir=NR_NO) then
  1483. begin
  1484. { rip based }
  1485. output.sib_present:=false;
  1486. output.bytes:=4;
  1487. output.modrm:=5 or (rfield shl 3);
  1488. end
  1489. else
  1490. { it's an indirection }
  1491. begin
  1492. { 16 bit or 32 bit address? }
  1493. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1494. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1495. message(asmw_e_16bit_32bit_not_supported);
  1496. { wrong, for various reasons }
  1497. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1498. exit;
  1499. output.rex:=output.rex or (rexbits(br) and $F1) or (rexbits(ir) and $F2);
  1500. process_ea:=true;
  1501. { base }
  1502. case br of
  1503. NR_R8,
  1504. NR_RAX : base:=0;
  1505. NR_R9,
  1506. NR_RCX : base:=1;
  1507. NR_R10,
  1508. NR_RDX : base:=2;
  1509. NR_R11,
  1510. NR_RBX : base:=3;
  1511. NR_R12,
  1512. NR_RSP : base:=4;
  1513. NR_R13,
  1514. NR_NO,
  1515. NR_RBP : base:=5;
  1516. NR_R14,
  1517. NR_RSI : base:=6;
  1518. NR_R15,
  1519. NR_RDI : base:=7;
  1520. else
  1521. exit;
  1522. end;
  1523. { index }
  1524. case ir of
  1525. NR_R8,
  1526. NR_RAX : index:=0;
  1527. NR_R9,
  1528. NR_RCX : index:=1;
  1529. NR_R10,
  1530. NR_RDX : index:=2;
  1531. NR_R11,
  1532. NR_RBX : index:=3;
  1533. NR_R12,
  1534. NR_NO : index:=4;
  1535. NR_R13,
  1536. NR_RBP : index:=5;
  1537. NR_R14,
  1538. NR_RSI : index:=6;
  1539. NR_R15,
  1540. NR_RDI : index:=7;
  1541. else
  1542. exit;
  1543. end;
  1544. case s of
  1545. 0,
  1546. 1 : scalefactor:=0;
  1547. 2 : scalefactor:=1;
  1548. 4 : scalefactor:=2;
  1549. 8 : scalefactor:=3;
  1550. else
  1551. exit;
  1552. end;
  1553. { If rbp or r13 is used we must always include an offset }
  1554. if (br=NR_NO) or
  1555. ((br<>NR_RBP) and (br<>NR_R13) and (o=0) and (sym=nil)) then
  1556. md:=0
  1557. else
  1558. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1559. md:=1
  1560. else
  1561. md:=2;
  1562. if (br=NR_NO) or (md=2) then
  1563. output.bytes:=4
  1564. else
  1565. output.bytes:=md;
  1566. { SIB needed ? }
  1567. if (ir=NR_NO) and (br<>NR_RSP) and (br<>NR_R12) then
  1568. begin
  1569. output.sib_present:=false;
  1570. output.modrm:=(md shl 6) or (rfield shl 3) or base;
  1571. end
  1572. else
  1573. begin
  1574. output.sib_present:=true;
  1575. output.modrm:=(md shl 6) or (rfield shl 3) or 4;
  1576. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1577. end;
  1578. end;
  1579. output.size:=1+ord(output.sib_present)+output.bytes;
  1580. process_ea:=true;
  1581. end;
  1582. {$else x86_64}
  1583. function process_ea(const input:toper;out output:ea;rfield:longint):boolean;
  1584. var
  1585. sym : tasmsymbol;
  1586. md,s,rv : byte;
  1587. base,index,scalefactor,
  1588. o : longint;
  1589. ir,br : Tregister;
  1590. isub,bsub : tsubregister;
  1591. begin
  1592. process_ea:=false;
  1593. fillchar(output,sizeof(output),0);
  1594. {Register ?}
  1595. if (input.typ=top_reg) then
  1596. begin
  1597. rv:=regval(input.reg);
  1598. output.modrm:=$c0 or (rfield shl 3) or rv;
  1599. output.size:=1;
  1600. process_ea:=true;
  1601. exit;
  1602. end;
  1603. {No register, so memory reference.}
  1604. if (input.typ<>top_ref) then
  1605. internalerror(200409262);
  1606. if ((input.ref^.index<>NR_NO) and (getregtype(input.ref^.index)<>R_INTREGISTER)) or
  1607. ((input.ref^.base<>NR_NO) and (getregtype(input.ref^.base)<>R_INTREGISTER)) then
  1608. internalerror(200301081);
  1609. ir:=input.ref^.index;
  1610. br:=input.ref^.base;
  1611. isub:=getsubreg(ir);
  1612. bsub:=getsubreg(br);
  1613. s:=input.ref^.scalefactor;
  1614. o:=input.ref^.offset;
  1615. sym:=input.ref^.symbol;
  1616. { it's direct address }
  1617. if (br=NR_NO) and (ir=NR_NO) then
  1618. begin
  1619. { it's a pure offset }
  1620. output.sib_present:=false;
  1621. output.bytes:=4;
  1622. output.modrm:=5 or (rfield shl 3);
  1623. end
  1624. else
  1625. { it's an indirection }
  1626. begin
  1627. { 16 bit address? }
  1628. if ((ir<>NR_NO) and (isub<>R_SUBADDR)) or
  1629. ((br<>NR_NO) and (bsub<>R_SUBADDR)) then
  1630. message(asmw_e_16bit_not_supported);
  1631. {$ifdef OPTEA}
  1632. { make single reg base }
  1633. if (br=NR_NO) and (s=1) then
  1634. begin
  1635. br:=ir;
  1636. ir:=NR_NO;
  1637. end;
  1638. { convert [3,5,9]*EAX to EAX+[2,4,8]*EAX }
  1639. if (br=NR_NO) and
  1640. (((s=2) and (ir<>NR_ESP)) or
  1641. (s=3) or (s=5) or (s=9)) then
  1642. begin
  1643. br:=ir;
  1644. dec(s);
  1645. end;
  1646. { swap ESP into base if scalefactor is 1 }
  1647. if (s=1) and (ir=NR_ESP) then
  1648. begin
  1649. ir:=br;
  1650. br:=NR_ESP;
  1651. end;
  1652. {$endif OPTEA}
  1653. { wrong, for various reasons }
  1654. if (ir=NR_ESP) or ((s<>1) and (s<>2) and (s<>4) and (s<>8) and (ir<>NR_NO)) then
  1655. exit;
  1656. { base }
  1657. case br of
  1658. NR_EAX : base:=0;
  1659. NR_ECX : base:=1;
  1660. NR_EDX : base:=2;
  1661. NR_EBX : base:=3;
  1662. NR_ESP : base:=4;
  1663. NR_NO,
  1664. NR_EBP : base:=5;
  1665. NR_ESI : base:=6;
  1666. NR_EDI : base:=7;
  1667. else
  1668. exit;
  1669. end;
  1670. { index }
  1671. case ir of
  1672. NR_EAX : index:=0;
  1673. NR_ECX : index:=1;
  1674. NR_EDX : index:=2;
  1675. NR_EBX : index:=3;
  1676. NR_NO : index:=4;
  1677. NR_EBP : index:=5;
  1678. NR_ESI : index:=6;
  1679. NR_EDI : index:=7;
  1680. else
  1681. exit;
  1682. end;
  1683. case s of
  1684. 0,
  1685. 1 : scalefactor:=0;
  1686. 2 : scalefactor:=1;
  1687. 4 : scalefactor:=2;
  1688. 8 : scalefactor:=3;
  1689. else
  1690. exit;
  1691. end;
  1692. if (br=NR_NO) or
  1693. ((br<>NR_EBP) and (o=0) and (sym=nil)) then
  1694. md:=0
  1695. else
  1696. if ((o>=-128) and (o<=127) and (sym=nil)) then
  1697. md:=1
  1698. else
  1699. md:=2;
  1700. if (br=NR_NO) or (md=2) then
  1701. output.bytes:=4
  1702. else
  1703. output.bytes:=md;
  1704. { SIB needed ? }
  1705. if (ir=NR_NO) and (br<>NR_ESP) then
  1706. begin
  1707. output.sib_present:=false;
  1708. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or base;
  1709. end
  1710. else
  1711. begin
  1712. output.sib_present:=true;
  1713. output.modrm:=(longint(md) shl 6) or (rfield shl 3) or 4;
  1714. output.sib:=(scalefactor shl 6) or (index shl 3) or base;
  1715. end;
  1716. end;
  1717. if output.sib_present then
  1718. output.size:=2+output.bytes
  1719. else
  1720. output.size:=1+output.bytes;
  1721. process_ea:=true;
  1722. end;
  1723. {$endif x86_64}
  1724. function taicpu.calcsize(p:PInsEntry):shortint;
  1725. var
  1726. codes : pchar;
  1727. c : byte;
  1728. len : shortint;
  1729. ea_data : ea;
  1730. exists_vex: boolean;
  1731. exists_vex_extention: boolean;
  1732. exists_prefix_66: boolean;
  1733. exists_prefix_F2: boolean;
  1734. exists_prefix_F3: boolean;
  1735. {$ifdef x86_64}
  1736. omit_rexw : boolean;
  1737. {$endif x86_64}
  1738. begin
  1739. len:=0;
  1740. codes:=@p^.code[0];
  1741. exists_vex := false;
  1742. exists_vex_extention := false;
  1743. exists_prefix_66 := false;
  1744. exists_prefix_F2 := false;
  1745. exists_prefix_F3 := false;
  1746. {$ifdef x86_64}
  1747. rex:=0;
  1748. omit_rexw:=false;
  1749. {$endif x86_64}
  1750. repeat
  1751. c:=ord(codes^);
  1752. inc(codes);
  1753. case c of
  1754. 0 :
  1755. break;
  1756. 1,2,3 :
  1757. begin
  1758. inc(codes,c);
  1759. inc(len,c);
  1760. end;
  1761. 8,9,10 :
  1762. begin
  1763. {$ifdef x86_64}
  1764. rex:=rex or (rexbits(oper[c-8]^.reg) and $F1);
  1765. {$endif x86_64}
  1766. inc(codes);
  1767. inc(len);
  1768. end;
  1769. 11 :
  1770. begin
  1771. inc(codes);
  1772. inc(len);
  1773. end;
  1774. 4,5,6,7 :
  1775. begin
  1776. if opsize=S_W then
  1777. inc(len,2)
  1778. else
  1779. inc(len);
  1780. end;
  1781. 12,13,14,
  1782. 16,17,18,
  1783. 20,21,22,23,
  1784. 40,41,42 :
  1785. inc(len);
  1786. 24,25,26,
  1787. 31,
  1788. 48,49,50 :
  1789. inc(len,2);
  1790. 28,29,30:
  1791. begin
  1792. if opsize=S_Q then
  1793. inc(len,8)
  1794. else
  1795. inc(len,4);
  1796. end;
  1797. 36,37,38:
  1798. inc(len,sizeof(pint));
  1799. 44,45,46:
  1800. inc(len,8);
  1801. 32,33,34,
  1802. 52,53,54,
  1803. 56,57,58,
  1804. 172,173,174 :
  1805. inc(len,4);
  1806. 60,61,62,63: ; // ignore vex-coded operand-idx
  1807. 208,209,210 :
  1808. begin
  1809. case (oper[c-208]^.ot and OT_SIZE_MASK) of
  1810. OT_BITS16:
  1811. inc(len);
  1812. {$ifdef x86_64}
  1813. OT_BITS64:
  1814. begin
  1815. rex:=rex or $48;
  1816. end;
  1817. {$endif x86_64}
  1818. end;
  1819. end;
  1820. 200 :
  1821. {$ifndef x86_64}
  1822. inc(len);
  1823. {$else x86_64}
  1824. { every insentry with code 0310 must be marked with NOX86_64 }
  1825. InternalError(2011051301);
  1826. {$endif x86_64}
  1827. 201 :
  1828. {$ifdef x86_64}
  1829. inc(len)
  1830. {$endif x86_64}
  1831. ;
  1832. 212 :
  1833. inc(len);
  1834. 214 :
  1835. begin
  1836. {$ifdef x86_64}
  1837. rex:=rex or $48;
  1838. {$endif x86_64}
  1839. end;
  1840. 202,
  1841. 211,
  1842. 213,
  1843. 215,
  1844. 217,218: ;
  1845. 219:
  1846. begin
  1847. inc(len);
  1848. exists_prefix_F2 := true;
  1849. end;
  1850. 220:
  1851. begin
  1852. inc(len);
  1853. exists_prefix_F3 := true;
  1854. end;
  1855. 241:
  1856. begin
  1857. inc(len);
  1858. exists_prefix_66 := true;
  1859. end;
  1860. 221:
  1861. {$ifdef x86_64}
  1862. omit_rexw:=true
  1863. {$endif x86_64}
  1864. ;
  1865. 64..151 :
  1866. begin
  1867. {$ifdef x86_64}
  1868. if (c<127) then
  1869. begin
  1870. if (oper[c and 7]^.typ=top_reg) then
  1871. begin
  1872. rex:=rex or (rexbits(oper[c and 7]^.reg) and $F4);
  1873. end;
  1874. end;
  1875. {$endif x86_64}
  1876. if not process_ea(oper[(c shr 3) and 7]^, ea_data, 0) then
  1877. Message(asmw_e_invalid_effective_address)
  1878. else
  1879. inc(len,ea_data.size);
  1880. {$ifdef x86_64}
  1881. rex:=rex or ea_data.rex;
  1882. {$endif x86_64}
  1883. end;
  1884. 242: // VEX prefix for AVX (length = 2 or 3 bytes, dependens on REX.XBW or opcode-prefix ($0F38 or $0F3A))
  1885. // =>> DEFAULT = 2 Bytes
  1886. begin
  1887. if not(exists_vex) then
  1888. begin
  1889. inc(len, 2);
  1890. exists_vex := true;
  1891. end;
  1892. end;
  1893. 243: // REX.W = 1
  1894. // =>> VEX prefix length = 3
  1895. begin
  1896. if not(exists_vex_extention) then
  1897. begin
  1898. inc(len);
  1899. exists_vex_extention := true;
  1900. end;
  1901. end;
  1902. 244: ; // VEX length bit
  1903. 247: inc(len); // operand 3 (ymmreg) encoded immediate byte (bit 4-7)
  1904. 248: // VEX-Extention prefix $0F
  1905. // ignore for calculating length
  1906. ;
  1907. 249, // VEX-Extention prefix $0F38
  1908. 250: // VEX-Extention prefix $0F3A
  1909. begin
  1910. if not(exists_vex_extention) then
  1911. begin
  1912. inc(len);
  1913. exists_vex_extention := true;
  1914. end;
  1915. end;
  1916. else
  1917. InternalError(200603141);
  1918. end;
  1919. until false;
  1920. {$ifdef x86_64}
  1921. if ((rex and $80)<>0) and ((rex and $4F)<>0) then
  1922. Message(asmw_e_bad_reg_with_rex);
  1923. rex:=rex and $4F; { reset extra bits in upper nibble }
  1924. if omit_rexw then
  1925. begin
  1926. if rex=$48 then { remove rex entirely? }
  1927. rex:=0
  1928. else
  1929. rex:=rex and $F7;
  1930. end;
  1931. if not(exists_vex) then
  1932. begin
  1933. if rex<>0 then
  1934. Inc(len);
  1935. end;
  1936. {$endif}
  1937. if exists_vex then
  1938. begin
  1939. if exists_prefix_66 then dec(len);
  1940. if exists_prefix_F2 then dec(len);
  1941. if exists_prefix_F3 then dec(len);
  1942. {$ifdef x86_64}
  1943. if not(exists_vex_extention) then
  1944. if rex and $0B <> 0 then inc(len); // REX.WXB <> 0 =>> needed VEX-Extention
  1945. {$endif x86_64}
  1946. end;
  1947. calcsize:=len;
  1948. end;
  1949. procedure taicpu.GenCode(objdata:TObjData);
  1950. {
  1951. * the actual codes (C syntax, i.e. octal):
  1952. * \0 - terminates the code. (Unless it's a literal of course.)
  1953. * \1, \2, \3 - that many literal bytes follow in the code stream
  1954. * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
  1955. * (POP is never used for CS) depending on operand 0
  1956. * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
  1957. * on operand 0
  1958. * \10, \11, \12 - a literal byte follows in the code stream, to be added
  1959. * to the register value of operand 0, 1 or 2
  1960. * \13 - a literal byte follows in the code stream, to be added
  1961. * to the condition code value of the instruction.
  1962. * \14, \15, \16 - a signed byte immediate operand, from operand 0, 1 or 2
  1963. * \20, \21, \22 - a byte immediate operand, from operand 0, 1 or 2
  1964. * \24, \25, \26, \27 - an unsigned byte immediate operand, from operand 0, 1, 2 or 3
  1965. * \30, \31, \32 - a word immediate operand, from operand 0, 1 or 2
  1966. * \34, \35, \36 - select between \3[012] and \4[012] depending on 16/32 bit
  1967. * assembly mode or the address-size override on the operand
  1968. * \37 - a word constant, from the _segment_ part of operand 0
  1969. * \40, \41, \42 - a long immediate operand, from operand 0, 1 or 2
  1970. * \44, \45, \46 - select between \3[012], \4[012] or \5[456] depending
  1971. on the address size of instruction
  1972. * \50, \51, \52 - a byte relative operand, from operand 0, 1 or 2
  1973. * \54, \55, \56 - a qword immediate, from operand 0, 1 or 2
  1974. * \60, \61, \62 - a word relative operand, from operand 0, 1 or 2
  1975. * \64, \65, \66 - select between \6[012] and \7[012] depending on 16/32 bit
  1976. * assembly mode or the address-size override on the operand
  1977. * \70, \71, \72 - a long relative operand, from operand 0, 1 or 2
  1978. * \74, \75, \76 - a vex-coded vector operand, from operand 0, 1 or 2
  1979. * \1ab - a ModRM, calculated on EA in operand a, with the spare
  1980. * field the register value of operand b.
  1981. * \2ab - a ModRM, calculated on EA in operand a, with the spare
  1982. * field equal to digit b.
  1983. * \254,\255,\256 - a signed 32-bit immediate to be extended to 64 bits
  1984. * \300,\301,\302 - might be an 0x67, depending on the address size of
  1985. * the memory reference in operand x.
  1986. * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
  1987. * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
  1988. * \312 - (disassembler only) invalid with non-default address size.
  1989. * \320,\321,\322 - might be an 0x66 or 0x48 byte, depending on the operand
  1990. * size of operand x.
  1991. * \324 - indicates fixed 16-bit operand size, i.e. optional 0x66.
  1992. * \325 - indicates fixed 32-bit operand size, i.e. optional 0x66.
  1993. * \326 - indicates fixed 64-bit operand size, i.e. optional 0x48.
  1994. * \327 - indicates that this instruction is only valid when the
  1995. * operand size is the default (instruction to disassembler,
  1996. * generates no code in the assembler)
  1997. * \331 - instruction not valid with REP prefix. Hint for
  1998. * disassembler only; for SSE instructions.
  1999. * \332 - disassemble a rep (0xF3 byte) prefix as repe not rep.
  2000. * \333 - 0xF3 prefix for SSE instructions
  2001. * \334 - 0xF2 prefix for SSE instructions
  2002. * \335 - Indicates 64-bit operand size with REX.W not necessary
  2003. * \361 - 0x66 prefix for SSE instructions
  2004. * \362 - VEX prefix for AVX instructions
  2005. * \363 - VEX W1
  2006. * \364 - VEX Vector length 256
  2007. * \367 - operand 3 (ymmreg) encoded in bit 4-7 of the immediate byte
  2008. * \370 - VEX 0F-FLAG
  2009. * \371 - VEX 0F38-FLAG
  2010. * \372 - VEX 0F3A-FLAG
  2011. }
  2012. var
  2013. currval : aint;
  2014. currsym : tobjsymbol;
  2015. currrelreloc,
  2016. currabsreloc,
  2017. currabsreloc32 : TObjRelocationType;
  2018. {$ifdef x86_64}
  2019. rexwritten : boolean;
  2020. {$endif x86_64}
  2021. procedure getvalsym(opidx:longint);
  2022. begin
  2023. case oper[opidx]^.typ of
  2024. top_ref :
  2025. begin
  2026. currval:=oper[opidx]^.ref^.offset;
  2027. currsym:=ObjData.symbolref(oper[opidx]^.ref^.symbol);
  2028. {$ifdef i386}
  2029. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2030. (tf_pic_uses_got in target_info.flags) then
  2031. begin
  2032. currrelreloc:=RELOC_PLT32;
  2033. currabsreloc:=RELOC_GOT32;
  2034. currabsreloc32:=RELOC_GOT32;
  2035. end
  2036. else
  2037. {$endif i386}
  2038. {$ifdef x86_64}
  2039. if oper[opidx]^.ref^.refaddr=addr_pic then
  2040. begin
  2041. currrelreloc:=RELOC_PLT32;
  2042. currabsreloc:=RELOC_GOTPCREL;
  2043. currabsreloc32:=RELOC_GOTPCREL;
  2044. end
  2045. else if oper[opidx]^.ref^.refaddr=addr_pic_no_got then
  2046. begin
  2047. currrelreloc:=RELOC_RELATIVE;
  2048. currabsreloc:=RELOC_RELATIVE;
  2049. currabsreloc32:=RELOC_RELATIVE;
  2050. end
  2051. else
  2052. {$endif x86_64}
  2053. begin
  2054. currrelreloc:=RELOC_RELATIVE;
  2055. currabsreloc:=RELOC_ABSOLUTE;
  2056. currabsreloc32:=RELOC_ABSOLUTE32;
  2057. end;
  2058. end;
  2059. top_const :
  2060. begin
  2061. currval:=aint(oper[opidx]^.val);
  2062. currsym:=nil;
  2063. currabsreloc:=RELOC_ABSOLUTE;
  2064. currabsreloc32:=RELOC_ABSOLUTE32;
  2065. end;
  2066. else
  2067. Message(asmw_e_immediate_or_reference_expected);
  2068. end;
  2069. end;
  2070. {$ifdef x86_64}
  2071. procedure maybewriterex;
  2072. begin
  2073. if (rex<>0) and not(rexwritten) then
  2074. begin
  2075. rexwritten:=true;
  2076. objdata.writebytes(rex,1);
  2077. end;
  2078. end;
  2079. {$endif x86_64}
  2080. procedure objdata_writereloc(Data:aint;len:aword;p:TObjSymbol;Reloctype:TObjRelocationType);
  2081. begin
  2082. {$ifdef i386}
  2083. { Special case of '_GLOBAL_OFFSET_TABLE_'
  2084. which needs a special relocation type R_386_GOTPC }
  2085. if assigned (p) and
  2086. (p.name='_GLOBAL_OFFSET_TABLE_') and
  2087. (tf_pic_uses_got in target_info.flags) then
  2088. begin
  2089. { nothing else than a 4 byte relocation should occur
  2090. for GOT }
  2091. if len<>4 then
  2092. Message1(asmw_e_invalid_opcode_and_operands,GetString);
  2093. Reloctype:=RELOC_GOTPC;
  2094. { We need to add the offset of the relocation
  2095. of _GLOBAL_OFFSET_TABLE symbol within
  2096. the current instruction }
  2097. inc(data,objdata.currobjsec.size-insoffset);
  2098. end;
  2099. {$endif i386}
  2100. objdata.writereloc(data,len,p,Reloctype);
  2101. end;
  2102. const
  2103. CondVal:array[TAsmCond] of byte=($0,
  2104. $7, $3, $2, $6, $2, $4, $F, $D, $C, $E, $6, $2,
  2105. $3, $7, $3, $5, $E, $C, $D, $F, $1, $B, $9, $5,
  2106. $0, $A, $A, $B, $8, $4);
  2107. var
  2108. c : byte;
  2109. pb : pbyte;
  2110. codes : pchar;
  2111. bytes : array[0..3] of byte;
  2112. rfield,
  2113. data,s,opidx : longint;
  2114. ea_data : ea;
  2115. relsym : TObjSymbol;
  2116. needed_VEX_Extention: boolean;
  2117. needed_VEX: boolean;
  2118. opmode: integer;
  2119. VEXvvvv: byte;
  2120. VEXmmmmm: byte;
  2121. begin
  2122. { safety check }
  2123. if objdata.currobjsec.size<>longword(insoffset) then
  2124. internalerror(200130121);
  2125. { load data to write }
  2126. codes:=insentry^.code;
  2127. {$ifdef x86_64}
  2128. rexwritten:=false;
  2129. {$endif x86_64}
  2130. { Force word push/pop for registers }
  2131. if (opsize=S_W) and ((codes[0]=#4) or (codes[0]=#6) or
  2132. ((codes[0]=#1) and ((codes[2]=#5) or (codes[2]=#7)))) then
  2133. begin
  2134. bytes[0]:=$66;
  2135. objdata.writebytes(bytes,1);
  2136. end;
  2137. // needed VEX Prefix (for AVX etc.)
  2138. needed_VEX := false;
  2139. needed_VEX_Extention := false;
  2140. opmode := -1;
  2141. VEXvvvv := 0;
  2142. VEXmmmmm := 0;
  2143. repeat
  2144. c:=ord(codes^);
  2145. inc(codes);
  2146. case c of
  2147. 0: break;
  2148. 1,
  2149. 2,
  2150. 3: inc(codes,c);
  2151. 60: opmode := 0;
  2152. 61: opmode := 1;
  2153. 62: opmode := 2;
  2154. 219: VEXvvvv := VEXvvvv OR $02; // set SIMD-prefix $F3
  2155. 220: VEXvvvv := VEXvvvv OR $03; // set SIMD-prefix $F2
  2156. 241: VEXvvvv := VEXvvvv OR $01; // set SIMD-prefix $66
  2157. 242: needed_VEX := true;
  2158. 243: begin
  2159. needed_VEX_Extention := true;
  2160. VEXvvvv := VEXvvvv OR (1 shl 7); // set REX.W
  2161. end;
  2162. 244: VEXvvvv := VEXvvvv OR $04; // vectorlength = 256 bits AND no scalar
  2163. 248: VEXmmmmm := VEXmmmmm OR $01; // set leading opcode byte $0F
  2164. 249: begin
  2165. needed_VEX_Extention := true;
  2166. VEXmmmmm := VEXmmmmm OR $02; // set leading opcode byte $0F38
  2167. end;
  2168. 250: begin
  2169. needed_VEX_Extention := true;
  2170. VEXmmmmm := VEXmmmmm OR $03; // set leading opcode byte $0F3A
  2171. end;
  2172. end;
  2173. until false;
  2174. if needed_VEX then
  2175. begin
  2176. if (opmode > ops) or
  2177. (opmode < -1) then
  2178. begin
  2179. Internalerror(777100);
  2180. end
  2181. else if opmode = -1 then
  2182. begin
  2183. VEXvvvv := VEXvvvv or ($0F shl 3); // set VEXvvvv bits (bits 6-3) to 1
  2184. end
  2185. else if oper[opmode]^.typ = top_reg then
  2186. begin
  2187. VEXvvvv := VEXvvvv or ((not(regval(oper[opmode]^.reg)) and $07) shl 3);
  2188. {$ifdef x86_64}
  2189. if rexbits(oper[opmode]^.reg) = 0 then VEXvvvv := VEXvvvv or (1 shl 6);
  2190. {$else}
  2191. VEXvvvv := VEXvvvv or (1 shl 6);
  2192. {$endif x86_64}
  2193. end
  2194. else Internalerror(777101);
  2195. if not(needed_VEX_Extention) then
  2196. begin
  2197. {$ifdef x86_64}
  2198. if rex and $0B <> 0 then needed_VEX_Extention := true;
  2199. {$endif x86_64}
  2200. end;
  2201. if needed_VEX_Extention then
  2202. begin
  2203. // VEX-Prefix-Length = 3 Bytes
  2204. bytes[0]:=$C4;
  2205. objdata.writebytes(bytes,1);
  2206. {$ifdef x86_64}
  2207. VEXmmmmm := VEXmmmmm or ((not(rex) and $07) shl 5); // set REX.rxb
  2208. {$else}
  2209. VEXmmmmm := VEXmmmmm or (7 shl 5); //
  2210. {$endif x86_64}
  2211. bytes[0] := VEXmmmmm;
  2212. objdata.writebytes(bytes,1);
  2213. {$ifdef x86_64}
  2214. VEXvvvv := VEXvvvv OR ((rex and $08) shl 7); // set REX.w
  2215. {$endif x86_64}
  2216. bytes[0] := VEXvvvv;
  2217. objdata.writebytes(bytes,1);
  2218. end
  2219. else
  2220. begin
  2221. // VEX-Prefix-Length = 2 Bytes
  2222. bytes[0]:=$C5;
  2223. objdata.writebytes(bytes,1);
  2224. {$ifdef x86_64}
  2225. if rex and $04 = 0 then
  2226. {$endif x86_64}
  2227. begin
  2228. VEXvvvv := VEXvvvv or (1 shl 7);
  2229. end;
  2230. bytes[0] := VEXvvvv;
  2231. objdata.writebytes(bytes,1);
  2232. end;
  2233. end
  2234. else
  2235. begin
  2236. needed_VEX_Extention := false;
  2237. opmode := -1;
  2238. end;
  2239. { load data to write }
  2240. codes:=insentry^.code;
  2241. repeat
  2242. c:=ord(codes^);
  2243. inc(codes);
  2244. case c of
  2245. 0 :
  2246. break;
  2247. 1,2,3 :
  2248. begin
  2249. {$ifdef x86_64}
  2250. if not(needed_VEX) then // TG
  2251. maybewriterex;
  2252. {$endif x86_64}
  2253. objdata.writebytes(codes^,c);
  2254. inc(codes,c);
  2255. end;
  2256. 4,6 :
  2257. begin
  2258. case oper[0]^.reg of
  2259. NR_CS:
  2260. bytes[0]:=$e;
  2261. NR_NO,
  2262. NR_DS:
  2263. bytes[0]:=$1e;
  2264. NR_ES:
  2265. bytes[0]:=$6;
  2266. NR_SS:
  2267. bytes[0]:=$16;
  2268. else
  2269. internalerror(777004);
  2270. end;
  2271. if c=4 then
  2272. inc(bytes[0]);
  2273. objdata.writebytes(bytes,1);
  2274. end;
  2275. 5,7 :
  2276. begin
  2277. case oper[0]^.reg of
  2278. NR_FS:
  2279. bytes[0]:=$a0;
  2280. NR_GS:
  2281. bytes[0]:=$a8;
  2282. else
  2283. internalerror(777005);
  2284. end;
  2285. if c=5 then
  2286. inc(bytes[0]);
  2287. objdata.writebytes(bytes,1);
  2288. end;
  2289. 8,9,10 :
  2290. begin
  2291. {$ifdef x86_64}
  2292. if not(needed_VEX) then // TG
  2293. maybewriterex;
  2294. {$endif x86_64}
  2295. bytes[0]:=ord(codes^)+regval(oper[c-8]^.reg);
  2296. inc(codes);
  2297. objdata.writebytes(bytes,1);
  2298. end;
  2299. 11 :
  2300. begin
  2301. bytes[0]:=ord(codes^)+condval[condition];
  2302. inc(codes);
  2303. objdata.writebytes(bytes,1);
  2304. end;
  2305. 12,13,14 :
  2306. begin
  2307. getvalsym(c-12);
  2308. if (currval<-128) or (currval>127) then
  2309. Message2(asmw_e_value_exceeds_bounds,'signed byte',tostr(currval));
  2310. if assigned(currsym) then
  2311. objdata_writereloc(currval,1,currsym,currabsreloc)
  2312. else
  2313. objdata.writebytes(currval,1);
  2314. end;
  2315. 16,17,18 :
  2316. begin
  2317. getvalsym(c-16);
  2318. if (currval<-256) or (currval>255) then
  2319. Message2(asmw_e_value_exceeds_bounds,'byte',tostr(currval));
  2320. if assigned(currsym) then
  2321. objdata_writereloc(currval,1,currsym,currabsreloc)
  2322. else
  2323. objdata.writebytes(currval,1);
  2324. end;
  2325. 20,21,22,23 :
  2326. begin
  2327. getvalsym(c-20);
  2328. if (currval<0) or (currval>255) then
  2329. Message2(asmw_e_value_exceeds_bounds,'unsigned byte',tostr(currval));
  2330. if assigned(currsym) then
  2331. objdata_writereloc(currval,1,currsym,currabsreloc)
  2332. else
  2333. objdata.writebytes(currval,1);
  2334. end;
  2335. 24,25,26 : // 030..032
  2336. begin
  2337. getvalsym(c-24);
  2338. {$ifndef i8086}
  2339. { currval is an aint so this cannot happen on i8086 and causes only a warning }
  2340. if (currval<-65536) or (currval>65535) then
  2341. Message2(asmw_e_value_exceeds_bounds,'word',tostr(currval));
  2342. {$endif i8086}
  2343. if assigned(currsym) then
  2344. objdata_writereloc(currval,2,currsym,currabsreloc)
  2345. else
  2346. objdata.writebytes(currval,2);
  2347. end;
  2348. 28,29,30 : // 034..036
  2349. { !!! These are intended (and used in opcode table) to select depending
  2350. on address size, *not* operand size. Works by coincidence only. }
  2351. begin
  2352. getvalsym(c-28);
  2353. if opsize=S_Q then
  2354. begin
  2355. if assigned(currsym) then
  2356. objdata_writereloc(currval,8,currsym,currabsreloc)
  2357. else
  2358. objdata.writebytes(currval,8);
  2359. end
  2360. else
  2361. begin
  2362. if assigned(currsym) then
  2363. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2364. else
  2365. objdata.writebytes(currval,4);
  2366. end
  2367. end;
  2368. 32,33,34 : // 040..042
  2369. begin
  2370. getvalsym(c-32);
  2371. if assigned(currsym) then
  2372. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2373. else
  2374. objdata.writebytes(currval,4);
  2375. end;
  2376. 36,37,38 : // 044..046 - select between word/dword/qword depending on
  2377. begin // address size (we support only default address sizes).
  2378. getvalsym(c-36);
  2379. {$ifdef x86_64}
  2380. if assigned(currsym) then
  2381. objdata_writereloc(currval,8,currsym,currabsreloc)
  2382. else
  2383. objdata.writebytes(currval,8);
  2384. {$else x86_64}
  2385. if assigned(currsym) then
  2386. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2387. else
  2388. objdata.writebytes(currval,4);
  2389. {$endif x86_64}
  2390. end;
  2391. 40,41,42 : // 050..052 - byte relative operand
  2392. begin
  2393. getvalsym(c-40);
  2394. data:=currval-insend;
  2395. {$push}
  2396. {$r-}
  2397. if assigned(currsym) then
  2398. inc(data,currsym.address);
  2399. {$pop}
  2400. if (data>127) or (data<-128) then
  2401. Message1(asmw_e_short_jmp_out_of_range,tostr(data));
  2402. objdata.writebytes(data,1);
  2403. end;
  2404. 44,45,46: // 054..056 - qword immediate operand
  2405. begin
  2406. getvalsym(c-44);
  2407. if assigned(currsym) then
  2408. objdata_writereloc(currval,8,currsym,currabsreloc)
  2409. else
  2410. objdata.writebytes(currval,8);
  2411. end;
  2412. 52,53,54 : // 064..066 - select between 16/32 address mode, but we support only 32
  2413. begin
  2414. getvalsym(c-52);
  2415. if assigned(currsym) then
  2416. objdata_writereloc(currval,4,currsym,currrelreloc)
  2417. else
  2418. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2419. end;
  2420. 56,57,58 : // 070..072 - long relative operand
  2421. begin
  2422. getvalsym(c-56);
  2423. if assigned(currsym) then
  2424. objdata_writereloc(currval,4,currsym,currrelreloc)
  2425. else
  2426. objdata_writereloc(currval-insend,4,nil,currabsreloc32)
  2427. end;
  2428. 60,61,62 : ; // 074..076 - vex-coded vector operand
  2429. // ignore
  2430. 172,173,174 : // 0254..0256 - dword implicitly sign-extended to 64-bit (x86_64 only)
  2431. begin
  2432. getvalsym(c-172);
  2433. {$ifdef x86_64}
  2434. { for i386 as aint type is longint the
  2435. following test is useless }
  2436. if (currval<low(longint)) or (currval>high(longint)) then
  2437. Message2(asmw_e_value_exceeds_bounds,'signed dword',tostr(currval));
  2438. {$endif x86_64}
  2439. if assigned(currsym) then
  2440. objdata_writereloc(currval,4,currsym,currabsreloc32)
  2441. else
  2442. objdata.writebytes(currval,4);
  2443. end;
  2444. 200 : { fixed 16-bit addr }
  2445. {$ifndef x86_64}
  2446. begin
  2447. bytes[0]:=$67;
  2448. objdata.writebytes(bytes,1);
  2449. end;
  2450. {$else x86_64}
  2451. { every insentry having code 0310 must be marked with NOX86_64 }
  2452. InternalError(2011051302);
  2453. {$endif}
  2454. 201 : { fixed 32-bit addr }
  2455. {$ifdef x86_64}
  2456. begin
  2457. bytes[0]:=$67;
  2458. objdata.writebytes(bytes,1);
  2459. end
  2460. {$endif x86_64}
  2461. ;
  2462. 208,209,210 :
  2463. begin
  2464. case oper[c-208]^.ot and OT_SIZE_MASK of
  2465. OT_BITS16 :
  2466. begin
  2467. bytes[0]:=$66;
  2468. objdata.writebytes(bytes,1);
  2469. end;
  2470. {$ifndef x86_64}
  2471. OT_BITS64 :
  2472. Message(asmw_e_64bit_not_supported);
  2473. {$endif x86_64}
  2474. end;
  2475. end;
  2476. 211,
  2477. 213 : {no action needed};
  2478. 212,
  2479. 241:
  2480. begin
  2481. if not(needed_VEX) then
  2482. begin
  2483. bytes[0]:=$66;
  2484. objdata.writebytes(bytes,1);
  2485. end;
  2486. end;
  2487. 214 :
  2488. begin
  2489. {$ifndef x86_64}
  2490. Message(asmw_e_64bit_not_supported);
  2491. {$endif x86_64}
  2492. end;
  2493. 219 :
  2494. begin
  2495. if not(needed_VEX) then
  2496. begin
  2497. bytes[0]:=$f3;
  2498. objdata.writebytes(bytes,1);
  2499. end;
  2500. end;
  2501. 220 :
  2502. begin
  2503. if not(needed_VEX) then
  2504. begin
  2505. bytes[0]:=$f2;
  2506. objdata.writebytes(bytes,1);
  2507. end;
  2508. end;
  2509. 221:
  2510. ;
  2511. 202,
  2512. 215,
  2513. 217,218 :
  2514. begin
  2515. { these are dissambler hints or 32 bit prefixes which
  2516. are not needed }
  2517. end;
  2518. 242..244: ; // VEX flags =>> nothing todo
  2519. 247: begin
  2520. if needed_VEX then
  2521. begin
  2522. if ops = 4 then
  2523. begin
  2524. if (oper[3]^.typ=top_reg) then
  2525. begin
  2526. if (oper[3]^.ot and otf_reg_xmm <> 0) or
  2527. (oper[3]^.ot and otf_reg_ymm <> 0) then
  2528. begin
  2529. bytes[0] := ((getsupreg(oper[3]^.reg) and 15) shl 4);
  2530. objdata.writebytes(bytes,1);
  2531. end
  2532. else Internalerror(777102);
  2533. end
  2534. else Internalerror(777103);
  2535. end
  2536. else Internalerror(777104);
  2537. end
  2538. else Internalerror(777105);
  2539. end;
  2540. 248..250: ; // VEX flags =>> nothing todo
  2541. 31,
  2542. 48,49,50 :
  2543. begin
  2544. InternalError(777006);
  2545. end
  2546. else
  2547. begin
  2548. { rex should be written at this point }
  2549. {$ifdef x86_64}
  2550. if not(needed_VEX) then // TG
  2551. if (rex<>0) and not(rexwritten) then
  2552. internalerror(200603191);
  2553. {$endif x86_64}
  2554. if (c>=64) and (c<=151) then // 0100..0227
  2555. begin
  2556. if (c<127) then // 0177
  2557. begin
  2558. if (oper[c and 7]^.typ=top_reg) then
  2559. rfield:=regval(oper[c and 7]^.reg)
  2560. else
  2561. rfield:=regval(oper[c and 7]^.ref^.base);
  2562. end
  2563. else
  2564. rfield:=c and 7;
  2565. opidx:=(c shr 3) and 7;
  2566. if not process_ea(oper[opidx]^,ea_data,rfield) then
  2567. Message(asmw_e_invalid_effective_address);
  2568. pb:=@bytes[0];
  2569. pb^:=ea_data.modrm;
  2570. inc(pb);
  2571. if ea_data.sib_present then
  2572. begin
  2573. pb^:=ea_data.sib;
  2574. inc(pb);
  2575. end;
  2576. s:=pb-@bytes[0];
  2577. objdata.writebytes(bytes,s);
  2578. case ea_data.bytes of
  2579. 0 : ;
  2580. 1 :
  2581. begin
  2582. if (oper[opidx]^.ot and OT_MEMORY)=OT_MEMORY then
  2583. begin
  2584. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2585. {$ifdef i386}
  2586. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2587. (tf_pic_uses_got in target_info.flags) then
  2588. currabsreloc:=RELOC_GOT32
  2589. else
  2590. {$endif i386}
  2591. {$ifdef x86_64}
  2592. if oper[opidx]^.ref^.refaddr=addr_pic then
  2593. currabsreloc:=RELOC_GOTPCREL
  2594. else
  2595. {$endif x86_64}
  2596. currabsreloc:=RELOC_ABSOLUTE;
  2597. objdata_writereloc(oper[opidx]^.ref^.offset,1,currsym,currabsreloc);
  2598. end
  2599. else
  2600. begin
  2601. bytes[0]:=oper[opidx]^.ref^.offset;
  2602. objdata.writebytes(bytes,1);
  2603. end;
  2604. inc(s);
  2605. end;
  2606. 2,4 :
  2607. begin
  2608. currsym:=objdata.symbolref(oper[opidx]^.ref^.symbol);
  2609. currval:=oper[opidx]^.ref^.offset;
  2610. {$ifdef x86_64}
  2611. if oper[opidx]^.ref^.refaddr=addr_pic then
  2612. currabsreloc:=RELOC_GOTPCREL
  2613. else
  2614. if oper[opidx]^.ref^.base=NR_RIP then
  2615. begin
  2616. currabsreloc:=RELOC_RELATIVE;
  2617. { Adjust reloc value by number of bytes following the displacement,
  2618. but not if displacement is specified by literal constant }
  2619. if Assigned(currsym) then
  2620. Dec(currval,InsEnd-objdata.CurrObjSec.Size-ea_data.bytes);
  2621. end
  2622. else
  2623. {$endif x86_64}
  2624. {$ifdef i386}
  2625. if (oper[opidx]^.ref^.refaddr=addr_pic) and
  2626. (tf_pic_uses_got in target_info.flags) then
  2627. currabsreloc:=RELOC_GOT32
  2628. else
  2629. {$endif i386}
  2630. currabsreloc:=RELOC_ABSOLUTE32;
  2631. if (currabsreloc=RELOC_ABSOLUTE32) and
  2632. (Assigned(oper[opidx]^.ref^.relsymbol)) then
  2633. begin
  2634. relsym:=objdata.symbolref(oper[opidx]^.ref^.relsymbol);
  2635. if relsym.objsection=objdata.CurrObjSec then
  2636. begin
  2637. currval:=objdata.CurrObjSec.size+ea_data.bytes-relsym.offset+currval;
  2638. currabsreloc:=RELOC_RELATIVE;
  2639. end
  2640. else
  2641. begin
  2642. currabsreloc:=RELOC_PIC_PAIR;
  2643. currval:=relsym.offset;
  2644. end;
  2645. end;
  2646. objdata_writereloc(currval,ea_data.bytes,currsym,currabsreloc);
  2647. inc(s,ea_data.bytes);
  2648. end;
  2649. end;
  2650. end
  2651. else
  2652. InternalError(777007);
  2653. end;
  2654. end;
  2655. until false;
  2656. end;
  2657. function taicpu.is_same_reg_move(regtype: Tregistertype):boolean;
  2658. begin
  2659. result:=(((opcode=A_MOV) or (opcode=A_XCHG)) and
  2660. (regtype = R_INTREGISTER) and
  2661. (ops=2) and
  2662. (oper[0]^.typ=top_reg) and
  2663. (oper[1]^.typ=top_reg) and
  2664. (oper[0]^.reg=oper[1]^.reg)
  2665. ) or
  2666. (((opcode=A_MOVSS) or (opcode=A_MOVSD) or (opcode=A_MOVQ) or
  2667. (opcode=A_MOVAPS) or (OPCODE=A_MOVAPD) or
  2668. (opcode=A_VMOVSS) or (opcode=A_VMOVSD) or (opcode=A_VMOVQ) or
  2669. (opcode=A_VMOVAPS) or (OPCODE=A_VMOVAPD)) and
  2670. (regtype = R_MMREGISTER) and
  2671. (ops=2) and
  2672. (oper[0]^.typ=top_reg) and
  2673. (oper[1]^.typ=top_reg) and
  2674. (oper[0]^.reg=oper[1]^.reg)
  2675. );
  2676. end;
  2677. procedure build_spilling_operation_type_table;
  2678. var
  2679. opcode : tasmop;
  2680. i : integer;
  2681. begin
  2682. new(operation_type_table);
  2683. fillchar(operation_type_table^,sizeof(toperation_type_table),byte(operand_read));
  2684. for opcode:=low(tasmop) to high(tasmop) do
  2685. begin
  2686. for i:=1 to MaxInsChanges do
  2687. begin
  2688. case InsProp[opcode].Ch[i] of
  2689. Ch_Rop1 :
  2690. operation_type_table^[opcode,0]:=operand_read;
  2691. Ch_Wop1 :
  2692. operation_type_table^[opcode,0]:=operand_write;
  2693. Ch_RWop1,
  2694. Ch_Mop1 :
  2695. operation_type_table^[opcode,0]:=operand_readwrite;
  2696. Ch_Rop2 :
  2697. operation_type_table^[opcode,1]:=operand_read;
  2698. Ch_Wop2 :
  2699. operation_type_table^[opcode,1]:=operand_write;
  2700. Ch_RWop2,
  2701. Ch_Mop2 :
  2702. operation_type_table^[opcode,1]:=operand_readwrite;
  2703. Ch_Rop3 :
  2704. operation_type_table^[opcode,2]:=operand_read;
  2705. Ch_Wop3 :
  2706. operation_type_table^[opcode,2]:=operand_write;
  2707. Ch_RWop3,
  2708. Ch_Mop3 :
  2709. operation_type_table^[opcode,2]:=operand_readwrite;
  2710. end;
  2711. end;
  2712. end;
  2713. { Special cases that can't be decoded from the InsChanges flags }
  2714. operation_type_table^[A_IMUL,1]:=operand_readwrite;
  2715. end;
  2716. function taicpu.spilling_get_operation_type(opnr: longint): topertype;
  2717. begin
  2718. { the information in the instruction table is made for the string copy
  2719. operation MOVSD so hack here (FK)
  2720. VMOVSS and VMOVSD has two and three operand flavours, this cannot modelled by x86ins.dat
  2721. so fix it here (FK)
  2722. }
  2723. if ((opcode=A_MOVSD) or (opcode=A_VMOVSS) or (opcode=A_VMOVSD)) and (ops=2) then
  2724. begin
  2725. case opnr of
  2726. 0:
  2727. result:=operand_read;
  2728. 1:
  2729. result:=operand_write;
  2730. else
  2731. internalerror(200506055);
  2732. end
  2733. end
  2734. else
  2735. result:=operation_type_table^[opcode,opnr];
  2736. end;
  2737. function spilling_create_load(const ref:treference;r:tregister):Taicpu;
  2738. var
  2739. tmpref: treference;
  2740. begin
  2741. case getregtype(r) of
  2742. R_INTREGISTER :
  2743. begin
  2744. tmpref:=ref;
  2745. if getsubreg(r)=R_SUBH then
  2746. inc(tmpref.offset);
  2747. { we don't need special code here for 32 bit loads on x86_64, since
  2748. those will automatically zero-extend the upper 32 bits. }
  2749. result:=taicpu.op_ref_reg(A_MOV,reg2opsize(r),tmpref,r);
  2750. end;
  2751. R_MMREGISTER :
  2752. if current_settings.fputype in fpu_avx_instructionsets then
  2753. case getsubreg(r) of
  2754. R_SUBMMD:
  2755. result:=taicpu.op_ref_reg(A_VMOVSD,reg2opsize(r),ref,r);
  2756. R_SUBMMS:
  2757. result:=taicpu.op_ref_reg(A_VMOVSS,reg2opsize(r),ref,r);
  2758. R_SUBQ,
  2759. R_SUBMMWHOLE:
  2760. result:=taicpu.op_ref_reg(A_VMOVQ,S_NO,ref,r);
  2761. else
  2762. internalerror(200506043);
  2763. end
  2764. else
  2765. case getsubreg(r) of
  2766. R_SUBMMD:
  2767. result:=taicpu.op_ref_reg(A_MOVSD,reg2opsize(r),ref,r);
  2768. R_SUBMMS:
  2769. result:=taicpu.op_ref_reg(A_MOVSS,reg2opsize(r),ref,r);
  2770. R_SUBQ,
  2771. R_SUBMMWHOLE:
  2772. result:=taicpu.op_ref_reg(A_MOVQ,S_NO,ref,r);
  2773. else
  2774. internalerror(200506043);
  2775. end;
  2776. else
  2777. internalerror(200401041);
  2778. end;
  2779. end;
  2780. function spilling_create_store(r:tregister; const ref:treference):Taicpu;
  2781. var
  2782. size: topsize;
  2783. tmpref: treference;
  2784. begin
  2785. case getregtype(r) of
  2786. R_INTREGISTER :
  2787. begin
  2788. tmpref:=ref;
  2789. if getsubreg(r)=R_SUBH then
  2790. inc(tmpref.offset);
  2791. size:=reg2opsize(r);
  2792. {$ifdef x86_64}
  2793. { even if it's a 32 bit reg, we still have to spill 64 bits
  2794. because we often perform 64 bit operations on them }
  2795. if (size=S_L) then
  2796. begin
  2797. size:=S_Q;
  2798. r:=newreg(getregtype(r),getsupreg(r),R_SUBWHOLE);
  2799. end;
  2800. {$endif x86_64}
  2801. result:=taicpu.op_reg_ref(A_MOV,size,r,tmpref);
  2802. end;
  2803. R_MMREGISTER :
  2804. if current_settings.fputype in fpu_avx_instructionsets then
  2805. case getsubreg(r) of
  2806. R_SUBMMD:
  2807. result:=taicpu.op_reg_ref(A_VMOVSD,reg2opsize(r),r,ref);
  2808. R_SUBMMS:
  2809. result:=taicpu.op_reg_ref(A_VMOVSS,reg2opsize(r),r,ref);
  2810. R_SUBQ,
  2811. R_SUBMMWHOLE:
  2812. result:=taicpu.op_reg_ref(A_VMOVQ,S_NO,r,ref);
  2813. else
  2814. internalerror(200506042);
  2815. end
  2816. else
  2817. case getsubreg(r) of
  2818. R_SUBMMD:
  2819. result:=taicpu.op_reg_ref(A_MOVSD,reg2opsize(r),r,ref);
  2820. R_SUBMMS:
  2821. result:=taicpu.op_reg_ref(A_MOVSS,reg2opsize(r),r,ref);
  2822. R_SUBQ,
  2823. R_SUBMMWHOLE:
  2824. result:=taicpu.op_reg_ref(A_MOVQ,S_NO,r,ref);
  2825. else
  2826. internalerror(200506042);
  2827. end;
  2828. else
  2829. internalerror(200401041);
  2830. end;
  2831. end;
  2832. {*****************************************************************************
  2833. Instruction table
  2834. *****************************************************************************}
  2835. procedure BuildInsTabCache;
  2836. var
  2837. i : longint;
  2838. begin
  2839. new(instabcache);
  2840. FillChar(instabcache^,sizeof(tinstabcache),$ff);
  2841. i:=0;
  2842. while (i<InsTabEntries) do
  2843. begin
  2844. if InsTabCache^[InsTab[i].OPcode]=-1 then
  2845. InsTabCache^[InsTab[i].OPcode]:=i;
  2846. inc(i);
  2847. end;
  2848. end;
  2849. procedure BuildInsTabMemRefSizeInfoCache;
  2850. var
  2851. AsmOp: TasmOp;
  2852. i,j: longint;
  2853. insentry : PInsEntry;
  2854. MRefInfo: TMemRefSizeInfo;
  2855. SConstInfo: TConstSizeInfo;
  2856. actRegSize: int64;
  2857. actMemSize: int64;
  2858. actConstSize: int64;
  2859. actRegCount: integer;
  2860. actMemCount: integer;
  2861. actConstCount: integer;
  2862. actRegTypes : int64;
  2863. actRegMemTypes: int64;
  2864. NewRegSize: int64;
  2865. RegMMXSizeMask: int64;
  2866. RegXMMSizeMask: int64;
  2867. RegYMMSizeMask: int64;
  2868. bitcount: integer;
  2869. function bitcnt(aValue: int64): integer;
  2870. var
  2871. i: integer;
  2872. begin
  2873. result := 0;
  2874. for i := 0 to 63 do
  2875. begin
  2876. if (aValue mod 2) = 1 then
  2877. begin
  2878. inc(result);
  2879. end;
  2880. aValue := aValue shr 1;
  2881. end;
  2882. end;
  2883. begin
  2884. new(InsTabMemRefSizeInfoCache);
  2885. FillChar(InsTabMemRefSizeInfoCache^,sizeof(TInsTabMemRefSizeInfoCache),0);
  2886. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  2887. begin
  2888. i := InsTabCache^[AsmOp];
  2889. if i >= 0 then
  2890. begin
  2891. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  2892. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  2893. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := false;
  2894. insentry:=@instab[i];
  2895. RegMMXSizeMask := 0;
  2896. RegXMMSizeMask := 0;
  2897. RegYMMSizeMask := 0;
  2898. while (insentry^.opcode=AsmOp) do
  2899. begin
  2900. MRefInfo := msiUnkown;
  2901. actRegSize := 0;
  2902. actRegCount := 0;
  2903. actRegTypes := 0;
  2904. NewRegSize := 0;
  2905. actMemSize := 0;
  2906. actMemCount := 0;
  2907. actRegMemTypes := 0;
  2908. actConstSize := 0;
  2909. actConstCount := 0;
  2910. if asmop = a_movups then
  2911. begin
  2912. RegXMMSizeMask := RegXMMSizeMask;
  2913. end;
  2914. for j := 0 to insentry^.ops -1 do
  2915. begin
  2916. if (insentry^.optypes[j] and OT_REGISTER) = OT_REGISTER then
  2917. begin
  2918. inc(actRegCount);
  2919. NewRegSize := (insentry^.optypes[j] and OT_SIZE_MASK);
  2920. if NewRegSize = 0 then
  2921. begin
  2922. case insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG) of
  2923. OT_MMXREG: begin
  2924. NewRegSize := OT_BITS64;
  2925. end;
  2926. OT_XMMREG: begin
  2927. NewRegSize := OT_BITS128;
  2928. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  2929. end;
  2930. OT_YMMREG: begin
  2931. NewRegSize := OT_BITS256;
  2932. InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX := true;
  2933. end;
  2934. else NewRegSize := not(0);
  2935. end;
  2936. end;
  2937. actRegSize := actRegSize or NewRegSize;
  2938. actRegTypes := actRegTypes or (insentry^.optypes[j] and (OT_MMXREG OR OT_XMMREG OR OT_YMMREG));
  2939. end
  2940. else if ((insentry^.optypes[j] and OT_MEMORY) <> 0) then
  2941. begin
  2942. inc(actMemCount);
  2943. actMemSize := actMemSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  2944. if (insentry^.optypes[j] and OT_REGMEM) = OT_REGMEM then
  2945. begin
  2946. actRegMemTypes := actRegMemTypes or insentry^.optypes[j];
  2947. end;
  2948. end
  2949. else if ((insentry^.optypes[j] and OT_IMMEDIATE) = OT_IMMEDIATE) then
  2950. begin
  2951. inc(actConstCount);
  2952. actConstSize := actConstSize or (insentry^.optypes[j] and OT_SIZE_MASK);
  2953. end
  2954. end;
  2955. if actConstCount > 0 then
  2956. begin
  2957. case actConstSize of
  2958. 0: SConstInfo := csiNoSize;
  2959. OT_BITS8: SConstInfo := csiMem8;
  2960. OT_BITS16: SConstInfo := csiMem16;
  2961. OT_BITS32: SConstInfo := csiMem32;
  2962. OT_BITS64: SConstInfo := csiMem64;
  2963. else SConstInfo := csiMultiple;
  2964. end;
  2965. if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize = csiUnkown then
  2966. begin
  2967. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := SConstInfo;
  2968. end
  2969. else if InsTabMemRefSizeInfoCache^[AsmOp].ConstSize <> SConstInfo then
  2970. begin
  2971. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiMultiple;
  2972. end;
  2973. end;
  2974. case actMemCount of
  2975. 0: ; // nothing todo
  2976. 1: begin
  2977. MRefInfo := msiUnkown;
  2978. case actRegMemTypes and (OT_MMXRM OR OT_XMMRM OR OT_YMMRM) of
  2979. OT_MMXRM: actMemSize := actMemSize or OT_BITS64;
  2980. OT_XMMRM: actMemSize := actMemSize or OT_BITS128;
  2981. OT_YMMRM: actMemSize := actMemSize or OT_BITS256;
  2982. end;
  2983. case actMemSize of
  2984. 0: MRefInfo := msiNoSize;
  2985. OT_BITS8: MRefInfo := msiMem8;
  2986. OT_BITS16: MRefInfo := msiMem16;
  2987. OT_BITS32: MRefInfo := msiMem32;
  2988. OT_BITS64: MRefInfo := msiMem64;
  2989. OT_BITS128: MRefInfo := msiMem128;
  2990. OT_BITS256: MRefInfo := msiMem256;
  2991. OT_BITS80,
  2992. OT_FAR,
  2993. OT_NEAR,
  2994. OT_SHORT: ; // ignore
  2995. else begin
  2996. bitcount := bitcnt(actMemSize);
  2997. if bitcount > 1 then MRefInfo := msiMultiple
  2998. else InternalError(777203);
  2999. end;
  3000. end;
  3001. if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize = msiUnkown then
  3002. begin
  3003. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := MRefInfo;
  3004. end
  3005. else if InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize <> MRefInfo then
  3006. begin
  3007. with InsTabMemRefSizeInfoCache^[AsmOp] do
  3008. begin
  3009. if ((MemRefSize = msiMem8) OR (MRefInfo = msiMem8)) then MemRefSize := msiMultiple8
  3010. else if ((MemRefSize = msiMem16) OR (MRefInfo = msiMem16)) then MemRefSize := msiMultiple16
  3011. else if ((MemRefSize = msiMem32) OR (MRefInfo = msiMem32)) then MemRefSize := msiMultiple32
  3012. else if ((MemRefSize = msiMem64) OR (MRefInfo = msiMem64)) then MemRefSize := msiMultiple64
  3013. else if ((MemRefSize = msiMem128) OR (MRefInfo = msiMem128)) then MemRefSize := msiMultiple128
  3014. else if ((MemRefSize = msiMem256) OR (MRefInfo = msiMem256)) then MemRefSize := msiMultiple256
  3015. else MemRefSize := msiMultiple;
  3016. end;
  3017. end;
  3018. if actRegCount > 0 then
  3019. begin
  3020. case actRegTypes and (OT_MMXREG or OT_XMMREG or OT_YMMREG) of
  3021. OT_MMXREG: RegMMXSizeMask := RegMMXSizeMask or actMemSize;
  3022. OT_XMMREG: RegXMMSizeMask := RegXMMSizeMask or actMemSize;
  3023. OT_YMMREG: RegYMMSizeMask := RegYMMSizeMask or actMemSize;
  3024. else begin
  3025. RegMMXSizeMask := not(0);
  3026. RegXMMSizeMask := not(0);
  3027. RegYMMSizeMask := not(0);
  3028. end;
  3029. end;
  3030. end;
  3031. end;
  3032. else InternalError(777202);
  3033. end;
  3034. inc(insentry);
  3035. end;
  3036. if (InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize in MemRefMultiples) and
  3037. (InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX)then
  3038. begin
  3039. case RegXMMSizeMask of
  3040. OT_BITS64: case RegYMMSizeMask of
  3041. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3042. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y256;
  3043. end;
  3044. OT_BITS128: begin
  3045. if RegMMXSizeMask = 0 then
  3046. begin
  3047. case RegYMMSizeMask of
  3048. OT_BITS128: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegx64y128;
  3049. OT_BITS256: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3050. end;
  3051. end
  3052. else if RegYMMSizeMask = 0 then
  3053. begin
  3054. case RegMMXSizeMask of
  3055. OT_BITS64: InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiMemRegSize;
  3056. end;
  3057. end
  3058. else InternalError(777205);
  3059. end;
  3060. end;
  3061. end;
  3062. end;
  3063. end;
  3064. for AsmOp := low(TAsmOp) to high(TAsmOp) do
  3065. begin
  3066. // only supported intructiones with SSE- or AVX-operands
  3067. if not(InsTabMemRefSizeInfoCache^[AsmOp].ExistsSSEAVX) then
  3068. begin
  3069. InsTabMemRefSizeInfoCache^[AsmOp].MemRefSize := msiUnkown;
  3070. InsTabMemRefSizeInfoCache^[AsmOp].ConstSize := csiUnkown;
  3071. end;
  3072. end;
  3073. end;
  3074. procedure InitAsm;
  3075. begin
  3076. build_spilling_operation_type_table;
  3077. if not assigned(instabcache) then
  3078. BuildInsTabCache;
  3079. if not assigned(InsTabMemRefSizeInfoCache) then
  3080. BuildInsTabMemRefSizeInfoCache;
  3081. end;
  3082. procedure DoneAsm;
  3083. begin
  3084. if assigned(operation_type_table) then
  3085. begin
  3086. dispose(operation_type_table);
  3087. operation_type_table:=nil;
  3088. end;
  3089. if assigned(instabcache) then
  3090. begin
  3091. dispose(instabcache);
  3092. instabcache:=nil;
  3093. end;
  3094. if assigned(InsTabMemRefSizeInfoCache) then
  3095. begin
  3096. dispose(InsTabMemRefSizeInfoCache);
  3097. InsTabMemRefSizeInfoCache:=nil;
  3098. end;
  3099. end;
  3100. begin
  3101. cai_align:=tai_align;
  3102. cai_cpu:=taicpu;
  3103. end.