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