aasmtai.pas 94 KB

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