aasmtai.pas 108 KB

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