aasmtai.pas 93 KB

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