aasmtai.pas 93 KB

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