aasmtai.pas 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit implements an abstract asmoutput class for all processor types
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements an abstract asm output class for all processor types)
  18. This unit implements an abstract assembler output class for all processors, these
  19. are then overridden for each assembler writer to actually write the data in these
  20. classes to an assembler file.
  21. }
  22. unit aasmtai;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,systems,
  28. cpuinfo,cpubase,
  29. {$ifdef llvm}
  30. { overrides max_operands }
  31. llvmbase,
  32. {$endif llvm}
  33. cgbase,cgutils,
  34. symtype,
  35. aasmbase,aasmdata,ogbase
  36. {$ifdef jvm}
  37. ,widestr
  38. {$endif jvm}
  39. ;
  40. type
  41. { keep the number of elements in this enumeration less or equal than 32 as long
  42. as FPC knows only 4 byte and 32 byte sets (FK) }
  43. taitype = (
  44. ait_none,
  45. ait_align,
  46. ait_section,
  47. ait_comment,
  48. ait_string,
  49. ait_instruction,
  50. ait_datablock,
  51. ait_symbol,
  52. { needed to calc the size of a symbol }
  53. ait_symbol_end,
  54. ait_directive,
  55. ait_label,
  56. ait_const,
  57. ait_realconst,
  58. ait_typedconst,
  59. ait_stab,
  60. ait_force_line,
  61. ait_function_name,
  62. ait_symbolpair,
  63. { used to split into tiny assembler files }
  64. ait_cutobject,
  65. ait_regalloc,
  66. ait_tempalloc,
  67. { used to mark assembler blocks and inlined functions }
  68. ait_marker,
  69. { used to describe a new location of a variable }
  70. ait_varloc,
  71. {$ifdef JVM}
  72. { JVM only }
  73. ait_jvar, { debug information for a local variable }
  74. ait_jcatch, { exception catch clause }
  75. {$endif JVM}
  76. {$ifdef llvm}
  77. ait_llvmins, { llvm instruction }
  78. ait_llvmalias, { alias for a symbol }
  79. ait_llvmdecl, { llvm symbol declaration (global/external variable, external procdef) }
  80. ait_llvmmetadatanode, (* llvm metadata node: !id = !{type value, ...} *)
  81. ait_llvmmetadatareftypedconst, { reference to metadata inside a metadata constant }
  82. ait_llvmmetadatarefoperand, { llvm metadata referece: !metadataname !id }
  83. {$endif}
  84. {$ifdef wasm}
  85. ait_export_name,
  86. ait_local,
  87. ait_globaltype,
  88. ait_functype,
  89. ait_tagtype,
  90. ait_import_module,
  91. ait_import_name,
  92. ait_wasm_structured_instruction,
  93. {$endif}
  94. { SEH directives used in ARM,MIPS and x86_64 COFF targets }
  95. ait_seh_directive,
  96. { Dwarf CFI directive }
  97. ait_cfi,
  98. ait_eabi_attribute,
  99. ait_attribute
  100. );
  101. taitypes = set of taitype;
  102. taiconst_type = (
  103. aitconst_128bit,
  104. aitconst_64bit,
  105. aitconst_32bit,
  106. aitconst_16bit,
  107. aitconst_8bit,
  108. aitconst_sleb128bit,
  109. aitconst_uleb128bit,
  110. { win32 only }
  111. aitconst_rva_symbol,
  112. aitconst_secrel32_symbol,
  113. { darwin only }
  114. { From gcc/config/darwin.c (darwin_asm_output_dwarf_delta):
  115. ***
  116. Output a difference of two labels that will be an assembly time
  117. constant if the two labels are local. (.long lab1-lab2 will be
  118. very different if lab1 is at the boundary between two sections; it
  119. will be relocated according to the second section, not the first,
  120. so one ends up with a difference between labels in different
  121. sections, which is bad in the dwarf2 eh context for instance.)
  122. ***
  123. We cannot use this everywhere, because older versions of the
  124. darwin assembler don't support the construct used for these
  125. relsyms (nor do they support dwarf, for that matter)
  126. }
  127. aitconst_darwin_dwarf_delta64,
  128. aitconst_darwin_dwarf_delta32,
  129. { ARM Thumb-2 only }
  130. aitconst_half16bit, { used for table jumps. The actual value is the 16bit value shifted left once }
  131. { AVR }
  132. aitconst_gs, { Upper 16-bit of 17-bit constant }
  133. { for use by dwarf debugger information }
  134. aitconst_16bit_unaligned,
  135. aitconst_32bit_unaligned,
  136. aitconst_64bit_unaligned,
  137. { i8086 far pointer; emits: 'DW symbol, SEG symbol' }
  138. aitconst_farptr,
  139. { i8086 segment of symbol; emits: 'DW SEG symbol' }
  140. aitconst_seg,
  141. { i8086 data segment group; emits: 'DW dgroup'
  142. generated by the this inline asm:
  143. DW SEG @DATA
  144. in all memory models, except huge }
  145. aitconst_dgroup,
  146. { i8086 far data segment of the current pascal module (unit or program);
  147. emits: 'DW CURRENTMODULENAME_DATA'
  148. generated by the this inline asm:
  149. DW SEG @DATA
  150. in the huge memory model }
  151. aitconst_fardataseg,
  152. { offset of symbol's GOT slot in GOT }
  153. aitconst_got,
  154. { offset of symbol itself from GOT }
  155. aitconst_gotoff_symbol,
  156. { offset in TLS block }
  157. aitconst_dtpoff,
  158. { ARM TLS code }
  159. aitconst_gottpoff,
  160. aitconst_tpoff,
  161. aitconst_tlsgd,
  162. aitconst_tlsdesc
  163. );
  164. tairealconsttype = (
  165. aitrealconst_s32bit,
  166. aitrealconst_s64bit,
  167. aitrealconst_s80bit,
  168. aitrealconst_s128bit,
  169. aitrealconst_s64comp
  170. );
  171. const
  172. {$if defined(cpu64bitaddr)}
  173. aitconst_ptr = aitconst_64bit;
  174. aitconst_ptr_unaligned = aitconst_64bit_unaligned;
  175. aitconst_sizeint = aitconst_64bit;
  176. aitconst_sizeint_unaligned = aitconst_64bit_unaligned;
  177. {$elseif defined(cpu32bitaddr)}
  178. aitconst_ptr = aitconst_32bit;
  179. aitconst_ptr_unaligned = aitconst_32bit_unaligned;
  180. aitconst_sizeint = aitconst_32bit;
  181. aitconst_sizeint_unaligned = aitconst_32bit_unaligned;
  182. {$elseif defined(cpu16bitaddr)}
  183. aitconst_ptr = aitconst_16bit;
  184. aitconst_ptr_unaligned = aitconst_16bit_unaligned;
  185. aitconst_sizeint = aitconst_16bit;
  186. aitconst_sizeint_unaligned = aitconst_16bit_unaligned;
  187. {$endif}
  188. {$if defined(cpu64bitalu)}
  189. aitconst_aint = aitconst_64bit;
  190. {$elseif defined(cpu32bitalu)}
  191. aitconst_aint = aitconst_32bit;
  192. {$elseif defined(cpu16bitalu)}
  193. aitconst_aint = aitconst_16bit;
  194. {$elseif defined(cpu8bitalu)}
  195. aitconst_aint = aitconst_8bit;
  196. {$endif}
  197. taitypestr : array[taitype] of string[24] = (
  198. '<none>',
  199. 'align',
  200. 'section',
  201. 'comment',
  202. 'string',
  203. 'instruction',
  204. 'datablock',
  205. 'symbol',
  206. 'symbol_end',
  207. 'symbol_directive',
  208. 'label',
  209. 'const',
  210. 'realconst',
  211. 'typedconst',
  212. 'stab',
  213. 'force_line',
  214. 'function_name',
  215. 'symbolpair',
  216. 'cut',
  217. 'regalloc',
  218. 'tempalloc',
  219. 'marker',
  220. 'varloc',
  221. {$ifdef JVM}
  222. 'jvar',
  223. 'jcatch',
  224. {$endif JVM}
  225. {$ifdef llvm}
  226. 'llvmins',
  227. 'llvmalias',
  228. 'llvmdecl',
  229. 'llvmmetadata',
  230. 'llvmmetadatareftc',
  231. 'llvmmetadatarefop',
  232. {$endif}
  233. {$ifdef wasm}
  234. 'export_name',
  235. 'local',
  236. 'globaltype',
  237. 'functype',
  238. 'tagtype',
  239. 'import_module',
  240. 'import_name',
  241. 'wasm_structured_instr',
  242. {$endif}
  243. 'cfi',
  244. 'seh_directive',
  245. 'eabi_attribute',
  246. 'attribute'
  247. );
  248. type
  249. { Types of operand }
  250. toptype=(top_none,top_reg,top_ref,top_const,top_bool,top_local
  251. {$ifdef arm}
  252. { ARM only }
  253. ,top_modeflags
  254. ,top_specialreg
  255. {$endif arm}
  256. {$if defined(arm) or defined(aarch64)}
  257. ,top_regset
  258. ,top_conditioncode
  259. ,top_shifterop
  260. ,top_realconst
  261. {$endif defined(arm) or defined(aarch64)}
  262. {$ifdef aarch64}
  263. ,top_indexedreg
  264. {$endif}
  265. {$ifdef m68k}
  266. { m68k only }
  267. ,top_regset
  268. ,top_regpair
  269. ,top_realconst
  270. {$endif m68k}
  271. {$ifdef jvm}
  272. { jvm only}
  273. ,top_single
  274. ,top_double
  275. ,top_string
  276. ,top_wstring
  277. {$endif jvm}
  278. {$ifdef llvm}
  279. { llvm only }
  280. ,top_single
  281. ,top_double
  282. ,top_undef
  283. {$ifdef cpuextended}
  284. ,top_extended80
  285. {$endif cpuextended}
  286. ,top_tai
  287. ,top_def
  288. ,top_fpcond
  289. ,top_cond
  290. ,top_para
  291. ,top_asmlist
  292. ,top_callingconvention
  293. {$endif llvm}
  294. {$if defined(riscv)}
  295. ,top_fenceflags
  296. ,top_roundingmode
  297. ,top_realconst
  298. {$endif defined(riscv)}
  299. {$ifdef wasm}
  300. ,top_functype
  301. ,top_single
  302. ,top_double
  303. {$endif wasm}
  304. );
  305. { kinds of operations that an instruction can perform on an operand }
  306. topertype = (operand_read,operand_write,operand_readwrite);
  307. tlocaloper = record
  308. localsym : pointer;
  309. localsymderef : tderef;
  310. localsymofs : longint;
  311. {$ifdef x86}
  312. localsegment,
  313. {$endif x86}
  314. localindexreg : tregister;
  315. localscale : byte;
  316. localgetoffset,
  317. localforceref : boolean
  318. end;
  319. plocaloper = ^tlocaloper;
  320. const
  321. { ait_* types which don't result in executable code or which don't influence
  322. the way the program runs/behaves, but which may be encountered by the
  323. optimizer (= if it's sometimes added to the exprasm list). Update if you add
  324. a new ait type! }
  325. SkipInstr = [ait_comment, ait_symbol,ait_section,ait_align
  326. ,ait_stab, ait_function_name, ait_force_line
  327. ,ait_regalloc, ait_tempalloc, ait_symbol_end
  328. ,ait_directive
  329. ,ait_varloc,
  330. {$ifdef JVM}
  331. ait_jvar,
  332. {$endif JVM}
  333. ait_seh_directive];
  334. { ait_* types which do not have line information (and hence which are of type
  335. tai, otherwise, they are of type tailineinfo }
  336. SkipLineInfo =[ait_label,
  337. ait_regalloc,ait_tempalloc,
  338. ait_stab,ait_function_name,
  339. ait_cutobject,ait_marker,ait_varloc,ait_align,ait_section,ait_comment,
  340. ait_const,ait_directive,
  341. ait_symbolpair,
  342. ait_realconst,
  343. ait_symbol,
  344. {$ifdef JVM}
  345. ait_jvar, ait_jcatch,
  346. {$endif JVM}
  347. {$ifdef llvm}
  348. ait_llvmdecl,
  349. ait_llvmmetadatanode,
  350. ait_llvmmetadatareftypedconst,
  351. ait_llvmmetadatarefoperand,
  352. {$endif llvm}
  353. {$ifdef wasm}
  354. ait_export_name,
  355. ait_local,
  356. ait_globaltype,
  357. ait_functype,
  358. ait_tagtype,
  359. ait_import_module,
  360. ait_import_name,
  361. ait_wasm_structured_instruction,
  362. {$endif wasm}
  363. ait_seh_directive,
  364. ait_cfi,
  365. ait_eabi_attribute,
  366. ait_attribute
  367. ];
  368. type
  369. { cut type, required for alphanumeric ordering of the assembler filenames }
  370. TCutPlace=(cut_normal,cut_begin,cut_end);
  371. TAsmMarker = (
  372. mark_NoPropInfoStart,mark_NoPropInfoEnd,
  373. mark_AsmBlockStart,mark_AsmBlockEnd,
  374. mark_NoLineInfoStart,mark_NoLineInfoEnd,mark_BlockStart,
  375. mark_Position
  376. {$ifdef avr}
  377. { spilling on avr destroys the flags as it might use adiw/add/adc, so in case
  378. the flags are allocated during spilling, this marker must be translated into
  379. a push of the flags when assembler post processing is carried out }
  380. ,mark_may_store_flags_with_r26
  381. { spilling on avr destroys the flags as it might use adiw/add/adc, so in case
  382. the flags are allocated during spilling, this marker must be translated into
  383. a pop of the flags when assembler post processing is carried out }
  384. ,mark_may_restore_flags_with_r26
  385. {$endif avr}
  386. );
  387. TRegAllocType = (ra_alloc,ra_dealloc,ra_sync,ra_resize,ra_markused);
  388. TStabType = (stab_stabs,stab_stabn,stab_stabd,
  389. { AIX/XCOFF stab types }
  390. stab_stabx,
  391. { begin/end include file }
  392. stabx_bi,stabx_ei,
  393. { begin/end function }
  394. stabx_bf, stabx_ef,
  395. { begin/end static data block }
  396. stabx_bs, stabx_es,
  397. { line spec, function start/end label }
  398. stabx_line, stabx_function);
  399. TAsmDirective=(
  400. asd_indirect_symbol,
  401. asd_extern,asd_nasm_import, asd_toc_entry,
  402. asd_reference,asd_no_dead_strip,asd_weak_reference,asd_lazy_reference,
  403. asd_weak_definition,
  404. { for Jasmin }
  405. asd_jclass,asd_jinterface,asd_jsuper,asd_jfield,asd_jlimit,asd_jline,
  406. { .ent/.end for MIPS }
  407. asd_ent,asd_ent_end,
  408. { supported by recent clang-based assemblers for data-in-code }
  409. asd_data_region, asd_end_data_region,
  410. { ARM }
  411. asd_thumb_func,asd_code,asd_force_thumb,
  412. { restricts the assembler only to those instructions, which are
  413. available on the specified CPU; this represents directives such as
  414. NASM's 'CPU 686' or MASM/TASM's '.686p'. Might not be supported by
  415. all assemblers. }
  416. asd_cpu,
  417. { for the OMF object format }
  418. asd_omf_linnum_line,
  419. { RISC-V }
  420. asd_option
  421. );
  422. TAsmSehDirective=(
  423. ash_proc,ash_endproc,
  424. ash_endprologue,ash_handler,ash_handlerdata,
  425. ash_eh,ash_32,ash_no32,
  426. ash_setframe,ash_stackalloc,ash_pushreg,
  427. ash_savereg,ash_savereg_x,ash_saveregp,ash_saveregp_x,
  428. ash_savexmm,ash_savefreg,ash_savefreg_x,ash_savefregp,ash_savefregp_x,ash_pushframe,
  429. ash_setfp,ash_addfp,ash_savefplr,ash_savefplr_x,
  430. ash_nop,
  431. ash_pushnv,ash_savenv
  432. );
  433. const
  434. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized','used');
  435. tempallocstr : array[boolean] of string[10]=('released','allocated');
  436. stabtypestr : array[TStabType] of string[8]=(
  437. 'stabs','stabn','stabd',
  438. 'stabx',
  439. 'bi','ei',
  440. 'bf','ef',
  441. 'bs','es',
  442. 'line','function');
  443. directivestr : array[TAsmDirective] of string[23]=(
  444. 'indirect_symbol',
  445. 'extern','nasm_import', 'tc', 'reference',
  446. 'no_dead_strip','weak','lazy_reference','weak',
  447. { for Jasmin }
  448. 'class','interface','super','field','limit','line',
  449. { .ent/.end for MIPS }
  450. 'ent','end',
  451. { supported by recent clang-based assemblers for data-in-code }
  452. 'data_region','end_data_region',
  453. { ARM }
  454. 'thumb_func',
  455. 'code',
  456. 'force_thumb',
  457. 'cpu',
  458. { for the OMF object format }
  459. 'omf_line',
  460. { RISC-V }
  461. 'option'
  462. );
  463. sehdirectivestr : array[TAsmSehDirective] of string[16]=(
  464. '.seh_proc','.seh_endproc',
  465. '.seh_endprologue','.seh_handler','.seh_handlerdata',
  466. '.seh_eh','.seh_32','seh_no32',
  467. '.seh_setframe','.seh_stackalloc','.seh_pushreg',
  468. '.seh_savereg','.seh_savereg_x','.seh_saveregp','.seh_saveregp_x',
  469. '.seh_savexmm','.seh_savefreg','.seh_savefreg_x','.seh_savefregp','.seh_savefregp_x','.seh_pushframe',
  470. '.seh_setfp','.seh_addfp','.seh_savefplr','.seh_savefplr_x',
  471. '.seh_nop',
  472. '.pushnv','.savenv'
  473. );
  474. symbolpairkindstr: array[TSymbolPairKind] of string[11]=(
  475. '.set', '.set', '.thumb_set', '.localentry'
  476. );
  477. type
  478. tai = class;
  479. { please keep the size of this record <=12 bytes and keep it properly aligned }
  480. toper = record
  481. ot : longint;
  482. {$ifdef x86}
  483. vopext: smallint;
  484. {$ENDIF}
  485. case typ : toptype of
  486. top_none : ();
  487. top_reg : (reg:tregister);
  488. top_ref : (ref:preference);
  489. top_const : (val:tcgint);
  490. top_bool : (b:boolean);
  491. { local varsym that will be inserted in pass_generate_code }
  492. top_local : (localoper:plocaloper);
  493. {$ifdef arm}
  494. top_regset : (regset:^tcpuregisterset; regtyp: tregistertype; subreg: tsubregister; usermode: boolean);
  495. top_modeflags : (modeflags : tcpumodeflags);
  496. top_specialreg : (specialreg:tregister; specialflags:tspecialregflags);
  497. {$endif arm}
  498. {$if defined(arm) or defined(aarch64)}
  499. top_shifterop : (shifterop : pshifterop);
  500. top_conditioncode : (cc : TAsmCond);
  501. top_realconst : (val_real:bestreal);
  502. {$endif defined(arm) or defined(aarch64)}
  503. {$ifdef aarch64}
  504. top_regset : (basereg: tregister; nregs, regsetindex: byte);
  505. top_indexedreg : (indexedreg: tregister; regindex: byte);
  506. {$endif}
  507. {$ifdef m68k}
  508. top_regset : (dataregset,addrregset,fpuregset: tcpuregisterset);
  509. top_regpair : (reghi,reglo: tregister);
  510. top_realconst : (val_real:bestreal);
  511. {$endif m68k}
  512. {$ifdef jvm}
  513. top_single : (sval:single);
  514. top_double : (dval:double);
  515. top_string : (pcvallen: aint; pcval: pchar);
  516. top_wstring : (pwstrval: tcompilerwidestring);
  517. {$endif jvm}
  518. {$ifdef llvm}
  519. top_single : (sval:single);
  520. top_double : (dval:double);
  521. top_undef : ();
  522. {$ifdef cpuextended}
  523. top_extended80 : (eval:extended);
  524. {$endif cpuextended}
  525. top_tai : (ai: tai);
  526. top_def : (def: tdef);
  527. top_cond : (cond: topcmp);
  528. top_fpcond : (fpcond: tllvmfpcmp);
  529. top_para : (paras: tfplist);
  530. top_asmlist : (asmlist: tasmlist);
  531. top_callingconvention: (callingconvention: tproccalloption);
  532. {$endif llvm}
  533. {$if defined(riscv32) or defined(riscv64)}
  534. top_fenceflags : (fenceflags : TFenceFlags);
  535. top_roundingmode : (roundingmode : TRoundingMode);
  536. top_realconst : (val_real:bestreal;special_value : TAsmRealSpecialValue);
  537. {$endif defined(riscv32) or defined(riscv64)}
  538. {$ifdef wasm}
  539. top_functype : (functype: TWasmFuncType);
  540. top_single : (sval:single);
  541. top_double : (dval:double);
  542. {$endif wasm}
  543. end;
  544. poper=^toper;
  545. { abstract assembler item }
  546. tai = class(TLinkedListItem)
  547. {$ifndef NOOPT}
  548. { pointer to record with optimizer info about this tai object }
  549. optinfo : pointer;
  550. {$endif NOOPT}
  551. typ : taitype;
  552. constructor Create;
  553. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  554. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  555. procedure buildderefimpl;virtual;
  556. procedure derefimpl;virtual;
  557. end;
  558. { abstract assembler item with line information }
  559. tailineinfo = class(tai)
  560. fileinfo : tfileposinfo;
  561. constructor Create;
  562. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  563. procedure ppuwrite(ppufile:tcompilerppufile);override;
  564. end;
  565. tai_simple = class(tai)
  566. constructor create(_typ : taitype);
  567. end;
  568. taiclass = class of tai;
  569. taiclassarray = array[taitype] of taiclass;
  570. { Generates an assembler string }
  571. { tai_string }
  572. tai_string = class(tailineinfo)
  573. str : TAnsiCharDynArray;
  574. constructor Create(const _str : string);
  575. constructor Create(const _str : ansistring);
  576. { data: not guaranteed to #0-terminated
  577. length: length of the data without #0 terminator (unless the #0
  578. terminator itself must be included)
  579. add0: add a terminating zero as part of the data after data }
  580. constructor Create_Data(data : pchar;length : longint; add0: boolean);
  581. destructor Destroy;override;
  582. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  583. procedure ppuwrite(ppufile:tcompilerppufile);override;
  584. function getcopy:tlinkedlistitem;override;
  585. function len : integer; inline;
  586. end;
  587. { Generates a common label }
  588. tai_symbol = class(tai)
  589. sym : tasmsymbol;
  590. value : puint;
  591. size : longint;
  592. is_global,
  593. has_value : boolean;
  594. constructor Create(_sym:tasmsymbol;siz:longint);
  595. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  596. constructor Create_Weak(_sym:tasmsymbol;siz:longint);
  597. constructor Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  598. constructor Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  599. constructor Createname_hidden(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  600. constructor Createname_global_value(const _name : string;_symtyp:Tasmsymtype;siz:longint;val:ptruint;def:tdef);
  601. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  602. procedure ppuwrite(ppufile:tcompilerppufile);override;
  603. procedure derefimpl;override;
  604. end;
  605. tai_symbol_end = class(tailineinfo)
  606. sym : tasmsymbol;
  607. constructor Create(_sym:tasmsymbol);
  608. constructor Createname(const _name : string);
  609. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  610. procedure ppuwrite(ppufile:tcompilerppufile);override;
  611. procedure derefimpl;override;
  612. end;
  613. tai_directive = class(tailineinfo)
  614. name : ansistring;
  615. directive : TAsmDirective;
  616. constructor Create(_directive:TAsmDirective;const _name:ansistring);
  617. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  618. procedure ppuwrite(ppufile:tcompilerppufile);override;
  619. end;
  620. { Generates an assembler label }
  621. tai_label = class(tai)
  622. labsym : tasmlabel;
  623. {$ifdef arm}
  624. { set to true when the label has been moved by insertpcrelativedata to the correct location
  625. so one label can be used multiple times }
  626. moved : boolean;
  627. { true, if a label has been already inserted, this is important for arm thumb where no negative
  628. pc relative offsets are allowed }
  629. inserted : boolean;
  630. {$endif arm}
  631. constructor Create(_labsym : tasmlabel);
  632. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  633. procedure ppuwrite(ppufile:tcompilerppufile);override;
  634. procedure derefimpl;override;
  635. end;
  636. { Generates an assembler comment }
  637. tai_comment = class(tai)
  638. str : pchar;
  639. constructor Create(_str : pchar);
  640. destructor Destroy; override;
  641. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  642. procedure ppuwrite(ppufile:tcompilerppufile);override;
  643. function getcopy:tlinkedlistitem;override;
  644. end;
  645. { Generates a section / segment directive }
  646. tai_section = class(tai)
  647. sectype : TAsmSectiontype;
  648. secorder : TasmSectionorder;
  649. secalign : longint;
  650. name : pshortstring;
  651. { used in binary writer }
  652. sec : TObjSection;
  653. { used only by ELF so far }
  654. secflags : TSectionFlags;
  655. secprogbits : TSectionProgbits;
  656. destructor Destroy;override;
  657. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  658. procedure ppuwrite(ppufile:tcompilerppufile);override;
  659. {$push}{$warnings off}
  660. private
  661. { this constructor is made private on purpose }
  662. { because sections should be created via new_section() }
  663. constructor Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:longint;Asecorder:TasmSectionorder=secorder_default);
  664. constructor Create_proc(Asectype:TAsmSectiontype;const Aname:string;Aalign:longint;Asecorder:TasmSectionorder=secorder_default);
  665. {$pop}
  666. end;
  667. { Generates an uninitializised data block }
  668. tai_datablock = class(tailineinfo)
  669. is_global : boolean;
  670. sym : tasmsymbol;
  671. size : asizeint;
  672. constructor Create(const _name: string; _size: asizeint; def: tdef; _typ: Tasmsymtype);
  673. constructor Create_hidden(const _name: string; _size: asizeint; def: tdef; _typ: Tasmsymtype);
  674. constructor Create_global(const _name: string; _size: asizeint; def: tdef; _typ: Tasmsymtype);
  675. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  676. procedure ppuwrite(ppufile:tcompilerppufile);override;
  677. procedure derefimpl;override;
  678. end;
  679. { Generates an integer const }
  680. { tai_const }
  681. tai_const = class(tai)
  682. sym,
  683. endsym : tasmsymbol;
  684. { if symbols and offset are provided the symofs is used,
  685. the value is calculated during assembling }
  686. symofs,
  687. value : int64;
  688. consttype : taiconst_type;
  689. { sleb128 and uleb128 values have a varying length, by calling FixSize their size can be fixed
  690. to avoid that other offsets need to be changed. The value to write is stored in fixed_size }
  691. fixed_size : byte;
  692. { we use for the 128bit int64/qword for now because I can't imagine a
  693. case where we need 128 bit now (FK) }
  694. constructor Create(_typ:taiconst_type;_value : int64);
  695. constructor Create_128bit(_value : int64);
  696. constructor Create_64bit(_value : int64);
  697. constructor Create_32bit(_value : longint);
  698. constructor Create_16bit(_value : word);
  699. constructor Create_64bit_unaligned(_value : int64);
  700. constructor Create_32bit_unaligned(_value : longint);
  701. constructor Create_16bit_unaligned(_value : word);
  702. constructor Create_8bit(_value : byte);
  703. constructor Create_char(size: integer; _value: dword);
  704. constructor Create_sleb128bit(_value : int64);
  705. constructor Create_uleb128bit(_value : qword);
  706. constructor Create_aint(_value : aint);
  707. constructor Create_sizeint(_value : asizeint);
  708. constructor Create_sizeint_unaligned(_value : asizeint);
  709. constructor Create_sym(_sym:tasmsymbol);
  710. {$ifdef i8086}
  711. constructor Create_sym_near(_sym:tasmsymbol);
  712. constructor Create_sym_far(_sym:tasmsymbol);
  713. constructor Createname_near(const name:string;ofs:asizeint);
  714. constructor Createname_far(const name:string;ofs:asizeint);
  715. constructor Createname_near(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  716. constructor Createname_far(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  717. {$endif i8086}
  718. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  719. constructor Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  720. constructor Create_type_sym_offset(_typ:taiconst_type;_sym:tasmsymbol;ofs:asizeint);
  721. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  722. constructor Create_rel_sym_offset(_typ : taiconst_type; _sym,_endsym : tasmsymbol; _ofs : int64);
  723. constructor Create_rva_sym(_sym:tasmsymbol);
  724. constructor Createname(const name:string;ofs:asizeint);
  725. constructor Createname_rel(const name, endname: string);
  726. constructor Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  727. constructor Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  728. constructor Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  729. constructor Create_type_name(_typ:taiconst_type;const name:string;symclass: TAsmSymbolClass;_symtyp:Tasmsymtype;ofs:asizeint);
  730. constructor Create_nil_codeptr;
  731. constructor Create_nil_codeptr_unaligned;
  732. constructor Create_nil_dataptr;
  733. constructor Create_nil_dataptr_unaligned;
  734. constructor Create_int_codeptr(_value: int64);
  735. constructor Create_int_codeptr_unaligned(_value: int64);
  736. constructor Create_int_dataptr(_value: int64);
  737. constructor Create_int_dataptr_unaligned(_value: int64);
  738. {$ifdef avr}
  739. constructor Create_int_dataptr_unaligned(_value: int64; size: taiconst_type);
  740. {$endif}
  741. {$ifdef i8086}
  742. constructor Create_seg_name(const name:string);
  743. constructor Create_dgroup;
  744. constructor Create_fardataseg;
  745. {$endif i8086}
  746. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  747. procedure ppuwrite(ppufile:tcompilerppufile);override;
  748. procedure derefimpl;override;
  749. function getcopy:tlinkedlistitem;override;
  750. function size:longint;
  751. { sleb128 and uleb128 values have a varying length, by calling FixSize their size can be fixed
  752. to avoid that other offsets need to be changed. The value to write is stored in fixed_size }
  753. Procedure FixSize;
  754. end;
  755. { floating point const }
  756. tformatoptions = (fo_none,fo_hiloswapped);
  757. tai_realconst = class(tai)
  758. realtyp: tairealconsttype;
  759. savesize: byte;
  760. value: record
  761. case tairealconsttype of
  762. aitrealconst_s32bit: (s32val: ts32real);
  763. aitrealconst_s64bit: (s64val: ts64real);
  764. aitrealconst_s80bit: (s80val: ts80real);
  765. aitrealconst_s128bit: (s128val: ts128real);
  766. aitrealconst_s64comp: (s64compval: ts64comp);
  767. end;
  768. {$ifdef ARM}
  769. formatoptions : tformatoptions;
  770. {$endif ARM}
  771. constructor create_s32real(val: ts32real);
  772. constructor create_s64real(val: ts64real);
  773. {$ifdef ARM}
  774. constructor create_s64real_hiloswapped(val : ts64real);
  775. {$endif ARM}
  776. constructor create_s80real(val: ts80real; _savesize: byte);
  777. constructor create_s128real(val: ts128real);
  778. constructor create_s64compreal(val: ts64comp);
  779. constructor ppuload(t: taitype;ppufile: tcompilerppufile); override;
  780. procedure ppuwrite(ppufile: tcompilerppufile); override;
  781. function getcopy:tlinkedlistitem;override;
  782. function datasize: word;
  783. end;
  784. { tai_stab }
  785. tai_stab = class(tai)
  786. str : pchar;
  787. stabtype : TStabType;
  788. constructor Create(_stabtype:TStabType;_str : pchar);
  789. constructor Create_str(_stabtype:TStabType;const s:string);
  790. constructor create_ansistr(_stabtype: TStabType; const s: ansistring);
  791. destructor Destroy;override;
  792. end;
  793. tai_force_line = class(tailineinfo)
  794. constructor Create;
  795. end;
  796. tai_function_name = class(tai)
  797. funcname : pshortstring;
  798. constructor create(const s:string);
  799. destructor destroy;override;
  800. end;
  801. { Insert a cut to split assembler into several smaller files }
  802. tai_cutobject = class(tai)
  803. place : tcutplace;
  804. constructor Create;
  805. constructor Create_begin;
  806. constructor Create_end;
  807. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  808. procedure ppuwrite(ppufile:tcompilerppufile);override;
  809. end;
  810. { Insert a marker for assembler and inline blocks }
  811. tai_marker = class(tai)
  812. Kind: TAsmMarker;
  813. Constructor Create(_Kind: TAsmMarker);
  814. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  815. procedure ppuwrite(ppufile:tcompilerppufile);override;
  816. end;
  817. tai_tempalloc = class(tai)
  818. allocation : boolean;
  819. {$ifdef EXTDEBUG}
  820. problem : pshortstring;
  821. {$endif EXTDEBUG}
  822. temppos,
  823. tempsize : longint;
  824. constructor alloc(pos,size:longint);
  825. constructor dealloc(pos,size:longint);
  826. {$ifdef EXTDEBUG}
  827. constructor allocinfo(pos,size:longint;const st:string);
  828. {$endif EXTDEBUG}
  829. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  830. destructor destroy;override;
  831. procedure ppuwrite(ppufile:tcompilerppufile);override;
  832. end;
  833. tai_regalloc = class(tai)
  834. reg : tregister;
  835. ratype : TRegAllocType;
  836. { tells BuildLabelTableAndFixRegAlloc that the deallocation should be kept }
  837. keep : boolean;
  838. { reg(de)alloc belongs to this instruction, this
  839. is only used for automatic inserted (de)alloc for
  840. imaginary register and required for spilling code }
  841. instr : tai;
  842. constructor alloc(r : tregister;ainstr:tai);
  843. constructor dealloc(r : tregister;ainstr:tai);
  844. constructor sync(r : tregister);
  845. constructor resize(r : tregister);
  846. constructor markused(r : tregister);
  847. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  848. procedure ppuwrite(ppufile:tcompilerppufile);override;
  849. end;
  850. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  851. { Class template for assembler instructions
  852. }
  853. tai_cpu_abstract = class(tailineinfo)
  854. protected
  855. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  856. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  857. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  858. procedure ppuderefoper(var o:toper);virtual;abstract;
  859. public
  860. { Condition flags for instruction }
  861. condition : TAsmCond;
  862. { Number of operands to instruction }
  863. ops : byte;
  864. { Number of allocate oper structures }
  865. opercnt : byte;
  866. { Operands of instruction }
  867. oper : array[0..max_operands-1] of poper;
  868. { Actual opcode of instruction }
  869. opcode : tasmop;
  870. {$ifdef x86}
  871. segprefix : tregister;
  872. {$endif x86}
  873. { true if instruction is a jmp }
  874. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  875. Constructor Create(op : tasmop);virtual;
  876. Destructor Destroy;override;
  877. function getcopy:TLinkedListItem;override;
  878. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  879. procedure ppuwrite(ppufile:tcompilerppufile);override;
  880. procedure buildderefimpl;override;
  881. procedure derefimpl;override;
  882. procedure SetCondition(const c:TAsmCond);
  883. procedure allocate_oper(opers:longint);
  884. procedure loadconst(opidx:longint;l:tcgint);
  885. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  886. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  887. procedure loadref(opidx:longint;const r:treference);
  888. procedure loadreg(opidx:longint;r:tregister);
  889. procedure loadoper(opidx:longint;o:toper); virtual;
  890. procedure clearop(opidx:longint); virtual;
  891. procedure freeop(opidx:longint);
  892. { register allocator }
  893. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  894. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  895. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  896. function Pass1(objdata:TObjData):longint;virtual;
  897. procedure Pass2(objdata:TObjData);virtual;
  898. procedure resetpass1; virtual;
  899. procedure resetpass2; virtual;
  900. end;
  901. tai_cpu_class = class of tai_cpu_abstract;
  902. { Buffer type used for alignment }
  903. tfillbuffer = array[0..63] of char;
  904. { alignment for operator }
  905. tai_align_abstract = class(tai)
  906. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  907. maxbytes : byte; { if needed bytes would be larger than maxbyes, alignment is ignored }
  908. fillsize : byte; { real size to fill }
  909. fillop : byte; { value to fill with - optional }
  910. use_op : boolean;
  911. constructor Create(b:byte);virtual;
  912. constructor Create_op(b: byte; _op: byte);virtual;
  913. constructor create_max(b: byte; max: byte);virtual;
  914. constructor create_op_max(b: byte; _op: byte; max: byte);virtual;
  915. constructor Create_zeros(b:byte);
  916. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  917. procedure ppuwrite(ppufile:tcompilerppufile);override;
  918. function calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;virtual;
  919. end;
  920. tai_align_class = class of tai_align_abstract;
  921. tai_varloc = class(tai)
  922. newlocation,
  923. newlocationhi : tregister;
  924. varsym : tsym;
  925. constructor create(sym : tsym;loc : tregister);
  926. constructor create64(sym : tsym;loc,lochi : tregister);
  927. {$ifdef cpu64bitalu}
  928. constructor create128(sym : tsym;loc,lochi : tregister);
  929. {$endif cpu64bitalu}
  930. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  931. procedure ppuwrite(ppufile:tcompilerppufile);override;
  932. procedure buildderefimpl;override;
  933. procedure derefimpl;override;
  934. end;
  935. TSehDirectiveDatatype=(sd_none,sd_string,sd_reg,sd_offset,sd_regoffset);
  936. TSehDirectiveData=record
  937. case typ: TSehDirectiveDatatype of
  938. sd_none: ();
  939. sd_string: (name:pshortstring;flags:byte);
  940. sd_reg,sd_offset,sd_regoffset: (reg:TRegister;offset:dword);
  941. end;
  942. tai_seh_directive = class(tai)
  943. kind: TAsmSehDirective;
  944. data: TSehDirectiveData;
  945. constructor create(_kind:TAsmSehDirective);
  946. constructor create_name(_kind:TAsmSehDirective;const _name: string);
  947. constructor create_reg(_kind:TAsmSehDirective;r:TRegister);
  948. constructor create_offset(_kind:TAsmSehDirective;ofs:dword);
  949. constructor create_reg_offset(_kind:TAsmSehDirective;r:TRegister;ofs:dword);
  950. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  951. destructor destroy;override;
  952. procedure ppuwrite(ppufile:tcompilerppufile);override;
  953. procedure generate_code(objdata:TObjData);virtual;
  954. property datatype: TSehDirectiveDatatype read data.typ;
  955. end;
  956. tai_seh_directive_class=class of tai_seh_directive;
  957. {$ifdef JVM}
  958. { JVM variable live range description }
  959. tai_jvar = class(tai)
  960. stackslot: longint;
  961. desc: pshortstring;
  962. startlab,stoplab: tasmsymbol;
  963. constructor Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  964. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  965. procedure ppuwrite(ppufile:tcompilerppufile);override;
  966. destructor destroy;override;
  967. end;
  968. tai_jvar_class = class of tai_jvar;
  969. { JVM exception catch description }
  970. tai_jcatch = class(tai)
  971. name: pshortstring;
  972. startlab,stoplab,handlerlab: tasmsymbol;
  973. constructor Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  974. destructor destroy;override;
  975. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  976. procedure ppuwrite(ppufile:tcompilerppufile);override;
  977. end;
  978. tai_jcatch_class = class of tai_jcatch;
  979. {$endif JVM}
  980. tai_symbolpair = class(tai)
  981. kind: TSymbolPairKind;
  982. sym,
  983. value: pshortstring;
  984. constructor create(akind: TSymbolPairKind; const asym, avalue: string);
  985. destructor destroy;override;
  986. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  987. procedure ppuwrite(ppufile:tcompilerppufile);override;
  988. end;
  989. teattrtyp = (eattrtype_none,eattrtype_dword,eattrtype_ntbs);
  990. tai_attribute = class(tai)
  991. eattr_typ : teattrtyp;
  992. tag,value : dword;
  993. valuestr : pstring;
  994. constructor create(atyp: taitype; atag,avalue : dword);
  995. constructor create(atyp: taitype; atag : dword;const avalue : string);
  996. destructor destroy;override;
  997. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  998. procedure ppuwrite(ppufile:tcompilerppufile);override;
  999. end;
  1000. var
  1001. { array with all class types for tais }
  1002. aiclass : taiclassarray;
  1003. { target specific tais, possibly overwritten in target specific aasmcpu }
  1004. cai_align : tai_align_class = tai_align_abstract;
  1005. cai_cpu : tai_cpu_class = tai_cpu_abstract;
  1006. cai_seh_directive: tai_seh_directive_class = tai_seh_directive;
  1007. { hook to notify uses of registers }
  1008. add_reg_instruction_hook : tadd_reg_instruction_proc;
  1009. procedure maybe_new_object_file(list:TAsmList);
  1010. function new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default) : tai_section;
  1011. function new_proc_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default) : tai_section;
  1012. function ppuloadai(ppufile:tcompilerppufile):tai;
  1013. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  1014. implementation
  1015. uses
  1016. {$ifdef x86}
  1017. aasmcpu,
  1018. {$endif x86}
  1019. SysUtils,
  1020. verbose,
  1021. globals,
  1022. ppu;
  1023. const
  1024. pputaimarker = 254;
  1025. {****************************************************************************
  1026. Helpers
  1027. ****************************************************************************}
  1028. procedure maybe_new_object_file(list:TAsmList);
  1029. begin
  1030. if create_smartlink_library then
  1031. list.concat(tai_cutobject.create);
  1032. end;
  1033. function new_section(list:TAsmList;Asectype:TAsmSectiontype;const Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default) : tai_section;
  1034. begin
  1035. Result:=tai_section.create(Asectype,Aname,Aalign,Asecorder);
  1036. list.concat(Result);
  1037. inc(list.section_count);
  1038. list.concat(cai_align.create(Aalign));
  1039. end;
  1040. function new_proc_section(list:TAsmList;Asectype:TAsmSectiontype;
  1041. const Aname:string;Aalign:byte;Asecorder:TasmSectionorder):tai_section;
  1042. begin
  1043. Result:=tai_section.Create_proc(Asectype,Aname,Aalign,Asecorder);
  1044. list.concat(Result);
  1045. inc(list.section_count);
  1046. list.concat(cai_align.create(Aalign));
  1047. end;
  1048. function ppuloadai(ppufile:tcompilerppufile):tai;
  1049. var
  1050. b : byte;
  1051. t : taitype;
  1052. begin
  1053. { marker }
  1054. b:=ppufile.getbyte;
  1055. if b<>pputaimarker then
  1056. internalerror(200208181);
  1057. { load nodetype }
  1058. t:=taitype(ppufile.getbyte);
  1059. if t<>ait_none then
  1060. begin
  1061. if t>high(taitype) then
  1062. internalerror(200208182);
  1063. if not assigned(aiclass[t]) then
  1064. internalerror(200208183);
  1065. {writeln('taiload: ',taitypestr[t]);}
  1066. { generate tai of the correct class }
  1067. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  1068. end
  1069. else
  1070. ppuloadai:=nil;
  1071. end;
  1072. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  1073. begin
  1074. { marker, read by ppuloadnode }
  1075. ppufile.putbyte(pputaimarker);
  1076. if assigned(n) then
  1077. begin
  1078. { type, read by ppuloadnode }
  1079. ppufile.putbyte(byte(n.typ));
  1080. {writeln('taiwrite: ',taitypestr[n.typ]);}
  1081. n.ppuwrite(ppufile);
  1082. end
  1083. else
  1084. ppufile.putbyte(byte(ait_none));
  1085. end;
  1086. constructor tai_symbolpair.create(akind: TSymbolPairKind; const asym, avalue: string);
  1087. begin
  1088. inherited create;
  1089. kind:=akind;
  1090. typ:=ait_symbolpair;
  1091. sym:=stringdup(asym);
  1092. value:=stringdup(avalue);
  1093. end;
  1094. destructor tai_symbolpair.destroy;
  1095. begin
  1096. stringdispose(sym);
  1097. stringdispose(value);
  1098. inherited destroy;
  1099. end;
  1100. constructor tai_symbolpair.ppuload(t: taitype; ppufile: tcompilerppufile);
  1101. begin
  1102. inherited ppuload(t,ppufile);
  1103. kind:=TSymbolPairKind(ppufile.getbyte);
  1104. sym:=ppufile.getpshortstring;
  1105. value:=ppufile.getpshortstring;
  1106. end;
  1107. procedure tai_symbolpair.ppuwrite(ppufile: tcompilerppufile);
  1108. begin
  1109. inherited ppuwrite(ppufile);
  1110. ppufile.putbyte(byte(kind));
  1111. ppufile.putstring(sym^);
  1112. ppufile.putstring(value^);
  1113. end;
  1114. constructor tai_varloc.create(sym: tsym; loc: tregister);
  1115. begin
  1116. inherited Create;
  1117. typ:=ait_varloc;
  1118. newlocation:=loc;
  1119. newlocationhi:=NR_NO;
  1120. varsym:=sym;
  1121. end;
  1122. constructor tai_varloc.create64(sym: tsym; loc, lochi: tregister);
  1123. begin
  1124. inherited Create;
  1125. typ:=ait_varloc;
  1126. newlocation:=loc;
  1127. newlocationhi:=lochi;
  1128. varsym:=sym;
  1129. end;
  1130. {$ifdef cpu64bitalu}
  1131. constructor tai_varloc.create128(sym: tsym; loc, lochi: tregister);
  1132. begin
  1133. inherited Create;
  1134. typ:=ait_varloc;
  1135. newlocation:=loc;
  1136. newlocationhi:=lochi;
  1137. varsym:=sym;
  1138. end;
  1139. {$endif cpu64bitalu}
  1140. constructor tai_varloc.ppuload(t: taitype; ppufile: tcompilerppufile);
  1141. begin
  1142. inherited ppuload(t, ppufile);
  1143. end;
  1144. procedure tai_varloc.ppuwrite(ppufile: tcompilerppufile);
  1145. begin
  1146. inherited ppuwrite(ppufile);
  1147. end;
  1148. procedure tai_varloc.buildderefimpl;
  1149. begin
  1150. inherited buildderefimpl;
  1151. end;
  1152. procedure tai_varloc.derefimpl;
  1153. begin
  1154. inherited derefimpl;
  1155. end;
  1156. {****************************************************************************
  1157. TAI
  1158. ****************************************************************************}
  1159. constructor tai.Create;
  1160. begin
  1161. {$ifndef NOOPT}
  1162. optinfo:=nil;
  1163. {$endif NOOPT}
  1164. end;
  1165. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  1166. begin
  1167. typ:=t;
  1168. {$ifndef NOOPT}
  1169. optinfo:=nil;
  1170. {$endif}
  1171. end;
  1172. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  1173. begin
  1174. end;
  1175. procedure tai.buildderefimpl;
  1176. begin
  1177. end;
  1178. procedure tai.derefimpl;
  1179. begin
  1180. end;
  1181. {****************************************************************************
  1182. TAILINEINFO
  1183. ****************************************************************************}
  1184. constructor tailineinfo.create;
  1185. begin
  1186. inherited create;
  1187. fileinfo:=current_filepos;
  1188. end;
  1189. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  1190. begin
  1191. inherited ppuload(t,ppufile);
  1192. ppufile.getposinfo(fileinfo);
  1193. end;
  1194. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  1195. begin
  1196. inherited ppuwrite(ppufile);
  1197. ppufile.putposinfo(fileinfo);
  1198. end;
  1199. {****************************************************************************
  1200. TAI_SIMPLE
  1201. ****************************************************************************}
  1202. constructor tai_simple.create(_typ : taitype);
  1203. begin
  1204. inherited create;
  1205. typ:=_typ;
  1206. end;
  1207. {****************************************************************************
  1208. TAI_SECTION
  1209. ****************************************************************************}
  1210. constructor tai_section.Create(Asectype:TAsmSectiontype;const Aname:string;Aalign:longint;Asecorder:TasmSectionorder=secorder_default);
  1211. begin
  1212. inherited Create;
  1213. typ:=ait_section;
  1214. sectype:=asectype;
  1215. secalign:=Aalign;
  1216. secorder:=Asecorder;
  1217. TObjData.sectiontype2progbitsandflags(sectype,secprogbits,secflags);
  1218. name:=stringdup(Aname);
  1219. sec:=nil;
  1220. // .noinit section should be marked with the nobits flag
  1221. if (sectype=sec_user) and (Aname='.noinit') then
  1222. secprogbits:=SPB_NOBITS;
  1223. end;
  1224. constructor tai_section.Create_proc(Asectype:TAsmSectiontype;
  1225. const Aname:string;Aalign:longint;Asecorder:TasmSectionorder);
  1226. begin
  1227. Create(Asectype,Aname,Aalign,Asecorder);
  1228. secprogbits:=SPB_PROGBITS;
  1229. exclude(secflags,SF_W);
  1230. include(secflags,SF_X);
  1231. end;
  1232. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  1233. begin
  1234. inherited ppuload(t,ppufile);
  1235. sectype:=TAsmSectiontype(ppufile.getbyte);
  1236. secalign:=ppufile.getlongint;
  1237. name:=ppufile.getpshortstring;
  1238. ppufile.getset(tppuset1(secflags));
  1239. secprogbits:=TSectionProgbits(ppufile.getbyte);
  1240. sec:=nil;
  1241. end;
  1242. destructor tai_section.Destroy;
  1243. begin
  1244. stringdispose(name);
  1245. end;
  1246. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  1247. begin
  1248. inherited ppuwrite(ppufile);
  1249. ppufile.putbyte(byte(sectype));
  1250. ppufile.putlongint(secalign);
  1251. ppufile.putstring(name^);
  1252. ppufile.putset(tppuset1(secflags));
  1253. ppufile.putbyte(byte(secprogbits));
  1254. end;
  1255. {****************************************************************************
  1256. TAI_DATABLOCK
  1257. ****************************************************************************}
  1258. constructor tai_datablock.Create(const _name : string;_size : asizeint; def: tdef; _typ:Tasmsymtype);
  1259. begin
  1260. inherited Create;
  1261. typ:=ait_datablock;
  1262. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_typ,def);
  1263. { keep things aligned }
  1264. if _size<=0 then
  1265. _size:=sizeof(aint);
  1266. size:=_size;
  1267. is_global:=false;
  1268. end;
  1269. constructor tai_datablock.Create_hidden(const _name: string; _size: asizeint; def: tdef; _typ:Tasmsymtype);
  1270. begin
  1271. if tf_supports_hidden_symbols in target_info.flags then
  1272. begin
  1273. inherited Create;
  1274. typ:=ait_datablock;
  1275. sym:=current_asmdata.DefineAsmSymbol(_name,AB_PRIVATE_EXTERN,_typ,def);
  1276. { keep things aligned }
  1277. if _size<=0 then
  1278. _size:=sizeof(aint);
  1279. size:=_size;
  1280. is_global:=true;
  1281. end
  1282. else
  1283. Create(_name,_size,def,_typ);
  1284. end;
  1285. constructor tai_datablock.Create_global(const _name : string;_size : asizeint; def: tdef; _typ:Tasmsymtype);
  1286. begin
  1287. inherited Create;
  1288. typ:=ait_datablock;
  1289. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_typ,def);
  1290. { keep things aligned }
  1291. if _size<=0 then
  1292. _size:=sizeof(aint);
  1293. size:=_size;
  1294. is_global:=true;
  1295. end;
  1296. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  1297. begin
  1298. inherited Create;
  1299. sym:=ppufile.getasmsymbol;
  1300. size:=ppufile.getaint;
  1301. is_global:=ppufile.getboolean;
  1302. end;
  1303. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  1304. begin
  1305. inherited ppuwrite(ppufile);
  1306. ppufile.putasmsymbol(sym);
  1307. ppufile.putaint(size);
  1308. ppufile.putboolean(is_global);
  1309. end;
  1310. procedure tai_datablock.derefimpl;
  1311. begin
  1312. end;
  1313. {****************************************************************************
  1314. TAI_SYMBOL
  1315. ****************************************************************************}
  1316. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  1317. begin
  1318. inherited Create;
  1319. typ:=ait_symbol;
  1320. sym:=_sym;
  1321. size:=siz;
  1322. { don't redefine global/external symbols as local, as code to access
  1323. such symbols is different on some platforms }
  1324. if not(sym.bind in [AB_NONE,AB_LOCAL]) then
  1325. internalerror(2013081601);
  1326. sym.bind:=AB_LOCAL;
  1327. is_global:=false;
  1328. end;
  1329. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  1330. begin
  1331. inherited Create;
  1332. typ:=ait_symbol;
  1333. sym:=_sym;
  1334. size:=siz;
  1335. { don't override PRIVATE_EXTERN with GLOBAL }
  1336. if not(sym.bind in [AB_GLOBAL,AB_PRIVATE_EXTERN]) then
  1337. sym.bind:=AB_GLOBAL;
  1338. is_global:=true;
  1339. end;
  1340. constructor tai_symbol.Create_Weak(_sym:tasmsymbol;siz:longint);
  1341. begin
  1342. inherited Create;
  1343. typ:=ait_symbol;
  1344. sym:=_sym;
  1345. size:=siz;
  1346. if not(sym.bind in [AB_NONE,AB_WEAK_EXTERNAL]) then
  1347. internalerror(2021092801);
  1348. sym.bind:=AB_WEAK;
  1349. is_global:=false;
  1350. end;
  1351. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  1352. begin
  1353. inherited Create;
  1354. typ:=ait_symbol;
  1355. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp,def);
  1356. size:=siz;
  1357. is_global:=false;
  1358. end;
  1359. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint;def:tdef);
  1360. begin
  1361. inherited Create;
  1362. typ:=ait_symbol;
  1363. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp,def);
  1364. size:=siz;
  1365. is_global:=true;
  1366. end;
  1367. constructor tai_symbol.Createname_hidden(const _name: string; _symtyp: Tasmsymtype; siz: longint; def: tdef);
  1368. begin
  1369. if tf_supports_hidden_symbols in target_info.flags then
  1370. begin
  1371. inherited Create;
  1372. typ:=ait_symbol;
  1373. sym:=current_asmdata.DefineAsmSymbol(_name,AB_PRIVATE_EXTERN,_symtyp,def);
  1374. size:=siz;
  1375. is_global:=true;
  1376. end
  1377. else
  1378. Createname(_name, _symtyp, siz, def);
  1379. end;
  1380. constructor tai_symbol.createname_global_value(const _name: string;_symtyp: tasmsymtype; siz: longint; val: ptruint;def:tdef);
  1381. begin
  1382. Createname_global(_name,_symtyp,siz,def);
  1383. value:=val;
  1384. has_value:=true;
  1385. end;
  1386. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  1387. begin
  1388. inherited ppuload(t,ppufile);
  1389. sym:=ppufile.getasmsymbol;
  1390. size:=ppufile.getlongint;
  1391. is_global:=ppufile.getboolean;
  1392. end;
  1393. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  1394. begin
  1395. inherited ppuwrite(ppufile);
  1396. ppufile.putasmsymbol(sym);
  1397. ppufile.putlongint(size);
  1398. ppufile.putboolean(is_global);
  1399. end;
  1400. procedure tai_symbol.derefimpl;
  1401. begin
  1402. end;
  1403. {****************************************************************************
  1404. TAI_SYMBOL_END
  1405. ****************************************************************************}
  1406. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  1407. begin
  1408. inherited Create;
  1409. typ:=ait_symbol_end;
  1410. sym:=_sym;
  1411. end;
  1412. constructor tai_symbol_end.Createname(const _name : string);
  1413. begin
  1414. inherited Create;
  1415. typ:=ait_symbol_end;
  1416. sym:=current_asmdata.GetAsmSymbol(_name);
  1417. if not assigned(sym) then
  1418. internalerror(2013080301);
  1419. end;
  1420. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  1421. begin
  1422. inherited ppuload(t,ppufile);
  1423. sym:=ppufile.getasmsymbol;
  1424. end;
  1425. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  1426. begin
  1427. inherited ppuwrite(ppufile);
  1428. ppufile.putasmsymbol(sym);
  1429. end;
  1430. procedure tai_symbol_end.derefimpl;
  1431. begin
  1432. end;
  1433. {****************************************************************************
  1434. TAI_SYMBOL_END
  1435. ****************************************************************************}
  1436. constructor tai_directive.Create(_directive:TAsmDirective;const _name:ansistring);
  1437. begin
  1438. inherited Create;
  1439. typ:=ait_directive;
  1440. name:=_name;
  1441. directive:=_directive;
  1442. end;
  1443. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  1444. begin
  1445. inherited ppuload(t,ppufile);
  1446. name:=ppufile.getansistring;
  1447. directive:=TAsmDirective(ppufile.getbyte);
  1448. end;
  1449. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  1450. begin
  1451. inherited ppuwrite(ppufile);
  1452. ppufile.putansistring(name);
  1453. ppufile.putbyte(byte(directive));
  1454. end;
  1455. {****************************************************************************
  1456. TAI_CONST
  1457. ****************************************************************************}
  1458. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  1459. begin
  1460. inherited Create;
  1461. typ:=ait_const;
  1462. consttype:=_typ;
  1463. value:=_value;
  1464. sym:=nil;
  1465. endsym:=nil;
  1466. end;
  1467. constructor tai_const.Create_128bit(_value : int64);
  1468. begin
  1469. inherited Create;
  1470. typ:=ait_const;
  1471. consttype:=aitconst_128bit;
  1472. value:=_value;
  1473. sym:=nil;
  1474. endsym:=nil;
  1475. end;
  1476. constructor tai_const.Create_64bit(_value : int64);
  1477. begin
  1478. inherited Create;
  1479. typ:=ait_const;
  1480. consttype:=aitconst_64bit;
  1481. value:=_value;
  1482. sym:=nil;
  1483. endsym:=nil;
  1484. end;
  1485. constructor tai_const.Create_32bit(_value : longint);
  1486. begin
  1487. inherited Create;
  1488. typ:=ait_const;
  1489. consttype:=aitconst_32bit;
  1490. value:=_value;
  1491. sym:=nil;
  1492. endsym:=nil;
  1493. end;
  1494. constructor tai_const.Create_16bit(_value : word);
  1495. begin
  1496. inherited Create;
  1497. typ:=ait_const;
  1498. consttype:=aitconst_16bit;
  1499. value:=_value;
  1500. sym:=nil;
  1501. endsym:=nil;
  1502. end;
  1503. constructor tai_const.Create_64bit_unaligned(_value : int64);
  1504. begin
  1505. inherited Create;
  1506. typ:=ait_const;
  1507. consttype:=aitconst_64bit_unaligned;
  1508. value:=_value;
  1509. sym:=nil;
  1510. endsym:=nil;
  1511. end;
  1512. constructor tai_const.Create_32bit_unaligned(_value : longint);
  1513. begin
  1514. inherited Create;
  1515. typ:=ait_const;
  1516. consttype:=aitconst_32bit_unaligned;
  1517. value:=_value;
  1518. sym:=nil;
  1519. endsym:=nil;
  1520. end;
  1521. constructor tai_const.Create_16bit_unaligned(_value : word);
  1522. begin
  1523. inherited Create;
  1524. typ:=ait_const;
  1525. consttype:=aitconst_16bit_unaligned;
  1526. value:=_value;
  1527. sym:=nil;
  1528. endsym:=nil;
  1529. end;
  1530. constructor tai_const.Create_8bit(_value : byte);
  1531. begin
  1532. inherited Create;
  1533. typ:=ait_const;
  1534. consttype:=aitconst_8bit;
  1535. value:=_value;
  1536. sym:=nil;
  1537. endsym:=nil;
  1538. end;
  1539. constructor tai_const.Create_char(size: integer; _value: dword);
  1540. begin
  1541. inherited Create;
  1542. typ:=ait_const;
  1543. case size of
  1544. 1:
  1545. begin
  1546. consttype:=aitconst_8bit;
  1547. value:=byte(_value)
  1548. end;
  1549. 2:
  1550. begin
  1551. consttype:=aitconst_16bit;
  1552. value:=word(_value)
  1553. end
  1554. else
  1555. InternalError(2010030701)
  1556. end
  1557. end;
  1558. constructor tai_const.Create_sleb128bit(_value : int64);
  1559. begin
  1560. inherited Create;
  1561. typ:=ait_const;
  1562. consttype:=aitconst_sleb128bit;
  1563. value:=_value;
  1564. sym:=nil;
  1565. endsym:=nil;
  1566. end;
  1567. constructor tai_const.Create_uleb128bit(_value : qword);
  1568. begin
  1569. inherited Create;
  1570. typ:=ait_const;
  1571. consttype:=aitconst_uleb128bit;
  1572. value:=int64(_value);
  1573. sym:=nil;
  1574. endsym:=nil;
  1575. end;
  1576. constructor tai_const.Create_aint(_value : aint);
  1577. begin
  1578. inherited Create;
  1579. typ:=ait_const;
  1580. consttype:=aitconst_aint;
  1581. value:=_value;
  1582. sym:=nil;
  1583. endsym:=nil;
  1584. end;
  1585. constructor tai_const.Create_sizeint(_value : asizeint);
  1586. begin
  1587. inherited Create;
  1588. typ:=ait_const;
  1589. consttype:=aitconst_sizeint;
  1590. value:=_value;
  1591. sym:=nil;
  1592. endsym:=nil;
  1593. end;
  1594. constructor tai_const.Create_sizeint_unaligned(_value : asizeint);
  1595. begin
  1596. inherited Create;
  1597. typ:=ait_const;
  1598. consttype:=aitconst_sizeint_unaligned;
  1599. value:=_value;
  1600. sym:=nil;
  1601. endsym:=nil;
  1602. end;
  1603. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1604. begin
  1605. inherited Create;
  1606. typ:=ait_const;
  1607. consttype:=_typ;
  1608. sym:=_sym;
  1609. endsym:=nil;
  1610. value:=0;
  1611. { update sym info }
  1612. if assigned(sym) then
  1613. sym.increfs;
  1614. end;
  1615. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1616. begin
  1617. self.create_sym_offset(_sym,0);
  1618. end;
  1619. {$ifdef i8086}
  1620. constructor tai_const.Create_sym_near(_sym: tasmsymbol);
  1621. begin
  1622. self.create_sym(_sym);
  1623. consttype:=aitconst_ptr;
  1624. end;
  1625. constructor tai_const.Create_sym_far(_sym: tasmsymbol);
  1626. begin
  1627. self.create_sym(_sym);
  1628. consttype:=aitconst_farptr;
  1629. end;
  1630. constructor tai_const.Createname_near(const name:string;ofs:asizeint);
  1631. begin
  1632. self.Createname(name,ofs);
  1633. consttype:=aitconst_ptr;
  1634. end;
  1635. constructor tai_const.Createname_far(const name:string;ofs:asizeint);
  1636. begin
  1637. self.Createname(name,ofs);
  1638. consttype:=aitconst_farptr;
  1639. end;
  1640. constructor tai_const.Createname_near(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1641. begin
  1642. self.Createname(name,_symtyp,ofs);
  1643. consttype:=aitconst_ptr;
  1644. end;
  1645. constructor tai_const.Createname_far(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1646. begin
  1647. self.Createname(name,_symtyp,ofs);
  1648. consttype:=aitconst_farptr;
  1649. end;
  1650. {$endif i8086}
  1651. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:asizeint);
  1652. begin
  1653. inherited Create;
  1654. typ:=ait_const;
  1655. {$ifdef i8086}
  1656. if assigned(_sym) and (_sym.typ=AT_DATA) then
  1657. begin
  1658. if current_settings.x86memorymodel in x86_far_data_models then
  1659. consttype:=aitconst_farptr
  1660. else
  1661. consttype:=aitconst_ptr;
  1662. end
  1663. else
  1664. begin
  1665. if current_settings.x86memorymodel in x86_far_code_models then
  1666. consttype:=aitconst_farptr
  1667. else
  1668. consttype:=aitconst_ptr;
  1669. end;
  1670. {$else i8086}
  1671. {$ifdef avr}
  1672. if assigned(_sym) and (_sym.typ=AT_FUNCTION) then
  1673. consttype:=aitconst_gs
  1674. else
  1675. {$endif avr}
  1676. consttype:=aitconst_ptr;
  1677. {$endif i8086}
  1678. { sym is allowed to be nil, this is used to write nil pointers }
  1679. sym:=_sym;
  1680. endsym:=nil;
  1681. { store the original offset in symofs so that we can recalculate the
  1682. value field in the assembler }
  1683. symofs:=ofs;
  1684. value:=ofs;
  1685. { update sym info }
  1686. if assigned(sym) then
  1687. sym.increfs;
  1688. end;
  1689. constructor tai_const.Create_type_sym_offset(_typ : taiconst_type;_sym : tasmsymbol; ofs : asizeint);
  1690. begin
  1691. inherited Create;
  1692. typ:=ait_const;
  1693. consttype:=_typ;
  1694. { sym is allowed to be nil, this is used to write nil pointers }
  1695. sym:=_sym;
  1696. endsym:=nil;
  1697. { store the original offset in symofs so that we can recalculate the
  1698. value field in the assembler }
  1699. symofs:=ofs;
  1700. value:=ofs;
  1701. { update sym info }
  1702. if assigned(sym) then
  1703. sym.increfs;
  1704. end;
  1705. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1706. begin
  1707. self.create_sym_offset(_sym,0);
  1708. consttype:=_typ;
  1709. endsym:=_endsym;
  1710. endsym.increfs;
  1711. end;
  1712. constructor tai_const.Create_rel_sym_offset(_typ: taiconst_type; _sym, _endsym: tasmsymbol; _ofs: int64);
  1713. begin
  1714. self.create_sym_offset(_sym,_ofs);
  1715. consttype:=_typ;
  1716. endsym:=_endsym;
  1717. endsym.increfs;
  1718. end;
  1719. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1720. begin
  1721. self.create_sym_offset(_sym,0);
  1722. consttype:=aitconst_rva_symbol;
  1723. end;
  1724. constructor tai_const.Createname(const name:string;ofs:asizeint);
  1725. begin
  1726. self.Createname(name,AT_NONE,ofs);
  1727. end;
  1728. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1729. begin
  1730. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1731. end;
  1732. constructor tai_const.Createname_rel(const name,endname:string);
  1733. begin
  1734. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,AT_NONE),0);
  1735. endsym:=current_asmdata.RefAsmSymbol(endname,AT_NONE)
  1736. end;
  1737. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;ofs:asizeint);
  1738. begin
  1739. self.Create_type_name(_typ,name,AT_NONE,ofs);
  1740. end;
  1741. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;_symtyp:Tasmsymtype;ofs:asizeint);
  1742. begin
  1743. self.create_sym_offset(current_asmdata.RefAsmSymbol(name,_symtyp),ofs);
  1744. consttype:=_typ;
  1745. end;
  1746. constructor tai_const.Create_type_name(_typ:taiconst_type;const name:string;symclass: TAsmSymbolClass;_symtyp:Tasmsymtype;ofs:asizeint);
  1747. begin
  1748. self.create_sym_offset(current_asmdata.RefAsmSymbolByClass(symclass,name,_symtyp),ofs);
  1749. consttype:=_typ;
  1750. end;
  1751. constructor tai_const.Create_nil_codeptr;
  1752. begin
  1753. self.Create_int_codeptr(0);
  1754. end;
  1755. constructor tai_const.Create_nil_codeptr_unaligned;
  1756. begin
  1757. self.Create_int_codeptr_unaligned(0);
  1758. end;
  1759. constructor tai_const.Create_nil_dataptr;
  1760. begin
  1761. self.Create_int_dataptr(0);
  1762. end;
  1763. constructor tai_const.Create_nil_dataptr_unaligned;
  1764. begin
  1765. self.Create_int_dataptr_unaligned(0);
  1766. end;
  1767. constructor tai_const.Create_int_codeptr(_value: int64);
  1768. begin
  1769. inherited Create;
  1770. typ:=ait_const;
  1771. {$ifdef i8086}
  1772. if current_settings.x86memorymodel in x86_far_code_models then
  1773. consttype:=aitconst_farptr
  1774. else
  1775. {$endif i8086}
  1776. {$ifdef avr}
  1777. consttype:=aitconst_gs;
  1778. {$else avr}
  1779. consttype:=aitconst_ptr;
  1780. {$endif avr}
  1781. sym:=nil;
  1782. endsym:=nil;
  1783. symofs:=0;
  1784. value:=_value;
  1785. end;
  1786. constructor tai_const.Create_int_codeptr_unaligned(_value: int64);
  1787. begin
  1788. inherited Create;
  1789. typ:=ait_const;
  1790. {$ifdef i8086}
  1791. if current_settings.x86memorymodel in x86_far_code_models then
  1792. consttype:=aitconst_farptr
  1793. else
  1794. {$endif i8086}
  1795. {$ifdef avr}
  1796. consttype:=aitconst_gs;
  1797. {$else avr}
  1798. consttype:=aitconst_ptr_unaligned;
  1799. {$endif avr}
  1800. sym:=nil;
  1801. endsym:=nil;
  1802. symofs:=0;
  1803. value:=_value;
  1804. end;
  1805. constructor tai_const.Create_int_dataptr(_value: int64);
  1806. begin
  1807. inherited Create;
  1808. typ:=ait_const;
  1809. {$ifdef i8086}
  1810. if current_settings.x86memorymodel in x86_far_data_models then
  1811. consttype:=aitconst_farptr
  1812. else
  1813. {$endif i8086}
  1814. consttype:=aitconst_ptr;
  1815. sym:=nil;
  1816. endsym:=nil;
  1817. symofs:=0;
  1818. value:=_value;
  1819. end;
  1820. constructor tai_const.Create_int_dataptr_unaligned(_value: int64);
  1821. begin
  1822. inherited Create;
  1823. typ:=ait_const;
  1824. {$ifdef i8086}
  1825. if current_settings.x86memorymodel in x86_far_data_models then
  1826. consttype:=aitconst_farptr
  1827. else
  1828. {$endif i8086}
  1829. consttype:=aitconst_ptr_unaligned;
  1830. sym:=nil;
  1831. endsym:=nil;
  1832. symofs:=0;
  1833. value:=_value;
  1834. end;
  1835. {$ifdef avr}
  1836. constructor tai_const.Create_int_dataptr_unaligned(_value: int64;
  1837. size: taiconst_type);
  1838. begin
  1839. inherited Create;
  1840. typ:=ait_const;
  1841. consttype:=size;
  1842. sym:=nil;
  1843. endsym:=nil;
  1844. symofs:=0;
  1845. value:=_value;
  1846. end;
  1847. {$endif avr}
  1848. {$ifdef i8086}
  1849. constructor tai_const.Create_seg_name(const name:string);
  1850. begin
  1851. self.Createname(name,0);
  1852. self.consttype:=aitconst_seg;
  1853. end;
  1854. constructor tai_const.Create_dgroup;
  1855. begin
  1856. self.Create_16bit(0);
  1857. self.consttype:=aitconst_dgroup;
  1858. end;
  1859. constructor tai_const.Create_fardataseg;
  1860. begin
  1861. self.Create_16bit(0);
  1862. self.consttype:=aitconst_fardataseg;
  1863. end;
  1864. {$endif i8086}
  1865. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1866. begin
  1867. inherited ppuload(t,ppufile);
  1868. consttype:=taiconst_type(ppufile.getbyte);
  1869. sym:=ppufile.getasmsymbol;
  1870. endsym:=ppufile.getasmsymbol;
  1871. value:=ppufile.getint64;
  1872. end;
  1873. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1874. begin
  1875. inherited ppuwrite(ppufile);
  1876. ppufile.putbyte(byte(consttype));
  1877. ppufile.putasmsymbol(sym);
  1878. ppufile.putasmsymbol(endsym);
  1879. ppufile.putint64(value);
  1880. end;
  1881. procedure tai_const.derefimpl;
  1882. begin
  1883. end;
  1884. function tai_const.getcopy:tlinkedlistitem;
  1885. begin
  1886. getcopy:=inherited getcopy;
  1887. { we need to increase the reference number }
  1888. if assigned(sym) then
  1889. sym.increfs;
  1890. if assigned(endsym) then
  1891. endsym.increfs;
  1892. end;
  1893. function tai_const.size:longint;
  1894. begin
  1895. case consttype of
  1896. aitconst_8bit :
  1897. result:=1;
  1898. aitconst_16bit,aitconst_16bit_unaligned :
  1899. result:=2;
  1900. aitconst_32bit,aitconst_darwin_dwarf_delta32,
  1901. aitconst_32bit_unaligned:
  1902. result:=4;
  1903. aitconst_64bit,aitconst_darwin_dwarf_delta64,
  1904. aitconst_64bit_unaligned:
  1905. result:=8;
  1906. aitconst_secrel32_symbol,
  1907. aitconst_rva_symbol :
  1908. if target_info.system in systems_peoptplus then
  1909. result:=sizeof(longint)
  1910. else
  1911. result:=sizeof(pint);
  1912. aitconst_uleb128bit :
  1913. begin
  1914. if fixed_size>0 then
  1915. result:=fixed_size
  1916. else if sym=nil then
  1917. begin
  1918. FixSize;
  1919. result:=fixed_size;
  1920. end
  1921. else
  1922. { worst case }
  1923. result:=sizeof(pint)+2;
  1924. end;
  1925. aitconst_sleb128bit :
  1926. begin
  1927. if fixed_size>0 then
  1928. result:=fixed_size
  1929. else if sym=nil then
  1930. begin
  1931. FixSize;
  1932. result:=fixed_size;
  1933. end
  1934. else
  1935. { worst case }
  1936. result:=sizeof(pint)+2;
  1937. end;
  1938. aitconst_half16bit,
  1939. aitconst_gs:
  1940. result:=2;
  1941. aitconst_farptr:
  1942. result:=4;
  1943. aitconst_dgroup,
  1944. aitconst_fardataseg,
  1945. aitconst_seg:
  1946. result:=2;
  1947. aitconst_got:
  1948. result:=sizeof(pint);
  1949. aitconst_gotoff_symbol:
  1950. result:=4;
  1951. aitconst_gottpoff:
  1952. result:=4;
  1953. aitconst_tlsgd:
  1954. result:=4;
  1955. aitconst_tpoff:
  1956. result:=4;
  1957. aitconst_tlsdesc:
  1958. result:=4;
  1959. aitconst_dtpoff:
  1960. result:=4;
  1961. else
  1962. internalerror(200603253);
  1963. end;
  1964. end;
  1965. procedure tai_const.FixSize;
  1966. begin
  1967. case consttype of
  1968. aitconst_uleb128bit:
  1969. fixed_size:=LengthUleb128(qword(value));
  1970. aitconst_sleb128bit:
  1971. fixed_size:=LengthSleb128(value);
  1972. else
  1973. Internalerror(2019030301);
  1974. end;
  1975. end;
  1976. {****************************************************************************
  1977. TAI_realconst
  1978. ****************************************************************************}
  1979. constructor tai_realconst.create_s32real(val: ts32real);
  1980. begin
  1981. inherited create;
  1982. typ:=ait_realconst;
  1983. realtyp:=aitrealconst_s32bit;
  1984. savesize:=4;
  1985. value.s32val:=val;
  1986. end;
  1987. constructor tai_realconst.create_s64real(val: ts64real);
  1988. begin
  1989. inherited create;
  1990. typ:=ait_realconst;
  1991. realtyp:=aitrealconst_s64bit;
  1992. savesize:=8;
  1993. value.s64val:=val;
  1994. end;
  1995. {$ifdef ARM}
  1996. constructor tai_realconst.create_s64real_hiloswapped(val : ts64real);
  1997. begin
  1998. inherited create;
  1999. typ:=ait_realconst;
  2000. realtyp:=aitrealconst_s64bit;
  2001. value.s64val:=val;
  2002. savesize:=8;
  2003. formatoptions:=fo_hiloswapped;
  2004. end;
  2005. {$endif ARM}
  2006. constructor tai_realconst.create_s80real(val: ts80real; _savesize: byte);
  2007. begin
  2008. inherited create;
  2009. typ:=ait_realconst;
  2010. realtyp:=aitrealconst_s80bit;
  2011. savesize:=_savesize;
  2012. value.s80val:=val;
  2013. end;
  2014. constructor tai_realconst.create_s128real(val: ts128real);
  2015. begin
  2016. inherited create;
  2017. typ:=ait_realconst;
  2018. realtyp:=aitrealconst_s128bit;
  2019. savesize:=16;
  2020. value.s128val:=val;
  2021. end;
  2022. constructor tai_realconst.create_s64compreal(val: ts64comp);
  2023. begin
  2024. inherited create;
  2025. typ:=ait_realconst;
  2026. realtyp:=aitrealconst_s64comp;
  2027. savesize:=8;
  2028. value.s64compval:=val;
  2029. end;
  2030. constructor tai_realconst.ppuload(t: taitype; ppufile: tcompilerppufile);
  2031. begin
  2032. inherited;
  2033. realtyp:=tairealconsttype(ppufile.getbyte);
  2034. {$ifdef ARM}
  2035. formatoptions:=tformatoptions(ppufile.getbyte);
  2036. {$endif ARM}
  2037. case realtyp of
  2038. aitrealconst_s32bit:
  2039. value.s32val:=ppufile.getreal;
  2040. aitrealconst_s64bit:
  2041. value.s64val:=ppufile.getreal;
  2042. aitrealconst_s80bit:
  2043. value.s80val:=ppufile.getreal;
  2044. aitrealconst_s128bit:
  2045. value.s128val:=ppufile.getreal;
  2046. aitrealconst_s64comp:
  2047. value.s64compval:=comp(ppufile.getint64);
  2048. end;
  2049. end;
  2050. procedure tai_realconst.ppuwrite(ppufile: tcompilerppufile);
  2051. var
  2052. c: comp;
  2053. begin
  2054. inherited ppuwrite(ppufile);
  2055. ppufile.putbyte(byte(realtyp));
  2056. {$ifdef ARM}
  2057. ppufile.putbyte(byte(formatoptions));
  2058. {$endif ARM}
  2059. case realtyp of
  2060. aitrealconst_s32bit:
  2061. ppufile.putreal(value.s32val);
  2062. aitrealconst_s64bit:
  2063. ppufile.putreal(value.s64val);
  2064. aitrealconst_s80bit:
  2065. ppufile.putreal(value.s80val);
  2066. aitrealconst_s128bit:
  2067. ppufile.putreal(value.s128val);
  2068. aitrealconst_s64comp:
  2069. begin
  2070. c:=comp(value.s64compval);
  2071. ppufile.putint64(int64(c));
  2072. end
  2073. end;
  2074. end;
  2075. function tai_realconst.getcopy: tlinkedlistitem;
  2076. begin
  2077. result:=inherited getcopy;
  2078. tai_realconst(result).value:=value;
  2079. tai_realconst(result).realtyp:=realtyp;
  2080. tai_realconst(result).savesize:=savesize;
  2081. {$ifdef ARM}
  2082. tai_realconst(result).formatoptions:=formatoptions;
  2083. {$endif ARM}
  2084. end;
  2085. function tai_realconst.datasize: word;
  2086. begin
  2087. result:=0;
  2088. case realtyp of
  2089. aitrealconst_s32bit:
  2090. result:=4;
  2091. aitrealconst_s64bit,
  2092. aitrealconst_s64comp:
  2093. result:=8;
  2094. aitrealconst_s80bit:
  2095. result:=10;
  2096. aitrealconst_s128bit:
  2097. result:=16;
  2098. end;
  2099. end;
  2100. {****************************************************************************
  2101. TAI_STRING
  2102. ****************************************************************************}
  2103. constructor tai_string.Create(const _str : string);
  2104. var
  2105. lNewLen : Integer;
  2106. begin
  2107. inherited Create;
  2108. typ:=ait_string;
  2109. lNewLen:=length(_str);
  2110. setlength(str,lNewLen);
  2111. if lNewLen>0 then
  2112. move(_str[1],str[0],lNewLen);
  2113. end;
  2114. constructor tai_string.Create(const _str: ansistring);
  2115. var
  2116. lNewLen : Integer;
  2117. begin
  2118. inherited Create;
  2119. typ:=ait_string;
  2120. lNewLen:=length(_str);
  2121. setlength(str,lNewlen);
  2122. if lNewLen>0 then
  2123. move(_str[1],str[0],lNewLen);
  2124. end;
  2125. constructor tai_string.Create_Data(data : pchar;length : longint; add0: boolean);
  2126. begin
  2127. inherited Create;
  2128. typ:=ait_string;
  2129. setlength(str,length+ord(add0));
  2130. if length>0 then
  2131. move(data^,str[0],length);
  2132. if add0 then
  2133. str[length]:=#0;
  2134. end;
  2135. destructor tai_string.Destroy;
  2136. begin
  2137. inherited Destroy;
  2138. end;
  2139. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  2140. var
  2141. lNewLen : integer;
  2142. begin
  2143. inherited ppuload(t,ppufile);
  2144. lNewLen:=ppufile.getlongint;
  2145. setlength(str,lNewLen);
  2146. ppufile.getdata(str[0],lnewlen);
  2147. end;
  2148. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  2149. var
  2150. lWriteLen : integer;
  2151. begin
  2152. inherited ppuwrite(ppufile);
  2153. lWriteLen:=length(str);
  2154. ppufile.putlongint(lWriteLen);
  2155. ppufile.putdata(str[0],lWriteLen);
  2156. end;
  2157. function tai_string.getcopy : tlinkedlistitem;
  2158. var
  2159. p : tlinkedlistitem;
  2160. lWriteLen : integer;
  2161. begin
  2162. p:=inherited getcopy;
  2163. lWriteLen:=length(str);
  2164. setlength(tai_string(p).str,lWriteLen);
  2165. move(str[0],tai_string(p).str[0],lWriteLen);
  2166. getcopy:=p;
  2167. end;
  2168. function tai_string.len: integer;
  2169. begin
  2170. Result:=Length(str);
  2171. end;
  2172. {****************************************************************************
  2173. TAI_LABEL
  2174. ****************************************************************************}
  2175. constructor tai_label.Create(_labsym : tasmlabel);
  2176. begin
  2177. inherited Create;
  2178. typ:=ait_label;
  2179. labsym:=_labsym;
  2180. labsym.is_set:=true;
  2181. end;
  2182. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  2183. begin
  2184. inherited ppuload(t,ppufile);
  2185. labsym:=tasmlabel(ppufile.getasmsymbol);
  2186. ppufile.getbyte; { was is_global flag, now unused }
  2187. end;
  2188. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  2189. begin
  2190. inherited ppuwrite(ppufile);
  2191. ppufile.putasmsymbol(labsym);
  2192. ppufile.putbyte(0); { was is_global flag, now unused }
  2193. end;
  2194. procedure tai_label.derefimpl;
  2195. begin
  2196. labsym.is_set:=true;
  2197. end;
  2198. {****************************************************************************
  2199. tai_comment comment to be inserted in the assembler file
  2200. ****************************************************************************}
  2201. constructor tai_comment.Create(_str : pchar);
  2202. begin
  2203. inherited Create;
  2204. typ:=ait_comment;
  2205. str:=_str;
  2206. end;
  2207. destructor tai_comment.destroy;
  2208. begin
  2209. freemem(str);
  2210. inherited Destroy;
  2211. end;
  2212. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  2213. var
  2214. len : longint;
  2215. begin
  2216. inherited ppuload(t,ppufile);
  2217. len:=ppufile.getlongint;
  2218. getmem(str,len+1);
  2219. ppufile.getdata(str^,len);
  2220. str[len]:=#0;
  2221. end;
  2222. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  2223. var
  2224. len : longint;
  2225. begin
  2226. inherited ppuwrite(ppufile);
  2227. len:=strlen(str);
  2228. ppufile.putlongint(len);
  2229. ppufile.putdata(str^,len);
  2230. end;
  2231. function tai_comment.getcopy : tlinkedlistitem;
  2232. var
  2233. p : tlinkedlistitem;
  2234. begin
  2235. p:=inherited getcopy;
  2236. getmem(tai_comment(p).str,strlen(str)+1);
  2237. move(str^,tai_comment(p).str^,strlen(str)+1);
  2238. getcopy:=p;
  2239. end;
  2240. {****************************************************************************
  2241. TAI_STABS
  2242. ****************************************************************************}
  2243. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  2244. begin
  2245. inherited create;
  2246. typ:=ait_stab;
  2247. str:=_str;
  2248. stabtype:=_stabtype;
  2249. end;
  2250. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  2251. begin
  2252. self.create(_stabtype,strpnew(s));
  2253. end;
  2254. constructor tai_stab.create_ansistr(_stabtype:TStabType;const s:ansistring);
  2255. begin
  2256. inherited create;
  2257. typ:=ait_stab;
  2258. stabtype:=_stabtype;
  2259. getmem(str,length(s)+1);
  2260. if length(s)>0 then
  2261. move(s[1],str^,length(s)+1)
  2262. else
  2263. str^:=#0;
  2264. end;
  2265. destructor tai_stab.destroy;
  2266. begin
  2267. freemem(str);
  2268. inherited destroy;
  2269. end;
  2270. {****************************************************************************
  2271. TAI_FORCE_LINE
  2272. ****************************************************************************}
  2273. constructor tai_force_line.create;
  2274. begin
  2275. inherited create;
  2276. typ:=ait_force_line;
  2277. end;
  2278. {****************************************************************************
  2279. TAI_FUNCTION_NAME
  2280. ****************************************************************************}
  2281. constructor tai_function_name.create(const s:string);
  2282. begin
  2283. inherited create;
  2284. typ:=ait_function_name;
  2285. funcname:=stringdup(s);
  2286. end;
  2287. destructor tai_function_name.destroy;
  2288. begin
  2289. stringdispose(funcname);
  2290. inherited destroy;
  2291. end;
  2292. {****************************************************************************
  2293. TAI_CUTOBJECT
  2294. ****************************************************************************}
  2295. constructor tai_cutobject.Create;
  2296. begin
  2297. inherited Create;
  2298. typ:=ait_cutobject;
  2299. place:=cut_normal;
  2300. end;
  2301. constructor tai_cutobject.Create_begin;
  2302. begin
  2303. inherited Create;
  2304. typ:=ait_cutobject;
  2305. place:=cut_begin;
  2306. end;
  2307. constructor tai_cutobject.Create_end;
  2308. begin
  2309. inherited Create;
  2310. typ:=ait_cutobject;
  2311. place:=cut_end;
  2312. end;
  2313. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  2314. begin
  2315. inherited ppuload(t,ppufile);
  2316. place:=TCutPlace(ppufile.getbyte);
  2317. end;
  2318. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  2319. begin
  2320. inherited ppuwrite(ppufile);
  2321. ppufile.putbyte(byte(place));
  2322. end;
  2323. {****************************************************************************
  2324. Tai_Marker
  2325. ****************************************************************************}
  2326. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  2327. begin
  2328. Inherited Create;
  2329. typ := ait_marker;
  2330. Kind := _Kind;
  2331. end;
  2332. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  2333. begin
  2334. inherited ppuload(t,ppufile);
  2335. kind:=TAsmMarker(ppufile.getbyte);
  2336. end;
  2337. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  2338. begin
  2339. inherited ppuwrite(ppufile);
  2340. ppufile.putbyte(byte(kind));
  2341. end;
  2342. {*****************************************************************************
  2343. tai_tempalloc
  2344. *****************************************************************************}
  2345. constructor tai_tempalloc.alloc(pos,size:longint);
  2346. begin
  2347. inherited Create;
  2348. typ:=ait_tempalloc;
  2349. allocation:=true;
  2350. temppos:=pos;
  2351. tempsize:=size;
  2352. {$ifdef EXTDEBUG}
  2353. problem:=nil;
  2354. {$endif EXTDEBUG}
  2355. end;
  2356. destructor tai_tempalloc.destroy;
  2357. begin
  2358. {$ifdef EXTDEBUG}
  2359. stringdispose(problem);
  2360. {$endif EXTDEBUG}
  2361. inherited destroy;
  2362. end;
  2363. constructor tai_tempalloc.dealloc(pos,size:longint);
  2364. begin
  2365. inherited Create;
  2366. typ:=ait_tempalloc;
  2367. allocation:=false;
  2368. temppos:=pos;
  2369. tempsize:=size;
  2370. {$ifdef EXTDEBUG}
  2371. problem:=nil;
  2372. {$endif EXTDEBUG}
  2373. end;
  2374. {$ifdef EXTDEBUG}
  2375. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  2376. begin
  2377. inherited Create;
  2378. typ:=ait_tempalloc;
  2379. allocation:=false;
  2380. temppos:=pos;
  2381. tempsize:=size;
  2382. problem:=stringdup(st);
  2383. end;
  2384. {$endif EXTDEBUG}
  2385. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2386. begin
  2387. inherited ppuload(t,ppufile);
  2388. temppos:=ppufile.getlongint;
  2389. tempsize:=ppufile.getlongint;
  2390. allocation:=ppufile.getboolean;
  2391. {$ifdef EXTDEBUG}
  2392. problem:=nil;
  2393. {$endif EXTDEBUG}
  2394. end;
  2395. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  2396. begin
  2397. inherited ppuwrite(ppufile);
  2398. ppufile.putlongint(temppos);
  2399. ppufile.putlongint(tempsize);
  2400. ppufile.putboolean(allocation);
  2401. end;
  2402. {*****************************************************************************
  2403. tai_regalloc
  2404. *****************************************************************************}
  2405. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  2406. begin
  2407. inherited create;
  2408. typ:=ait_regalloc;
  2409. ratype:=ra_alloc;
  2410. reg:=r;
  2411. { ainstr must be an instruction }
  2412. if assigned(ainstr) and
  2413. (ainstr.typ<>ait_instruction) then
  2414. internalerror(200411011);
  2415. instr:=ainstr;
  2416. end;
  2417. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  2418. begin
  2419. inherited create;
  2420. typ:=ait_regalloc;
  2421. ratype:=ra_dealloc;
  2422. reg:=r;
  2423. { ainstr must be an instruction }
  2424. if assigned(ainstr) and
  2425. (ainstr.typ<>ait_instruction) then
  2426. internalerror(200411012);
  2427. instr:=ainstr;
  2428. end;
  2429. constructor tai_regalloc.sync(r : tregister);
  2430. begin
  2431. inherited create;
  2432. typ:=ait_regalloc;
  2433. ratype:=ra_sync;
  2434. reg:=r;
  2435. end;
  2436. constructor tai_regalloc.resize(r : tregister);
  2437. begin
  2438. inherited create;
  2439. typ:=ait_regalloc;
  2440. ratype:=ra_resize;
  2441. reg:=r;
  2442. end;
  2443. constructor tai_regalloc.markused(r : tregister);
  2444. begin
  2445. inherited create;
  2446. typ:=ait_regalloc;
  2447. ratype:=ra_markused;
  2448. reg:=r;
  2449. end;
  2450. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  2451. begin
  2452. inherited ppuload(t,ppufile);
  2453. ppufile.getdata(reg,sizeof(Tregister));
  2454. ratype:=tregalloctype(ppufile.getbyte);
  2455. keep:=ppufile.getboolean;
  2456. end;
  2457. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  2458. begin
  2459. inherited ppuwrite(ppufile);
  2460. ppufile.putdata(reg,sizeof(Tregister));
  2461. ppufile.putbyte(byte(ratype));
  2462. ppufile.putboolean(keep);
  2463. end;
  2464. {*****************************************************************************
  2465. TaiInstruction
  2466. *****************************************************************************}
  2467. constructor tai_cpu_abstract.Create(op : tasmop);
  2468. begin
  2469. inherited create;
  2470. typ:=ait_instruction;
  2471. is_jmp:=false;
  2472. opcode:=op;
  2473. ops:=0;
  2474. end;
  2475. destructor tai_cpu_abstract.Destroy;
  2476. var
  2477. i : integer;
  2478. begin
  2479. for i:=0 to opercnt-1 do
  2480. freeop(i);
  2481. inherited destroy;
  2482. end;
  2483. { ---------------------------------------------------------------------
  2484. Loading of operands.
  2485. ---------------------------------------------------------------------}
  2486. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  2487. begin
  2488. while (opers>opercnt) do
  2489. begin
  2490. new(oper[opercnt]);
  2491. fillchar(oper[opercnt]^,sizeof(toper),0);
  2492. inc(opercnt);
  2493. end;
  2494. end;
  2495. procedure tai_cpu_abstract.loadconst(opidx:longint;l:tcgint);
  2496. begin
  2497. allocate_oper(opidx+1);
  2498. with oper[opidx]^ do
  2499. begin
  2500. if typ<>top_const then
  2501. clearop(opidx);
  2502. val:=l;
  2503. typ:=top_const;
  2504. end;
  2505. end;
  2506. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  2507. var
  2508. r : treference;
  2509. begin
  2510. reference_reset_symbol(r,s,sofs,1,[]);
  2511. r.refaddr:=addr_full;
  2512. loadref(opidx,r);
  2513. end;
  2514. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  2515. begin
  2516. if not assigned(s) then
  2517. internalerror(200204251);
  2518. allocate_oper(opidx+1);
  2519. with oper[opidx]^ do
  2520. begin
  2521. if typ<>top_local then
  2522. begin
  2523. clearop(opidx);
  2524. new(localoper);
  2525. end;
  2526. with oper[opidx]^.localoper^ do
  2527. begin
  2528. localsym:=s;
  2529. localsymofs:=sofs;
  2530. localindexreg:=indexreg;
  2531. localscale:=scale;
  2532. localgetoffset:=getoffset;
  2533. localforceref:=forceref;
  2534. {$ifdef x86}
  2535. localsegment:=NR_NO;
  2536. {$endif x86}
  2537. end;
  2538. typ:=top_local;
  2539. end;
  2540. end;
  2541. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  2542. {$ifdef x86}
  2543. var
  2544. si_param: ShortInt;
  2545. {$endif}
  2546. begin
  2547. allocate_oper(opidx+1);
  2548. with oper[opidx]^ do
  2549. begin
  2550. if typ<>top_ref then
  2551. begin
  2552. clearop(opidx);
  2553. new(ref);
  2554. end;
  2555. ref^:=r;
  2556. {$ifdef x86}
  2557. { We allow this exception for x86, since overloading this would be
  2558. too much of a a speed penalty}
  2559. if is_x86_parameterized_string_op(opcode) then
  2560. begin
  2561. si_param:=get_x86_string_op_si_param(opcode);
  2562. if (si_param<>-1) and (taicpu(self).OperandOrder=op_att) then
  2563. si_param:=x86_parameterized_string_op_param_count(opcode)-si_param-1;
  2564. if (si_param=opidx) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2565. segprefix:=ref^.segment;
  2566. end
  2567. else if (opcode=A_XLAT) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2568. segprefix:=ref^.segment
  2569. else if (ref^.segment<>NR_NO) and (ref^.segment<>get_default_segment_of_ref(ref^)) then
  2570. segprefix:=ref^.segment;
  2571. {$endif}
  2572. {$ifndef llvm}
  2573. if (cs_create_pic in current_settings.moduleswitches) and
  2574. assigned(r.symbol) and
  2575. not assigned(r.relsymbol) and
  2576. (r.refaddr=addr_no)
  2577. {$ifdef ARM}
  2578. and not(r.base=NR_R15)
  2579. {$endif ARM}
  2580. {$ifdef aarch64}
  2581. and not(r.refaddr in [addr_full,addr_gotpageoffset,addr_gotpage])
  2582. {$endif aarch64}
  2583. {$ifdef riscv}
  2584. and not(opcode=A_LA)
  2585. and not(opcode=A_FLD)
  2586. and not(opcode=A_FLQ)
  2587. and not(opcode=A_FLW)
  2588. {$endif riscv}
  2589. then
  2590. internalerror(200502052);
  2591. {$endif not llvm}
  2592. typ:=top_ref;
  2593. if assigned(add_reg_instruction_hook) then
  2594. begin
  2595. add_reg_instruction_hook(self,ref^.base);
  2596. add_reg_instruction_hook(self,ref^.index);
  2597. end;
  2598. { mark symbol as used }
  2599. if assigned(ref^.symbol) then
  2600. ref^.symbol.increfs;
  2601. if assigned(ref^.relsymbol) then
  2602. ref^.relsymbol.increfs;
  2603. end;
  2604. end;
  2605. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  2606. begin
  2607. allocate_oper(opidx+1);
  2608. with oper[opidx]^ do
  2609. begin
  2610. if typ<>top_reg then
  2611. clearop(opidx);
  2612. reg:=r;
  2613. typ:=top_reg;
  2614. end;
  2615. if assigned(add_reg_instruction_hook) then
  2616. add_reg_instruction_hook(self,r);
  2617. {$ifdef ARM}
  2618. { R15 is the PC on the ARM thus moves to R15 are jumps.
  2619. Due to speed considerations we don't use a virtual overridden method here.
  2620. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  2621. problems with iregs getting r15.
  2622. }
  2623. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  2624. {$endif ARM}
  2625. end;
  2626. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  2627. {$ifdef x86}
  2628. var
  2629. si_param: ShortInt;
  2630. {$endif x86}
  2631. begin
  2632. allocate_oper(opidx+1);
  2633. clearop(opidx);
  2634. oper[opidx]^:=o;
  2635. { copy also the reference }
  2636. with oper[opidx]^ do
  2637. begin
  2638. case typ of
  2639. top_reg:
  2640. begin
  2641. if assigned(add_reg_instruction_hook) then
  2642. add_reg_instruction_hook(self,reg);
  2643. end;
  2644. top_ref:
  2645. begin
  2646. new(ref);
  2647. ref^:=o.ref^;
  2648. {$ifdef x86}
  2649. { We allow this exception for x86, since overloading this would be
  2650. too much of a a speed penalty}
  2651. if is_x86_parameterized_string_op(opcode) then
  2652. begin
  2653. si_param:=get_x86_string_op_si_param(opcode);
  2654. if (si_param<>-1) and (taicpu(self).OperandOrder=op_att) then
  2655. si_param:=x86_parameterized_string_op_param_count(opcode)-si_param-1;
  2656. if (si_param=opidx) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2657. segprefix:=ref^.segment;
  2658. end
  2659. else if (opcode=A_XLAT) and (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  2660. segprefix:=ref^.segment
  2661. else if (ref^.segment<>NR_NO) and (ref^.segment<>get_default_segment_of_ref(ref^)) then
  2662. segprefix:=ref^.segment;
  2663. {$endif x86}
  2664. if assigned(add_reg_instruction_hook) then
  2665. begin
  2666. add_reg_instruction_hook(self,ref^.base);
  2667. add_reg_instruction_hook(self,ref^.index);
  2668. end;
  2669. end;
  2670. {$ifdef ARM}
  2671. top_shifterop:
  2672. begin
  2673. new(shifterop);
  2674. shifterop^:=o.shifterop^;
  2675. if assigned(add_reg_instruction_hook) then
  2676. add_reg_instruction_hook(self,shifterop^.rs);
  2677. end;
  2678. {$endif ARM}
  2679. else
  2680. ;
  2681. end;
  2682. end;
  2683. end;
  2684. procedure tai_cpu_abstract.clearop(opidx:longint);
  2685. begin
  2686. with oper[opidx]^ do
  2687. begin
  2688. case typ of
  2689. top_ref:
  2690. dispose(ref);
  2691. top_local:
  2692. dispose(localoper);
  2693. {$ifdef ARM}
  2694. top_shifterop:
  2695. dispose(shifterop);
  2696. top_regset:
  2697. dispose(regset);
  2698. {$endif ARM}
  2699. {$ifdef jvm}
  2700. top_string:
  2701. freemem(pcval);
  2702. top_wstring:
  2703. donewidestring(pwstrval);
  2704. {$endif jvm}
  2705. {$ifdef wasm}
  2706. top_functype:
  2707. FreeAndNil(functype);
  2708. {$endif wasm}
  2709. else
  2710. ;
  2711. end;
  2712. typ:=top_none;
  2713. end;
  2714. end;
  2715. procedure tai_cpu_abstract.freeop(opidx:longint);
  2716. begin
  2717. clearop(opidx);
  2718. dispose(oper[opidx]);
  2719. end;
  2720. { ---------------------------------------------------------------------
  2721. Miscellaneous methods.
  2722. ---------------------------------------------------------------------}
  2723. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  2724. begin
  2725. condition:=c;
  2726. end;
  2727. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  2728. var
  2729. i : longint;
  2730. p : tai_cpu_abstract;
  2731. begin
  2732. p:=tai_cpu_abstract(inherited getcopy);
  2733. { make a copy of the references }
  2734. p.opercnt:=0;
  2735. p.allocate_oper(ops);
  2736. for i:=0 to ops-1 do
  2737. begin
  2738. p.oper[i]^:=oper[i]^;
  2739. case oper[i]^.typ of
  2740. top_local :
  2741. begin
  2742. new(p.oper[i]^.localoper);
  2743. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  2744. end;
  2745. top_ref :
  2746. begin
  2747. new(p.oper[i]^.ref);
  2748. p.oper[i]^.ref^:=oper[i]^.ref^;
  2749. if Assigned(p.oper[i]^.ref^.symbol) then
  2750. p.oper[i]^.ref^.symbol.increfs;
  2751. if Assigned(p.oper[i]^.ref^.relsymbol) then
  2752. p.oper[i]^.ref^.relsymbol.increfs;
  2753. {$ifdef jvm}
  2754. if Assigned(p.oper[i]^.ref^.indexsymbol) then
  2755. p.oper[i]^.ref^.indexsymbol.increfs;
  2756. {$endif jvm}
  2757. end;
  2758. {$ifdef ARM}
  2759. top_regset:
  2760. begin
  2761. new(p.oper[i]^.regset);
  2762. p.oper[i]^.regset^:=oper[i]^.regset^;
  2763. end;
  2764. top_shifterop:
  2765. begin
  2766. new(p.oper[i]^.shifterop);
  2767. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  2768. end;
  2769. {$endif ARM}
  2770. else
  2771. ;
  2772. end;
  2773. end;
  2774. getcopy:=p;
  2775. end;
  2776. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  2777. begin
  2778. { When the generic RA is used this needs to be overridden, we don't use
  2779. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  2780. when tai_cpu is created (PFV) }
  2781. internalerror(2004040901);
  2782. result:=false;
  2783. end;
  2784. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  2785. begin
  2786. internalerror(2004040902);
  2787. result:=operand_readwrite;
  2788. end;
  2789. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  2790. begin
  2791. result := operand_read;
  2792. end;
  2793. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2794. var
  2795. i : integer;
  2796. begin
  2797. inherited ppuload(t,ppufile);
  2798. { hopefully, we don't get problems with big/little endian here when cross compiling :/ }
  2799. ppufile.getdata(condition,sizeof(tasmcond));
  2800. ops := ppufile.getbyte;
  2801. allocate_oper(ops);
  2802. for i:=0 to ops-1 do
  2803. ppuloadoper(ppufile,oper[i]^);
  2804. opcode:=tasmop(ppufile.getword);
  2805. {$ifdef x86}
  2806. ppufile.getdata(segprefix,sizeof(Tregister));
  2807. {$endif x86}
  2808. is_jmp:=ppufile.getboolean;
  2809. end;
  2810. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  2811. var
  2812. i : integer;
  2813. begin
  2814. inherited ppuwrite(ppufile);
  2815. ppufile.putdata(condition,sizeof(tasmcond));
  2816. ppufile.putbyte(ops);
  2817. for i:=0 to ops-1 do
  2818. ppuwriteoper(ppufile,oper[i]^);
  2819. ppufile.putword(word(opcode));
  2820. {$ifdef x86}
  2821. ppufile.putdata(segprefix,sizeof(Tregister));
  2822. {$endif x86}
  2823. ppufile.putboolean(is_jmp);
  2824. end;
  2825. procedure tai_cpu_abstract.buildderefimpl;
  2826. var
  2827. i : integer;
  2828. begin
  2829. for i:=0 to ops-1 do
  2830. ppubuildderefimploper(oper[i]^);
  2831. end;
  2832. procedure tai_cpu_abstract.derefimpl;
  2833. var
  2834. i : integer;
  2835. begin
  2836. for i:=0 to ops-1 do
  2837. ppuderefoper(oper[i]^);
  2838. end;
  2839. procedure tai_cpu_abstract.resetpass1;
  2840. begin
  2841. end;
  2842. procedure tai_cpu_abstract.resetpass2;
  2843. begin
  2844. end;
  2845. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  2846. begin
  2847. result:=0;
  2848. end;
  2849. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  2850. begin
  2851. end;
  2852. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  2853. begin
  2854. o.typ:=toptype(ppufile.getbyte);
  2855. o.ot:=ppufile.getlongint;
  2856. case o.typ of
  2857. top_reg :
  2858. ppufile.getdata(o.reg,sizeof(Tregister));
  2859. top_ref :
  2860. begin
  2861. new(o.ref);
  2862. {$ifdef x86}
  2863. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  2864. {$endif x86}
  2865. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  2866. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  2867. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2868. o.ref^.scalefactor:=ppufile.getbyte;
  2869. o.ref^.offset:=ppufile.getaint;
  2870. o.ref^.symbol:=ppufile.getasmsymbol;
  2871. o.ref^.relsymbol:=ppufile.getasmsymbol;
  2872. end;
  2873. top_const :
  2874. o.val:=ppufile.getaint;
  2875. top_local :
  2876. begin
  2877. new(o.localoper);
  2878. with o.localoper^ do
  2879. begin
  2880. ppufile.getderef(localsymderef);
  2881. localsymofs:=ppufile.getaint;
  2882. {$ifdef x86}
  2883. localsegment:=tregister(ppufile.getlongint);
  2884. {$endif x86}
  2885. localindexreg:=tregister(ppufile.getlongint);
  2886. localscale:=ppufile.getbyte;
  2887. localgetoffset:=(ppufile.getbyte<>0);
  2888. end;
  2889. end;
  2890. else
  2891. internalerror(2007010210);
  2892. end;
  2893. end;
  2894. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  2895. begin
  2896. ppufile.putbyte(byte(o.typ));
  2897. ppufile.putlongint(o.ot);
  2898. case o.typ of
  2899. top_reg :
  2900. ppufile.putdata(o.reg,sizeof(Tregister));
  2901. top_ref :
  2902. begin
  2903. {$ifdef x86}
  2904. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  2905. {$endif x86}
  2906. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  2907. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  2908. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  2909. ppufile.putbyte(o.ref^.scalefactor);
  2910. ppufile.putaint(o.ref^.offset);
  2911. ppufile.putasmsymbol(o.ref^.symbol);
  2912. ppufile.putasmsymbol(o.ref^.relsymbol);
  2913. end;
  2914. top_const :
  2915. ppufile.putaint(o.val);
  2916. top_local :
  2917. begin
  2918. with o.localoper^ do
  2919. begin
  2920. ppufile.putderef(localsymderef);
  2921. ppufile.putaint(localsymofs);
  2922. {$ifdef x86}
  2923. ppufile.putlongint(longint(localsegment));
  2924. {$endif x86}
  2925. ppufile.putlongint(longint(localindexreg));
  2926. ppufile.putbyte(localscale);
  2927. ppufile.putbyte(byte(localgetoffset));
  2928. end;
  2929. end;
  2930. else
  2931. internalerror(2007010211);
  2932. end;
  2933. end;
  2934. {****************************************************************************
  2935. tai_align_abstract
  2936. ****************************************************************************}
  2937. constructor tai_align_abstract.Create(b: byte);
  2938. begin
  2939. inherited Create;
  2940. typ:=ait_align;
  2941. {$ifdef EXTDEBUG}
  2942. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  2943. internalerror(200709191);
  2944. {$endif EXTDEBUG}
  2945. if b in [1,2,4,8,16,32,64] then
  2946. aligntype := b
  2947. else
  2948. aligntype := 1;
  2949. fillsize:=0;
  2950. fillop:=0;
  2951. use_op:=false;
  2952. maxbytes:=aligntype;
  2953. end;
  2954. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2955. begin
  2956. inherited Create;
  2957. typ:=ait_align;
  2958. if b in [1,2,4,8,16,32] then
  2959. aligntype := b
  2960. else
  2961. aligntype := 1;
  2962. fillsize:=0;
  2963. fillop:=_op;
  2964. use_op:=true;
  2965. maxbytes:=aligntype;
  2966. end;
  2967. constructor tai_align_abstract.create_max(b : byte; max : byte);
  2968. begin
  2969. inherited Create;
  2970. typ:=ait_align;
  2971. if b in [1,2,4,8,16,32] then
  2972. aligntype := b
  2973. else
  2974. aligntype := 1;
  2975. maxbytes:=max;
  2976. fillsize:=0;
  2977. fillop:=0;
  2978. use_op:=false;
  2979. end;
  2980. constructor tai_align_abstract.create_op_max(b: byte; _op: byte; max: byte);
  2981. begin
  2982. inherited Create;
  2983. typ:=ait_align;
  2984. if b in [1,2,4,8,16,32] then
  2985. aligntype := b
  2986. else
  2987. aligntype := 1;
  2988. fillop:=_op;
  2989. use_op:=true;
  2990. maxbytes:=max;
  2991. fillsize:=0;
  2992. end;
  2993. constructor tai_align_abstract.Create_zeros(b: byte);
  2994. begin
  2995. inherited Create;
  2996. typ:=ait_align;
  2997. if b in [1,2,4,8,16,32] then
  2998. aligntype := b
  2999. else
  3000. aligntype := 1;
  3001. use_op:=true;
  3002. fillsize:=0;
  3003. fillop:=0;
  3004. maxbytes:=aligntype;
  3005. end;
  3006. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer;executable : boolean):pchar;
  3007. begin
  3008. if fillsize>sizeof(buf) then
  3009. internalerror(200404293);
  3010. fillchar(buf,high(buf),fillop);
  3011. calculatefillbuf:=pchar(@buf);
  3012. end;
  3013. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  3014. begin
  3015. inherited ppuload(t,ppufile);
  3016. aligntype:=ppufile.getbyte;
  3017. fillsize:=0;
  3018. fillop:=ppufile.getbyte;
  3019. use_op:=ppufile.getboolean;
  3020. maxbytes:=ppufile.getbyte;
  3021. end;
  3022. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  3023. begin
  3024. inherited ppuwrite(ppufile);
  3025. ppufile.putbyte(aligntype);
  3026. ppufile.putbyte(fillop);
  3027. ppufile.putboolean(use_op);
  3028. ppufile.putbyte(maxbytes);
  3029. end;
  3030. {****************************************************************************
  3031. tai_seh_directive
  3032. ****************************************************************************}
  3033. const
  3034. datatypemap: array[TAsmSehDirective] of TSehDirectiveDatatype=(
  3035. sd_string, { proc }
  3036. sd_none, { endproc }
  3037. sd_none, { endprologue }
  3038. sd_string, { handler }
  3039. sd_none, { handlerdata }
  3040. sd_none,sd_none,sd_none, { eh, 32, no32 }
  3041. sd_regoffset, { setframe }
  3042. sd_offset, { stackalloc }
  3043. sd_reg, { pushreg }
  3044. sd_regoffset, { savereg }
  3045. sd_regoffset, { savereg_x }
  3046. sd_regoffset, { saveregp }
  3047. sd_regoffset, { saveregp_x }
  3048. sd_regoffset, { savexmm }
  3049. sd_regoffset, { savefreg }
  3050. sd_regoffset, { savefreg_x }
  3051. sd_regoffset, { savefregp }
  3052. sd_regoffset, { savefregp_x }
  3053. sd_none, { pushframe }
  3054. sd_none, { setfp }
  3055. sd_none, { addfp }
  3056. sd_offset, { savefplr }
  3057. sd_offset, { savefplr_x }
  3058. sd_none, { nop }
  3059. sd_reg, { pushnv }
  3060. sd_none { savenv }
  3061. );
  3062. constructor tai_seh_directive.create(_kind:TAsmSehDirective);
  3063. begin
  3064. inherited Create;
  3065. typ:=ait_seh_directive;
  3066. kind:=_kind;
  3067. data.typ:=datatypemap[_kind];
  3068. end;
  3069. constructor tai_seh_directive.create_name(_kind:TAsmSehDirective;const _name:string);
  3070. begin
  3071. create(_kind);
  3072. data.name:=stringdup(_name);
  3073. end;
  3074. constructor tai_seh_directive.create_reg(_kind:TAsmSehDirective;r:TRegister);
  3075. begin
  3076. create(_kind);
  3077. data.reg:=r;
  3078. end;
  3079. constructor tai_seh_directive.create_offset(_kind:TAsmSehDirective;ofs:dword);
  3080. begin
  3081. create(_kind);
  3082. data.offset:=ofs;
  3083. end;
  3084. constructor tai_seh_directive.create_reg_offset(_kind:TAsmSehDirective;
  3085. r:TRegister;ofs:dword);
  3086. begin
  3087. create(_kind);
  3088. data.offset:=ofs;
  3089. data.reg:=r;
  3090. end;
  3091. constructor tai_seh_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  3092. begin
  3093. inherited ppuload(t, ppufile);
  3094. kind:=TAsmSehDirective(ppufile.getbyte);
  3095. data.typ:=datatypemap[kind];
  3096. case data.typ of
  3097. sd_none: ;
  3098. sd_string:
  3099. begin
  3100. data.name:=ppufile.getpshortstring;
  3101. data.flags:=ppufile.getbyte;
  3102. end;
  3103. sd_reg,sd_offset,sd_regoffset:
  3104. begin
  3105. ppufile.getdata(data.reg,sizeof(TRegister));
  3106. data.offset:=ppufile.getdword;
  3107. end;
  3108. end;
  3109. end;
  3110. destructor tai_seh_directive.destroy;
  3111. begin
  3112. if data.typ=sd_string then
  3113. stringdispose(data.name);
  3114. inherited destroy;
  3115. end;
  3116. procedure tai_seh_directive.ppuwrite(ppufile:tcompilerppufile);
  3117. begin
  3118. inherited ppuwrite(ppufile);
  3119. ppufile.putbyte(ord(kind));
  3120. case data.typ of
  3121. sd_none: ;
  3122. sd_string:
  3123. begin
  3124. ppufile.putstring(data.name^);
  3125. ppufile.putbyte(data.flags);
  3126. end;
  3127. sd_reg,sd_offset,sd_regoffset:
  3128. begin
  3129. ppufile.putdata(data.reg,sizeof(TRegister));
  3130. ppufile.putdword(data.offset);
  3131. end;
  3132. end;
  3133. end;
  3134. procedure tai_seh_directive.generate_code(objdata:TObjData);
  3135. begin
  3136. end;
  3137. {****************************************************************************
  3138. tai_attribute
  3139. ****************************************************************************}
  3140. constructor tai_attribute.create(atyp : taitype; atag,avalue : dword);
  3141. begin
  3142. inherited Create;
  3143. typ:=atyp;
  3144. eattr_typ:=eattrtype_dword;
  3145. tag:=atag;
  3146. value:=avalue;
  3147. end;
  3148. constructor tai_attribute.create(atyp : taitype; atag: dword; const avalue: string);
  3149. begin
  3150. inherited Create;
  3151. typ:=atyp;
  3152. eattr_typ:=eattrtype_ntbs;
  3153. tag:=atag;
  3154. valuestr:=NewStr(avalue);
  3155. end;
  3156. destructor tai_attribute.destroy;
  3157. begin
  3158. Inherited Destroy;
  3159. end;
  3160. constructor tai_attribute.ppuload(t:taitype;ppufile:tcompilerppufile);
  3161. begin
  3162. end;
  3163. procedure tai_attribute.ppuwrite(ppufile:tcompilerppufile);
  3164. begin
  3165. inherited ppuwrite(ppufile);
  3166. ppufile.putdword(tag);
  3167. ppufile.putdword(value);
  3168. end;
  3169. {$ifdef JVM}
  3170. {****************************************************************************
  3171. tai_jvar
  3172. ****************************************************************************}
  3173. constructor tai_jvar.Create(_stackslot: longint; const _desc: shortstring; _startlab, _stoplab: TAsmSymbol);
  3174. begin
  3175. Inherited create;
  3176. typ:=ait_jvar;
  3177. stackslot:=_stackslot;
  3178. desc:=stringdup(_desc);
  3179. startlab:=_startlab;
  3180. startlab.increfs;
  3181. stoplab:=_stoplab;
  3182. stoplab.increfs;
  3183. end;
  3184. constructor tai_jvar.ppuload(t: taitype; ppufile: tcompilerppufile);
  3185. begin
  3186. inherited ppuload(t, ppufile);
  3187. stackslot:=ppufile.getlongint;
  3188. desc:=ppufile.getpshortstring;
  3189. startlab:=ppufile.getasmsymbol;
  3190. stoplab:=ppufile.getasmsymbol;
  3191. end;
  3192. procedure tai_jvar.ppuwrite(ppufile: tcompilerppufile);
  3193. begin
  3194. inherited ppuwrite(ppufile);
  3195. ppufile.putlongint(stackslot);
  3196. ppufile.putstring(desc^);
  3197. ppufile.putasmsymbol(startlab);
  3198. ppufile.putasmsymbol(stoplab);
  3199. end;
  3200. destructor tai_jvar.destroy;
  3201. begin
  3202. stringdispose(desc);
  3203. inherited destroy;
  3204. end;
  3205. {****************************************************************************
  3206. tai_jcatch
  3207. ****************************************************************************}
  3208. constructor tai_jcatch.Create(const _name: shortstring; _startlab, _stoplab, _handlerlab: TAsmSymbol);
  3209. begin
  3210. Inherited create;
  3211. typ:=ait_jcatch;
  3212. name:=stringdup(_name);
  3213. startlab:=_startlab;
  3214. startlab.increfs;
  3215. stoplab:=_stoplab;
  3216. stoplab.increfs;
  3217. handlerlab:=_handlerlab;
  3218. handlerlab.increfs;
  3219. end;
  3220. destructor tai_jcatch.destroy;
  3221. begin
  3222. stringdispose(name);
  3223. inherited destroy;
  3224. end;
  3225. constructor tai_jcatch.ppuload(t: taitype; ppufile: tcompilerppufile);
  3226. begin
  3227. inherited ppuload(t, ppufile);
  3228. name:=ppufile.getpshortstring;
  3229. startlab:=ppufile.getasmsymbol;
  3230. startlab.increfs;
  3231. stoplab:=ppufile.getasmsymbol;
  3232. stoplab.increfs;
  3233. handlerlab:=ppufile.getasmsymbol;
  3234. handlerlab.increfs;
  3235. end;
  3236. procedure tai_jcatch.ppuwrite(ppufile: tcompilerppufile);
  3237. begin
  3238. inherited ppuwrite(ppufile);
  3239. ppufile.putstring(name^);
  3240. ppufile.putasmsymbol(startlab);
  3241. ppufile.putasmsymbol(stoplab);
  3242. ppufile.putasmsymbol(handlerlab);
  3243. end;
  3244. {$endif JVM}
  3245. begin
  3246. {$if not defined(WASM) and not defined(LLVM)}
  3247. {$push}{$warnings off}
  3248. { taitype should fit into a 4 byte set for speed reasons }
  3249. if ord(high(taitype))>31 then
  3250. internalerror(201108181);
  3251. {$pop}
  3252. {$endif WASM}
  3253. end.