aasmtai.pas 93 KB

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