aasmtai.pas 111 KB

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