aasmtai.pas 111 KB

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