aasmtai.pas 94 KB

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