aasmtai.pas 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462
  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 overriden 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. cgbase,cgutils,
  30. symtype,
  31. aasmbase,aasmdata,ogbase;
  32. type
  33. { keep the number of elements in this enumeration less or equal than 32 as long
  34. as FPC knows only 4 byte and 32 byte sets (FK) }
  35. taitype = (
  36. ait_none,
  37. ait_align,
  38. ait_section,
  39. ait_comment,
  40. ait_string,
  41. ait_instruction,
  42. ait_datablock,
  43. ait_symbol,
  44. { needed to calc the size of a symbol }
  45. ait_symbol_end,
  46. ait_directive,
  47. ait_label,
  48. ait_const,
  49. ait_real_32bit,
  50. ait_real_64bit,
  51. ait_real_80bit,
  52. ait_comp_64bit,
  53. ait_real_128bit,
  54. ait_stab,
  55. ait_force_line,
  56. ait_function_name,
  57. {$ifdef alpha}
  58. { the follow is for the DEC Alpha }
  59. ait_frame,
  60. ait_ent,
  61. {$endif alpha}
  62. {$ifdef ia64}
  63. ait_bundle,
  64. ait_stop,
  65. {$endif ia64}
  66. {$ifdef m68k}
  67. ait_labeled_instruction,
  68. {$endif m68k}
  69. {$ifdef support_llvm_typedef}
  70. { (llvm) type definition }
  71. ait_typedef,
  72. {$endif support_llvm_typedef}
  73. {$ifdef support_llvm}
  74. ait_llvmins,
  75. {$endif support_llvm}
  76. { used to split into tiny assembler files }
  77. ait_cutobject,
  78. ait_regalloc,
  79. ait_tempalloc,
  80. { used to mark assembler blocks and inlined functions }
  81. ait_marker
  82. );
  83. taiconst_type = (
  84. aitconst_128bit,
  85. aitconst_64bit,
  86. aitconst_32bit,
  87. aitconst_16bit,
  88. aitconst_8bit,
  89. aitconst_sleb128bit,
  90. aitconst_uleb128bit,
  91. { win32 only }
  92. aitconst_rva_symbol,
  93. aitconst_secrel32_symbol,
  94. { darwin only }
  95. aitconst_indirect_symbol,
  96. { From gcc/config/darwin.c (darwin_asm_output_dwarf_delta):
  97. ***
  98. Output a difference of two labels that will be an assembly time
  99. constant if the two labels are local. (.long lab1-lab2 will be
  100. very different if lab1 is at the boundary between two sections; it
  101. will be relocated according to the second section, not the first,
  102. so one ends up with a difference between labels in different
  103. sections, which is bad in the dwarf2 eh context for instance.)
  104. ***
  105. We cannot use this everywhere, because older versions of the
  106. darwin assembler don't support the construct used for these
  107. relsyms (nor do they support dwarf, for that matter)
  108. }
  109. aitconst_darwin_dwarf_delta64,
  110. aitconst_darwin_dwarf_delta32
  111. );
  112. const
  113. {$ifdef cpu64bitaddr}
  114. aitconst_ptr = aitconst_64bit;
  115. {$else cpu64bitaddr}
  116. aitconst_ptr = aitconst_32bit;
  117. {$endif cpu64bitaddr}
  118. {$ifdef cpu64bitalu}
  119. aitconst_aint = aitconst_64bit;
  120. {$else cpu64bitaddr}
  121. aitconst_aint = aitconst_32bit;
  122. {$endif cpu64bitaddr}
  123. taitypestr : array[taitype] of string[24] = (
  124. '<none>',
  125. 'align',
  126. 'section',
  127. 'comment',
  128. 'string',
  129. 'instruction',
  130. 'datablock',
  131. 'symbol',
  132. 'symbol_end',
  133. 'symbol_directive',
  134. 'label',
  135. 'const',
  136. 'real_32bit',
  137. 'real_64bit',
  138. 'real_80bit',
  139. 'comp_64bit',
  140. 'real_128bit',
  141. 'stab',
  142. 'force_line',
  143. 'function_name',
  144. {$ifdef alpha}
  145. { the follow is for the DEC Alpha }
  146. 'frame',
  147. 'ent',
  148. {$endif alpha}
  149. {$ifdef ia64}
  150. 'bundle',
  151. 'stop',
  152. {$endif ia64}
  153. {$ifdef m68k}
  154. 'labeled_instr',
  155. {$endif m68k}
  156. {$ifdef support_llvm_typedef}
  157. { (llvm) type definition }
  158. 'ait_typedef',
  159. {$endif support_llvm_typedef}
  160. {$ifdef support_llvm}
  161. 'ait_llvmins',
  162. {$endif support_llvm}
  163. 'cut',
  164. 'regalloc',
  165. 'tempalloc',
  166. 'marker'
  167. );
  168. type
  169. { Types of operand }
  170. toptype=(top_none,top_reg,top_ref,top_const,top_bool,top_local
  171. {$ifdef arm}
  172. { ARM only }
  173. ,top_regset
  174. ,top_shifterop
  175. {$endif arm}
  176. {$ifdef m68k}
  177. { m68k only }
  178. ,top_regset
  179. {$endif m68k}
  180. {$ifdef support_llvm}
  181. ,top_string
  182. {$endif support_llvm}
  183. { i386 only});
  184. { kinds of operations that an instruction can perform on an operand }
  185. topertype = (operand_read,operand_write,operand_readwrite);
  186. tlocaloper = record
  187. localsym : pointer;
  188. localsymderef : tderef;
  189. localsymofs : longint;
  190. localindexreg : tregister;
  191. localscale : byte;
  192. localgetoffset,
  193. localforceref : boolean
  194. end;
  195. plocaloper = ^tlocaloper;
  196. { please keep the size of this record <=12 bytes and keep it properly aligned }
  197. toper = record
  198. ot : longint;
  199. case typ : toptype of
  200. top_none : ();
  201. top_reg : (reg:tregister);
  202. top_ref : (ref:preference);
  203. top_const : (val:aint);
  204. top_bool : (b:boolean);
  205. { local varsym that will be inserted in pass_generate_code }
  206. top_local : (localoper:plocaloper);
  207. {$ifdef arm}
  208. top_regset : (regset:^tcpuregisterset);
  209. top_shifterop : (shifterop : pshifterop);
  210. {$endif arm}
  211. {$ifdef m68k}
  212. top_regset : (regset:^tcpuregisterset);
  213. {$endif m68k}
  214. {$ifdef support_llvm}
  215. top_string : (str: pchar);
  216. {$endif support_llvm}
  217. end;
  218. poper=^toper;
  219. const
  220. { ait_* types which don't result in executable code or which don't influence
  221. the way the program runs/behaves, but which may be encountered by the
  222. optimizer (= if it's sometimes added to the exprasm list). Update if you add
  223. a new ait type! }
  224. SkipInstr = [ait_comment, ait_symbol,ait_section
  225. ,ait_stab, ait_function_name, ait_force_line
  226. ,ait_regalloc, ait_tempalloc, ait_symbol_end, ait_directive];
  227. { ait_* types which do not have line information (and hence which are of type
  228. tai, otherwise, they are of type tailineinfo }
  229. SkipLineInfo =[ait_label,
  230. ait_regalloc,ait_tempalloc,
  231. ait_stab,ait_function_name,
  232. ait_cutobject,ait_marker,ait_align,ait_section,ait_comment,
  233. ait_const,
  234. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_real_128bit,
  235. ait_symbol
  236. ];
  237. type
  238. { cut type, required for alphanumeric ordering of the assembler filenames }
  239. TCutPlace=(cut_normal,cut_begin,cut_end);
  240. TAsmMarker = (
  241. mark_NoPropInfoStart,mark_NoPropInfoEnd,
  242. mark_AsmBlockStart,mark_AsmBlockEnd,
  243. mark_InlineStart,mark_InlineEnd,mark_BlockStart,
  244. mark_Position
  245. );
  246. TRegAllocType = (ra_alloc,ra_dealloc,ra_sync,ra_resize);
  247. TStabType = (stab_stabs,stab_stabn,stab_stabd);
  248. TAsmDirective=(
  249. asd_non_lazy_symbol_pointer,asd_indirect_symbol,asd_lazy_symbol_pointer,
  250. asd_extern,asd_nasm_import, asd_toc_entry, asd_mod_init_func, asd_mod_term_func,
  251. asd_reference,asd_no_dead_strip
  252. );
  253. const
  254. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized');
  255. tempallocstr : array[boolean] of string[10]=('released','allocated');
  256. stabtypestr : array[TStabType] of string[5]=('stabs','stabn','stabd');
  257. directivestr : array[TAsmDirective] of string[23]=(
  258. 'non_lazy_symbol_pointer','indirect_symbol','lazy_symbol_pointer',
  259. 'extern','nasm_import', 'tc', 'mod_init_func', 'mod_term_func', 'reference',
  260. 'no_dead_strip'
  261. );
  262. type
  263. { abstract assembler item }
  264. tai = class(TLinkedListItem)
  265. {$ifndef NOOPT}
  266. { pointer to record with optimizer info about this tai object }
  267. optinfo : pointer;
  268. {$endif NOOPT}
  269. typ : taitype;
  270. constructor Create;
  271. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  272. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  273. procedure buildderefimpl;virtual;
  274. procedure derefimpl;virtual;
  275. end;
  276. { abstract assembler item with line information }
  277. tailineinfo = class(tai)
  278. fileinfo : tfileposinfo;
  279. constructor Create;
  280. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  281. procedure ppuwrite(ppufile:tcompilerppufile);override;
  282. end;
  283. tai_simple = class(tai)
  284. constructor create(_typ : taitype);
  285. end;
  286. taiclass = class of tai;
  287. taiclassarray = array[taitype] of taiclass;
  288. { Generates an assembler string }
  289. tai_string = class(tailineinfo)
  290. str : pchar;
  291. { extra len so the string can contain an \0 }
  292. len : longint;
  293. constructor Create(const _str : string);
  294. constructor Create_pchar(_str : pchar;length : longint);
  295. destructor Destroy;override;
  296. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  297. procedure ppuwrite(ppufile:tcompilerppufile);override;
  298. function getcopy:tlinkedlistitem;override;
  299. end;
  300. { Generates a common label }
  301. tai_symbol = class(tai)
  302. is_global : boolean;
  303. sym : tasmsymbol;
  304. size : longint;
  305. constructor Create(_sym:tasmsymbol;siz:longint);
  306. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  307. constructor Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  308. constructor Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  309. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  310. procedure ppuwrite(ppufile:tcompilerppufile);override;
  311. procedure derefimpl;override;
  312. end;
  313. tai_symbol_end = class(tailineinfo)
  314. sym : tasmsymbol;
  315. constructor Create(_sym:tasmsymbol);
  316. constructor Createname(const _name : string);
  317. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  318. procedure ppuwrite(ppufile:tcompilerppufile);override;
  319. procedure derefimpl;override;
  320. end;
  321. tai_directive = class(tailineinfo)
  322. name : pshortstring;
  323. directive : TAsmDirective;
  324. constructor Create(_directive:TAsmDirective;const _name:string);
  325. destructor Destroy;override;
  326. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  327. procedure ppuwrite(ppufile:tcompilerppufile);override;
  328. end;
  329. { Generates an assembler label }
  330. tai_label = class(tai)
  331. is_global : boolean;
  332. labsym : tasmlabel;
  333. constructor Create(_labsym : tasmlabel);
  334. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  335. procedure ppuwrite(ppufile:tcompilerppufile);override;
  336. procedure derefimpl;override;
  337. end;
  338. { Generates an assembler comment }
  339. tai_comment = class(tai)
  340. str : pchar;
  341. constructor Create(_str : pchar);
  342. destructor Destroy; override;
  343. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  344. procedure ppuwrite(ppufile:tcompilerppufile);override;
  345. function getcopy:tlinkedlistitem;override;
  346. end;
  347. { Generates a section / segment directive }
  348. tai_section = class(tai)
  349. sectype : TAsmSectiontype;
  350. secorder : TasmSectionorder;
  351. secalign : byte;
  352. name : pshortstring;
  353. sec : TObjSection; { used in binary writer }
  354. constructor Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  355. destructor Destroy;override;
  356. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  357. procedure ppuwrite(ppufile:tcompilerppufile);override;
  358. end;
  359. { Generates an uninitializised data block }
  360. tai_datablock = class(tailineinfo)
  361. is_global : boolean;
  362. sym : tasmsymbol;
  363. size : aint;
  364. constructor Create(const _name : string;_size : aint);
  365. constructor Create_global(const _name : string;_size : aint);
  366. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  367. procedure ppuwrite(ppufile:tcompilerppufile);override;
  368. procedure derefimpl;override;
  369. end;
  370. { Generates an integer const }
  371. tai_const = class(tai)
  372. sym,
  373. endsym : tasmsymbol;
  374. { if symbols and offset are provided the symofs is used,
  375. the value is calculated during assembling }
  376. symofs,
  377. value : int64;
  378. consttype : taiconst_type;
  379. { we use for the 128bit int64/qword for now because I can't imagine a
  380. case where we need 128 bit now (FK) }
  381. constructor Create(_typ:taiconst_type;_value : int64);
  382. constructor Create_128bit(_value : int64);
  383. constructor Create_64bit(_value : int64);
  384. constructor Create_32bit(_value : longint);
  385. constructor Create_16bit(_value : word);
  386. constructor Create_8bit(_value : byte);
  387. constructor Create_sleb128bit(_value : int64);
  388. constructor Create_uleb128bit(_value : qword);
  389. constructor Create_aint(_value : aint);
  390. constructor Create_pint(_value : pint);
  391. constructor Create_sym(_sym:tasmsymbol);
  392. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  393. constructor Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  394. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  395. constructor Create_rva_sym(_sym:tasmsymbol);
  396. constructor Create_indirect_sym(_sym:tasmsymbol);
  397. constructor Createname(const name:string;ofs:aint);
  398. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  399. procedure ppuwrite(ppufile:tcompilerppufile);override;
  400. procedure derefimpl;override;
  401. function getcopy:tlinkedlistitem;override;
  402. function size:longint;
  403. end;
  404. { Generates a single float (32 bit real) }
  405. tai_real_32bit = class(tai)
  406. value : ts32real;
  407. constructor Create(_value : ts32real);
  408. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  409. procedure ppuwrite(ppufile:tcompilerppufile);override;
  410. end;
  411. tformatoptions = (fo_none,fo_hiloswapped);
  412. { Generates a double float (64 bit real) }
  413. tai_real_64bit = class(tai)
  414. value : ts64real;
  415. {$ifdef ARM}
  416. formatoptions : tformatoptions;
  417. constructor Create_hiloswapped(_value : ts64real);
  418. {$endif ARM}
  419. constructor Create(_value : ts64real);
  420. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  421. procedure ppuwrite(ppufile:tcompilerppufile);override;
  422. end;
  423. { Generates an extended float (80 bit real) }
  424. tai_real_80bit = class(tai)
  425. value : ts80real;
  426. constructor Create(_value : ts80real);
  427. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  428. procedure ppuwrite(ppufile:tcompilerppufile);override;
  429. end;
  430. { Generates an float128 (128 bit real) }
  431. tai_real_128bit = class(tai)
  432. value : ts128real;
  433. constructor Create(_value : ts128real);
  434. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  435. procedure ppuwrite(ppufile:tcompilerppufile);override;
  436. end;
  437. { Generates a comp int (integer over 64 bits)
  438. This is Intel 80x86 specific, and is not
  439. really supported on other processors.
  440. }
  441. tai_comp_64bit = class(tai)
  442. value : ts64comp;
  443. constructor Create(_value : ts64comp);
  444. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  445. procedure ppuwrite(ppufile:tcompilerppufile);override;
  446. end;
  447. tai_stab = class(tai)
  448. str : pchar;
  449. stabtype : TStabType;
  450. constructor Create(_stabtype:TStabType;_str : pchar);
  451. constructor Create_str(_stabtype:TStabType;const s:string);
  452. destructor Destroy;override;
  453. end;
  454. tai_force_line = class(tailineinfo)
  455. constructor Create;
  456. end;
  457. tai_function_name = class(tai)
  458. funcname : pshortstring;
  459. constructor create(const s:string);
  460. destructor destroy;override;
  461. end;
  462. { Insert a cut to split assembler into several smaller files }
  463. tai_cutobject = class(tai)
  464. place : tcutplace;
  465. constructor Create;
  466. constructor Create_begin;
  467. constructor Create_end;
  468. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  469. procedure ppuwrite(ppufile:tcompilerppufile);override;
  470. end;
  471. { Insert a marker for assembler and inline blocks }
  472. tai_marker = class(tai)
  473. Kind: TAsmMarker;
  474. Constructor Create(_Kind: TAsmMarker);
  475. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  476. procedure ppuwrite(ppufile:tcompilerppufile);override;
  477. end;
  478. tai_tempalloc = class(tai)
  479. allocation : boolean;
  480. {$ifdef EXTDEBUG}
  481. problem : pshortstring;
  482. {$endif EXTDEBUG}
  483. temppos,
  484. tempsize : longint;
  485. constructor alloc(pos,size:longint);
  486. constructor dealloc(pos,size:longint);
  487. {$ifdef EXTDEBUG}
  488. constructor allocinfo(pos,size:longint;const st:string);
  489. {$endif EXTDEBUG}
  490. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  491. destructor destroy;override;
  492. procedure ppuwrite(ppufile:tcompilerppufile);override;
  493. end;
  494. tai_regalloc = class(tai)
  495. reg : tregister;
  496. ratype : TRegAllocType;
  497. { reg(de)alloc belongs to this instruction, this
  498. is only used for automatic inserted (de)alloc for
  499. imaginary register and required for spilling code }
  500. instr : tai;
  501. constructor alloc(r : tregister;ainstr:tai);
  502. constructor dealloc(r : tregister;ainstr:tai);
  503. constructor sync(r : tregister);
  504. constructor resize(r : tregister);
  505. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  506. procedure ppuwrite(ppufile:tcompilerppufile);override;
  507. end;
  508. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  509. { Class template for assembler instructions
  510. }
  511. tai_cpu_abstract = class(tailineinfo)
  512. protected
  513. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  514. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  515. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  516. procedure ppuderefoper(var o:toper);virtual;abstract;
  517. public
  518. { Condition flags for instruction }
  519. condition : TAsmCond;
  520. { Number of operands to instruction }
  521. ops : byte;
  522. { Number of allocate oper structures }
  523. opercnt : byte;
  524. { Operands of instruction }
  525. oper : array[0..max_operands-1] of poper;
  526. { Actual opcode of instruction }
  527. opcode : tasmop;
  528. {$ifdef x86}
  529. segprefix : tregister;
  530. {$endif x86}
  531. { true if instruction is a jmp }
  532. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  533. Constructor Create(op : tasmop);virtual;
  534. Destructor Destroy;override;
  535. function getcopy:TLinkedListItem;override;
  536. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  537. procedure ppuwrite(ppufile:tcompilerppufile);override;
  538. procedure buildderefimpl;override;
  539. procedure derefimpl;override;
  540. procedure SetCondition(const c:TAsmCond);
  541. procedure allocate_oper(opers:longint);
  542. procedure loadconst(opidx:longint;l:aint);
  543. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  544. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  545. procedure loadref(opidx:longint;const r:treference);
  546. procedure loadreg(opidx:longint;r:tregister);
  547. procedure loadoper(opidx:longint;o:toper);
  548. procedure clearop(opidx:longint);
  549. procedure freeop(opidx:longint);
  550. { register allocator }
  551. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  552. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  553. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  554. function Pass1(objdata:TObjData):longint;virtual;
  555. procedure Pass2(objdata:TObjData);virtual;
  556. procedure resetpass1; virtual;
  557. procedure resetpass2; virtual;
  558. end;
  559. tai_cpu_class = class of tai_cpu_abstract;
  560. { Buffer type used for alignment }
  561. tfillbuffer = array[0..63] of char;
  562. { alignment for operator }
  563. tai_align_abstract = class(tai)
  564. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  565. fillsize : byte; { real size to fill }
  566. fillop : byte; { value to fill with - optional }
  567. use_op : boolean;
  568. constructor Create(b:byte);virtual;
  569. constructor Create_op(b: byte; _op: byte);virtual;
  570. constructor Create_zeros(b:byte);
  571. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  572. procedure ppuwrite(ppufile:tcompilerppufile);override;
  573. function calculatefillbuf(var buf : tfillbuffer):pchar;virtual;
  574. end;
  575. tai_align_class = class of tai_align_abstract;
  576. {$ifdef support_llvm_typedef}
  577. tai_typedef = class(tai);
  578. namestr: pstring;
  579. defstr: ansistring;
  580. constructor create(const typename: string; const typedef: ansistring);
  581. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  582. destructor destroy;override;
  583. procedure ppuwrite(ppufile:tcompilerppufile);override;
  584. end;
  585. {$endif support_llvm_typedef}
  586. var
  587. { array with all class types for tais }
  588. aiclass : taiclassarray;
  589. { target specific tais }
  590. cai_align : tai_align_class;
  591. cai_cpu : tai_cpu_class;
  592. { hook to notify uses of registers }
  593. add_reg_instruction_hook : tadd_reg_instruction_proc;
  594. procedure maybe_new_object_file(list:TAsmList);
  595. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  596. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  597. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  598. procedure section_symbol_end(list:TAsmList;const Aname:string);
  599. function ppuloadai(ppufile:tcompilerppufile):tai;
  600. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  601. implementation
  602. uses
  603. SysUtils,
  604. verbose,
  605. globals,
  606. fmodule
  607. {$ifdef support_llvm}
  608. ,strings
  609. {$endif support_llvm}
  610. ;
  611. const
  612. pputaimarker = 254;
  613. {****************************************************************************
  614. Helpers
  615. ****************************************************************************}
  616. procedure maybe_new_object_file(list:TAsmList);
  617. begin
  618. if create_smartlink_library then
  619. list.concat(tai_cutobject.create);
  620. end;
  621. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  622. begin
  623. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  624. list.concat(cai_align.create(Aalign));
  625. end;
  626. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  627. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  628. begin
  629. maybe_new_object_file(list);
  630. list.concat(tai_section.create(Asectype,Aname,Aalign));
  631. list.concat(cai_align.create(Aalign));
  632. if Aglobal or
  633. create_smartlink then
  634. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  635. else
  636. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  637. end;
  638. procedure section_symbol_end(list:TAsmList;const Aname:string);
  639. begin
  640. list.concat(tai_symbol_end.createname(Aname));
  641. end;
  642. function ppuloadai(ppufile:tcompilerppufile):tai;
  643. var
  644. b : byte;
  645. t : taitype;
  646. begin
  647. { marker }
  648. b:=ppufile.getbyte;
  649. if b<>pputaimarker then
  650. internalerror(200208181);
  651. { load nodetype }
  652. t:=taitype(ppufile.getbyte);
  653. if t<>ait_none then
  654. begin
  655. if t>high(taitype) then
  656. internalerror(200208182);
  657. if not assigned(aiclass[t]) then
  658. internalerror(200208183);
  659. {writeln('taiload: ',taitypestr[t]);}
  660. { generate tai of the correct class }
  661. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  662. end
  663. else
  664. ppuloadai:=nil;
  665. end;
  666. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  667. begin
  668. { marker, read by ppuloadnode }
  669. ppufile.putbyte(pputaimarker);
  670. if assigned(n) then
  671. begin
  672. { type, read by ppuloadnode }
  673. ppufile.putbyte(byte(n.typ));
  674. {writeln('taiwrite: ',taitypestr[n.typ]);}
  675. n.ppuwrite(ppufile);
  676. end
  677. else
  678. ppufile.putbyte(byte(ait_none));
  679. end;
  680. {****************************************************************************
  681. TAI
  682. ****************************************************************************}
  683. constructor tai.Create;
  684. begin
  685. {$ifndef NOOPT}
  686. optinfo:=nil;
  687. {$endif NOOPT}
  688. end;
  689. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  690. begin
  691. typ:=t;
  692. {$ifndef NOOPT}
  693. optinfo:=nil;
  694. {$endif}
  695. end;
  696. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  697. begin
  698. end;
  699. procedure tai.buildderefimpl;
  700. begin
  701. end;
  702. procedure tai.derefimpl;
  703. begin
  704. end;
  705. {****************************************************************************
  706. TAILINEINFO
  707. ****************************************************************************}
  708. constructor tailineinfo.create;
  709. begin
  710. inherited create;
  711. fileinfo:=current_filepos;
  712. end;
  713. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  714. begin
  715. inherited ppuload(t,ppufile);
  716. ppufile.getposinfo(fileinfo);
  717. end;
  718. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  719. begin
  720. inherited ppuwrite(ppufile);
  721. ppufile.putposinfo(fileinfo);
  722. end;
  723. {****************************************************************************
  724. TAI_SIMPLE
  725. ****************************************************************************}
  726. constructor tai_simple.create(_typ : taitype);
  727. begin
  728. inherited create;
  729. typ:=_typ;
  730. end;
  731. {****************************************************************************
  732. TAI_SECTION
  733. ****************************************************************************}
  734. constructor tai_section.Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  735. begin
  736. inherited Create;
  737. typ:=ait_section;
  738. sectype:=asectype;
  739. secalign:=Aalign;
  740. secorder:=Asecorder;
  741. name:=stringdup(Aname);
  742. sec:=nil;
  743. end;
  744. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  745. begin
  746. inherited ppuload(t,ppufile);
  747. sectype:=TAsmSectiontype(ppufile.getbyte);
  748. secalign:=ppufile.getbyte;
  749. name:=stringdup(ppufile.getstring);
  750. sec:=nil;
  751. end;
  752. destructor tai_section.Destroy;
  753. begin
  754. stringdispose(name);
  755. end;
  756. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  757. begin
  758. inherited ppuwrite(ppufile);
  759. ppufile.putbyte(byte(sectype));
  760. ppufile.putbyte(secalign);
  761. ppufile.putstring(name^);
  762. end;
  763. {****************************************************************************
  764. TAI_DATABLOCK
  765. ****************************************************************************}
  766. constructor tai_datablock.Create(const _name : string;_size : aint);
  767. begin
  768. inherited Create;
  769. typ:=ait_datablock;
  770. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  771. { keep things aligned }
  772. if _size<=0 then
  773. _size:=sizeof(aint);
  774. size:=_size;
  775. is_global:=false;
  776. end;
  777. constructor tai_datablock.Create_global(const _name : string;_size : aint);
  778. begin
  779. inherited Create;
  780. typ:=ait_datablock;
  781. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  782. { keep things aligned }
  783. if _size<=0 then
  784. _size:=sizeof(aint);
  785. size:=_size;
  786. is_global:=true;
  787. end;
  788. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  789. begin
  790. inherited Create;
  791. sym:=ppufile.getasmsymbol;
  792. size:=ppufile.getaint;
  793. is_global:=boolean(ppufile.getbyte);
  794. end;
  795. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  796. begin
  797. inherited ppuwrite(ppufile);
  798. ppufile.putasmsymbol(sym);
  799. ppufile.putaint(size);
  800. ppufile.putbyte(byte(is_global));
  801. end;
  802. procedure tai_datablock.derefimpl;
  803. begin
  804. end;
  805. {****************************************************************************
  806. TAI_SYMBOL
  807. ****************************************************************************}
  808. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  809. begin
  810. inherited Create;
  811. typ:=ait_symbol;
  812. sym:=_sym;
  813. size:=siz;
  814. sym.bind:=AB_LOCAL;
  815. is_global:=false;
  816. end;
  817. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  818. begin
  819. inherited Create;
  820. typ:=ait_symbol;
  821. sym:=_sym;
  822. size:=siz;
  823. sym.bind:=AB_GLOBAL;
  824. is_global:=true;
  825. end;
  826. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  827. begin
  828. inherited Create;
  829. typ:=ait_symbol;
  830. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  831. size:=siz;
  832. is_global:=false;
  833. end;
  834. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  835. begin
  836. inherited Create;
  837. typ:=ait_symbol;
  838. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  839. size:=siz;
  840. is_global:=true;
  841. end;
  842. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  843. begin
  844. inherited ppuload(t,ppufile);
  845. sym:=ppufile.getasmsymbol;
  846. size:=ppufile.getlongint;
  847. is_global:=boolean(ppufile.getbyte);
  848. end;
  849. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  850. begin
  851. inherited ppuwrite(ppufile);
  852. ppufile.putasmsymbol(sym);
  853. ppufile.putlongint(size);
  854. ppufile.putbyte(byte(is_global));
  855. end;
  856. procedure tai_symbol.derefimpl;
  857. begin
  858. end;
  859. {****************************************************************************
  860. TAI_SYMBOL_END
  861. ****************************************************************************}
  862. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  863. begin
  864. inherited Create;
  865. typ:=ait_symbol_end;
  866. sym:=_sym;
  867. end;
  868. constructor tai_symbol_end.Createname(const _name : string);
  869. begin
  870. inherited Create;
  871. typ:=ait_symbol_end;
  872. sym:=current_asmdata.RefAsmSymbol(_name);
  873. end;
  874. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  875. begin
  876. inherited ppuload(t,ppufile);
  877. sym:=ppufile.getasmsymbol;
  878. end;
  879. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  880. begin
  881. inherited ppuwrite(ppufile);
  882. ppufile.putasmsymbol(sym);
  883. end;
  884. procedure tai_symbol_end.derefimpl;
  885. begin
  886. end;
  887. {****************************************************************************
  888. TAI_SYMBOL_END
  889. ****************************************************************************}
  890. constructor tai_directive.Create(_directive:TAsmDirective;const _name:string);
  891. begin
  892. inherited Create;
  893. typ:=ait_directive;
  894. name:=stringdup(_name);
  895. directive:=_directive;
  896. end;
  897. destructor tai_directive.Destroy;
  898. begin
  899. stringdispose(name);
  900. end;
  901. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  902. begin
  903. inherited ppuload(t,ppufile);
  904. name:=stringdup(ppufile.getstring);
  905. directive:=TAsmDirective(ppufile.getbyte);
  906. end;
  907. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  908. begin
  909. inherited ppuwrite(ppufile);
  910. ppufile.putstring(name^);
  911. ppufile.putbyte(byte(directive));
  912. end;
  913. {****************************************************************************
  914. TAI_CONST
  915. ****************************************************************************}
  916. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  917. begin
  918. inherited Create;
  919. typ:=ait_const;
  920. consttype:=_typ;
  921. value:=_value;
  922. sym:=nil;
  923. endsym:=nil;
  924. end;
  925. constructor tai_const.Create_128bit(_value : int64);
  926. begin
  927. inherited Create;
  928. typ:=ait_const;
  929. consttype:=aitconst_128bit;
  930. value:=_value;
  931. sym:=nil;
  932. endsym:=nil;
  933. end;
  934. constructor tai_const.Create_64bit(_value : int64);
  935. begin
  936. inherited Create;
  937. typ:=ait_const;
  938. consttype:=aitconst_64bit;
  939. value:=_value;
  940. sym:=nil;
  941. endsym:=nil;
  942. end;
  943. constructor tai_const.Create_32bit(_value : longint);
  944. begin
  945. inherited Create;
  946. typ:=ait_const;
  947. consttype:=aitconst_32bit;
  948. value:=_value;
  949. sym:=nil;
  950. endsym:=nil;
  951. end;
  952. constructor tai_const.Create_16bit(_value : word);
  953. begin
  954. inherited Create;
  955. typ:=ait_const;
  956. consttype:=aitconst_16bit;
  957. value:=_value;
  958. sym:=nil;
  959. endsym:=nil;
  960. end;
  961. constructor tai_const.Create_8bit(_value : byte);
  962. begin
  963. inherited Create;
  964. typ:=ait_const;
  965. consttype:=aitconst_8bit;
  966. value:=_value;
  967. sym:=nil;
  968. endsym:=nil;
  969. end;
  970. constructor tai_const.Create_sleb128bit(_value : int64);
  971. begin
  972. inherited Create;
  973. typ:=ait_const;
  974. consttype:=aitconst_sleb128bit;
  975. value:=_value;
  976. sym:=nil;
  977. endsym:=nil;
  978. end;
  979. constructor tai_const.Create_uleb128bit(_value : qword);
  980. begin
  981. inherited Create;
  982. typ:=ait_const;
  983. consttype:=aitconst_uleb128bit;
  984. value:=int64(_value);
  985. sym:=nil;
  986. endsym:=nil;
  987. end;
  988. constructor tai_const.Create_aint(_value : aint);
  989. begin
  990. inherited Create;
  991. typ:=ait_const;
  992. consttype:=aitconst_aint;
  993. value:=_value;
  994. sym:=nil;
  995. endsym:=nil;
  996. end;
  997. constructor tai_const.Create_pint(_value : pint);
  998. begin
  999. inherited Create;
  1000. typ:=ait_const;
  1001. consttype:=aitconst_ptr;
  1002. value:=_value;
  1003. sym:=nil;
  1004. endsym:=nil;
  1005. end;
  1006. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1007. begin
  1008. inherited Create;
  1009. typ:=ait_const;
  1010. consttype:=_typ;
  1011. sym:=_sym;
  1012. endsym:=nil;
  1013. value:=0;
  1014. { update sym info }
  1015. if assigned(sym) then
  1016. sym.increfs;
  1017. end;
  1018. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1019. begin
  1020. self.create_sym_offset(_sym,0);
  1021. end;
  1022. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  1023. begin
  1024. inherited Create;
  1025. typ:=ait_const;
  1026. consttype:=aitconst_ptr;
  1027. { sym is allowed to be nil, this is used to write nil pointers }
  1028. sym:=_sym;
  1029. endsym:=nil;
  1030. { store the original offset in symofs so that we can recalculate the
  1031. value field in the assembler }
  1032. symofs:=ofs;
  1033. value:=ofs;
  1034. { update sym info }
  1035. if assigned(sym) then
  1036. sym.increfs;
  1037. end;
  1038. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1039. begin
  1040. self.create_sym_offset(_sym,0);
  1041. consttype:=_typ;
  1042. endsym:=_endsym;
  1043. endsym.increfs;
  1044. end;
  1045. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1046. begin
  1047. self.create_sym_offset(_sym,0);
  1048. consttype:=aitconst_rva_symbol;
  1049. end;
  1050. constructor tai_const.Create_indirect_sym(_sym:tasmsymbol);
  1051. begin
  1052. self.create_sym_offset(_sym,0);
  1053. consttype:=aitconst_indirect_symbol;
  1054. end;
  1055. constructor tai_const.Createname(const name:string;ofs:aint);
  1056. begin
  1057. self.create_sym_offset(current_asmdata.RefAsmSymbol(name),ofs);
  1058. end;
  1059. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1060. begin
  1061. inherited ppuload(t,ppufile);
  1062. consttype:=taiconst_type(ppufile.getbyte);
  1063. sym:=ppufile.getasmsymbol;
  1064. endsym:=ppufile.getasmsymbol;
  1065. value:=ppufile.getint64;
  1066. end;
  1067. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1068. begin
  1069. inherited ppuwrite(ppufile);
  1070. ppufile.putbyte(byte(consttype));
  1071. ppufile.putasmsymbol(sym);
  1072. ppufile.putasmsymbol(endsym);
  1073. ppufile.putint64(value);
  1074. end;
  1075. procedure tai_const.derefimpl;
  1076. begin
  1077. end;
  1078. function tai_const.getcopy:tlinkedlistitem;
  1079. begin
  1080. getcopy:=inherited getcopy;
  1081. { we need to increase the reference number }
  1082. sym.increfs;
  1083. if assigned(endsym) then
  1084. endsym.increfs;
  1085. end;
  1086. function tai_const.size:longint;
  1087. begin
  1088. case consttype of
  1089. aitconst_8bit :
  1090. result:=1;
  1091. aitconst_16bit :
  1092. result:=2;
  1093. aitconst_32bit :
  1094. result:=4;
  1095. aitconst_64bit :
  1096. result:=8;
  1097. aitconst_indirect_symbol,
  1098. aitconst_secrel32_symbol,
  1099. aitconst_rva_symbol :
  1100. if target_info.system=system_x86_64_win64 then
  1101. result:=sizeof(longint)
  1102. else
  1103. result:=sizeof(pint);
  1104. aitconst_uleb128bit :
  1105. result:=LengthUleb128(qword(value));
  1106. aitconst_sleb128bit :
  1107. result:=LengthSleb128(value);
  1108. else
  1109. internalerror(200603253);
  1110. end;
  1111. end;
  1112. {****************************************************************************
  1113. TAI_real_32bit
  1114. ****************************************************************************}
  1115. constructor tai_real_32bit.Create(_value : ts32real);
  1116. begin
  1117. inherited Create;
  1118. typ:=ait_real_32bit;
  1119. value:=_value;
  1120. end;
  1121. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1122. begin
  1123. inherited ppuload(t,ppufile);
  1124. value:=ppufile.getreal;
  1125. end;
  1126. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  1127. begin
  1128. inherited ppuwrite(ppufile);
  1129. ppufile.putreal(value);
  1130. end;
  1131. {****************************************************************************
  1132. TAI_real_64bit
  1133. ****************************************************************************}
  1134. constructor tai_real_64bit.Create(_value : ts64real);
  1135. begin
  1136. inherited Create;
  1137. typ:=ait_real_64bit;
  1138. value:=_value;
  1139. end;
  1140. {$ifdef ARM}
  1141. constructor tai_real_64bit.Create_hiloswapped(_value : ts64real);
  1142. begin
  1143. inherited Create;
  1144. typ:=ait_real_64bit;
  1145. value:=_value;
  1146. formatoptions:=fo_hiloswapped;
  1147. end;
  1148. {$endif ARM}
  1149. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1150. begin
  1151. inherited ppuload(t,ppufile);
  1152. value:=ppufile.getreal;
  1153. {$ifdef ARM}
  1154. formatoptions:=tformatoptions(ppufile.getbyte);
  1155. {$endif ARM}
  1156. end;
  1157. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  1158. begin
  1159. inherited ppuwrite(ppufile);
  1160. ppufile.putreal(value);
  1161. {$ifdef ARM}
  1162. ppufile.putbyte(byte(formatoptions));
  1163. {$endif ARM}
  1164. end;
  1165. {****************************************************************************
  1166. TAI_real_80bit
  1167. ****************************************************************************}
  1168. constructor tai_real_80bit.Create(_value : ts80real);
  1169. begin
  1170. inherited Create;
  1171. typ:=ait_real_80bit;
  1172. value:=_value;
  1173. end;
  1174. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1175. begin
  1176. inherited ppuload(t,ppufile);
  1177. value:=ppufile.getreal;
  1178. end;
  1179. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  1180. begin
  1181. inherited ppuwrite(ppufile);
  1182. ppufile.putreal(value);
  1183. end;
  1184. {****************************************************************************
  1185. TAI_real_80bit
  1186. ****************************************************************************}
  1187. constructor tai_real_128bit.Create(_value : ts128real);
  1188. begin
  1189. inherited Create;
  1190. typ:=ait_real_128bit;
  1191. value:=_value;
  1192. end;
  1193. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1194. begin
  1195. inherited ppuload(t,ppufile);
  1196. value:=ppufile.getreal;
  1197. end;
  1198. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  1199. begin
  1200. inherited ppuwrite(ppufile);
  1201. ppufile.putreal(value);
  1202. end;
  1203. {****************************************************************************
  1204. Tai_comp_64bit
  1205. ****************************************************************************}
  1206. constructor tai_comp_64bit.Create(_value : ts64comp);
  1207. begin
  1208. inherited Create;
  1209. typ:=ait_comp_64bit;
  1210. value:=_value;
  1211. end;
  1212. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1213. begin
  1214. inherited ppuload(t,ppufile);
  1215. ppufile.putdata(value,sizeof(value));
  1216. end;
  1217. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  1218. begin
  1219. inherited ppuwrite(ppufile);
  1220. ppufile.getdata(value,sizeof(value));
  1221. end;
  1222. {****************************************************************************
  1223. TAI_STRING
  1224. ****************************************************************************}
  1225. constructor tai_string.Create(const _str : string);
  1226. begin
  1227. inherited Create;
  1228. typ:=ait_string;
  1229. len:=length(_str);
  1230. getmem(str,len+1);
  1231. strpcopy(str,_str);
  1232. end;
  1233. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1234. begin
  1235. inherited Create;
  1236. typ:=ait_string;
  1237. str:=_str;
  1238. len:=length;
  1239. end;
  1240. destructor tai_string.destroy;
  1241. begin
  1242. if str<>nil then
  1243. freemem(str);
  1244. inherited Destroy;
  1245. end;
  1246. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1247. begin
  1248. inherited ppuload(t,ppufile);
  1249. len:=ppufile.getlongint;
  1250. getmem(str,len);
  1251. ppufile.getdata(str^,len);
  1252. end;
  1253. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1254. begin
  1255. inherited ppuwrite(ppufile);
  1256. ppufile.putlongint(len);
  1257. ppufile.putdata(str^,len);
  1258. end;
  1259. function tai_string.getcopy : tlinkedlistitem;
  1260. var
  1261. p : tlinkedlistitem;
  1262. begin
  1263. p:=inherited getcopy;
  1264. getmem(tai_string(p).str,len);
  1265. move(str^,tai_string(p).str^,len);
  1266. getcopy:=p;
  1267. end;
  1268. {****************************************************************************
  1269. TAI_LABEL
  1270. ****************************************************************************}
  1271. constructor tai_label.create(_labsym : tasmlabel);
  1272. begin
  1273. inherited Create;
  1274. typ:=ait_label;
  1275. labsym:=_labsym;
  1276. labsym.is_set:=true;
  1277. is_global:=(labsym.bind=AB_GLOBAL);
  1278. end;
  1279. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1280. begin
  1281. inherited ppuload(t,ppufile);
  1282. labsym:=tasmlabel(ppufile.getasmsymbol);
  1283. is_global:=boolean(ppufile.getbyte);
  1284. end;
  1285. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1286. begin
  1287. inherited ppuwrite(ppufile);
  1288. ppufile.putasmsymbol(labsym);
  1289. ppufile.putbyte(byte(is_global));
  1290. end;
  1291. procedure tai_label.derefimpl;
  1292. begin
  1293. labsym.is_set:=true;
  1294. end;
  1295. {****************************************************************************
  1296. tai_comment comment to be inserted in the assembler file
  1297. ****************************************************************************}
  1298. constructor tai_comment.Create(_str : pchar);
  1299. begin
  1300. inherited Create;
  1301. typ:=ait_comment;
  1302. str:=_str;
  1303. end;
  1304. destructor tai_comment.destroy;
  1305. begin
  1306. freemem(str);
  1307. inherited Destroy;
  1308. end;
  1309. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1310. var
  1311. len : longint;
  1312. begin
  1313. inherited ppuload(t,ppufile);
  1314. len:=ppufile.getlongint;
  1315. getmem(str,len+1);
  1316. ppufile.getdata(str^,len);
  1317. str[len]:=#0;
  1318. end;
  1319. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1320. var
  1321. len : longint;
  1322. begin
  1323. inherited ppuwrite(ppufile);
  1324. len:=strlen(str);
  1325. ppufile.putlongint(len);
  1326. ppufile.putdata(str^,len);
  1327. end;
  1328. function tai_comment.getcopy : tlinkedlistitem;
  1329. var
  1330. p : tlinkedlistitem;
  1331. begin
  1332. p:=inherited getcopy;
  1333. getmem(tai_comment(p).str,strlen(str)+1);
  1334. move(str^,tai_comment(p).str^,strlen(str)+1);
  1335. getcopy:=p;
  1336. end;
  1337. {****************************************************************************
  1338. TAI_STABS
  1339. ****************************************************************************}
  1340. constructor tai_stab.create(_stabtype:TStabType;_str : pchar);
  1341. begin
  1342. inherited create;
  1343. typ:=ait_stab;
  1344. str:=_str;
  1345. stabtype:=_stabtype;
  1346. end;
  1347. constructor tai_stab.create_str(_stabtype:TStabType;const s:string);
  1348. begin
  1349. self.create(_stabtype,strpnew(s));
  1350. end;
  1351. destructor tai_stab.destroy;
  1352. begin
  1353. freemem(str);
  1354. inherited destroy;
  1355. end;
  1356. {****************************************************************************
  1357. TAI_FORCE_LINE
  1358. ****************************************************************************}
  1359. constructor tai_force_line.create;
  1360. begin
  1361. inherited create;
  1362. typ:=ait_force_line;
  1363. end;
  1364. {****************************************************************************
  1365. TAI_FUNCTION_NAME
  1366. ****************************************************************************}
  1367. constructor tai_function_name.create(const s:string);
  1368. begin
  1369. inherited create;
  1370. typ:=ait_function_name;
  1371. funcname:=stringdup(s);
  1372. end;
  1373. destructor tai_function_name.destroy;
  1374. begin
  1375. stringdispose(funcname);
  1376. inherited destroy;
  1377. end;
  1378. {****************************************************************************
  1379. TAI_CUTOBJECT
  1380. ****************************************************************************}
  1381. constructor tai_cutobject.Create;
  1382. begin
  1383. inherited Create;
  1384. typ:=ait_cutobject;
  1385. place:=cut_normal;
  1386. end;
  1387. constructor tai_cutobject.Create_begin;
  1388. begin
  1389. inherited Create;
  1390. typ:=ait_cutobject;
  1391. place:=cut_begin;
  1392. end;
  1393. constructor tai_cutobject.Create_end;
  1394. begin
  1395. inherited Create;
  1396. typ:=ait_cutobject;
  1397. place:=cut_end;
  1398. end;
  1399. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1400. begin
  1401. inherited ppuload(t,ppufile);
  1402. place:=TCutPlace(ppufile.getbyte);
  1403. end;
  1404. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1405. begin
  1406. inherited ppuwrite(ppufile);
  1407. ppufile.putbyte(byte(place));
  1408. end;
  1409. {****************************************************************************
  1410. Tai_Marker
  1411. ****************************************************************************}
  1412. constructor Tai_Marker.Create(_Kind: TAsmMarker);
  1413. begin
  1414. Inherited Create;
  1415. typ := ait_marker;
  1416. Kind := _Kind;
  1417. end;
  1418. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1419. begin
  1420. inherited ppuload(t,ppufile);
  1421. kind:=TAsmMarker(ppufile.getbyte);
  1422. end;
  1423. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1424. begin
  1425. inherited ppuwrite(ppufile);
  1426. ppufile.putbyte(byte(kind));
  1427. end;
  1428. {*****************************************************************************
  1429. tai_tempalloc
  1430. *****************************************************************************}
  1431. constructor tai_tempalloc.alloc(pos,size:longint);
  1432. begin
  1433. inherited Create;
  1434. typ:=ait_tempalloc;
  1435. allocation:=true;
  1436. temppos:=pos;
  1437. tempsize:=size;
  1438. {$ifdef EXTDEBUG}
  1439. problem:=nil;
  1440. {$endif EXTDEBUG}
  1441. end;
  1442. destructor tai_tempalloc.destroy;
  1443. begin
  1444. {$ifdef EXTDEBUG}
  1445. stringdispose(problem);
  1446. {$endif EXTDEBUG}
  1447. inherited destroy;
  1448. end;
  1449. constructor tai_tempalloc.dealloc(pos,size:longint);
  1450. begin
  1451. inherited Create;
  1452. typ:=ait_tempalloc;
  1453. allocation:=false;
  1454. temppos:=pos;
  1455. tempsize:=size;
  1456. {$ifdef EXTDEBUG}
  1457. problem:=nil;
  1458. {$endif EXTDEBUG}
  1459. end;
  1460. {$ifdef EXTDEBUG}
  1461. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1462. begin
  1463. inherited Create;
  1464. typ:=ait_tempalloc;
  1465. allocation:=false;
  1466. temppos:=pos;
  1467. tempsize:=size;
  1468. problem:=stringdup(st);
  1469. end;
  1470. {$endif EXTDEBUG}
  1471. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1472. begin
  1473. inherited ppuload(t,ppufile);
  1474. temppos:=ppufile.getlongint;
  1475. tempsize:=ppufile.getlongint;
  1476. allocation:=boolean(ppufile.getbyte);
  1477. {$ifdef EXTDEBUG}
  1478. problem:=nil;
  1479. {$endif EXTDEBUG}
  1480. end;
  1481. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1482. begin
  1483. inherited ppuwrite(ppufile);
  1484. ppufile.putlongint(temppos);
  1485. ppufile.putlongint(tempsize);
  1486. ppufile.putbyte(byte(allocation));
  1487. end;
  1488. {*****************************************************************************
  1489. tai_regalloc
  1490. *****************************************************************************}
  1491. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  1492. begin
  1493. inherited create;
  1494. typ:=ait_regalloc;
  1495. ratype:=ra_alloc;
  1496. reg:=r;
  1497. { ainstr must be an instruction }
  1498. if assigned(ainstr) and
  1499. (ainstr.typ<>ait_instruction) then
  1500. internalerror(200411011);
  1501. instr:=ainstr;
  1502. end;
  1503. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  1504. begin
  1505. inherited create;
  1506. typ:=ait_regalloc;
  1507. ratype:=ra_dealloc;
  1508. reg:=r;
  1509. { ainstr must be an instruction }
  1510. if assigned(ainstr) and
  1511. (ainstr.typ<>ait_instruction) then
  1512. internalerror(200411012);
  1513. instr:=ainstr;
  1514. end;
  1515. constructor tai_regalloc.sync(r : tregister);
  1516. begin
  1517. inherited create;
  1518. typ:=ait_regalloc;
  1519. ratype:=ra_sync;
  1520. reg:=r;
  1521. end;
  1522. constructor tai_regalloc.resize(r : tregister);
  1523. begin
  1524. inherited create;
  1525. typ:=ait_regalloc;
  1526. ratype:=ra_resize;
  1527. reg:=r;
  1528. end;
  1529. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1530. begin
  1531. inherited ppuload(t,ppufile);
  1532. ppufile.getdata(reg,sizeof(Tregister));
  1533. ratype:=tregalloctype(ppufile.getbyte);
  1534. end;
  1535. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1536. begin
  1537. inherited ppuwrite(ppufile);
  1538. ppufile.putdata(reg,sizeof(Tregister));
  1539. ppufile.putbyte(byte(ratype));
  1540. end;
  1541. {*****************************************************************************
  1542. TaiInstruction
  1543. *****************************************************************************}
  1544. constructor tai_cpu_abstract.Create(op : tasmop);
  1545. begin
  1546. inherited create;
  1547. typ:=ait_instruction;
  1548. is_jmp:=false;
  1549. opcode:=op;
  1550. ops:=0;
  1551. fillchar(condition,sizeof(condition),0);
  1552. fillchar(oper,sizeof(oper),0);
  1553. end;
  1554. destructor tai_cpu_abstract.Destroy;
  1555. var
  1556. i : integer;
  1557. begin
  1558. for i:=0 to opercnt-1 do
  1559. freeop(i);
  1560. inherited destroy;
  1561. end;
  1562. { ---------------------------------------------------------------------
  1563. Loading of operands.
  1564. ---------------------------------------------------------------------}
  1565. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  1566. begin
  1567. while (opers>opercnt) do
  1568. begin
  1569. new(oper[opercnt]);
  1570. fillchar(oper[opercnt]^,sizeof(toper),0);
  1571. inc(opercnt);
  1572. end;
  1573. end;
  1574. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  1575. begin
  1576. allocate_oper(opidx+1);
  1577. with oper[opidx]^ do
  1578. begin
  1579. if typ<>top_const then
  1580. clearop(opidx);
  1581. val:=l;
  1582. typ:=top_const;
  1583. end;
  1584. end;
  1585. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1586. var
  1587. r : treference;
  1588. begin
  1589. reference_reset_symbol(r,s,sofs);
  1590. r.refaddr:=addr_full;
  1591. loadref(opidx,r);
  1592. end;
  1593. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  1594. begin
  1595. if not assigned(s) then
  1596. internalerror(200204251);
  1597. allocate_oper(opidx+1);
  1598. with oper[opidx]^ do
  1599. begin
  1600. if typ<>top_local then
  1601. begin
  1602. clearop(opidx);
  1603. new(localoper);
  1604. end;
  1605. with oper[opidx]^.localoper^ do
  1606. begin
  1607. localsym:=s;
  1608. localsymofs:=sofs;
  1609. localindexreg:=indexreg;
  1610. localscale:=scale;
  1611. localgetoffset:=getoffset;
  1612. localforceref:=forceref;
  1613. end;
  1614. typ:=top_local;
  1615. end;
  1616. end;
  1617. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  1618. begin
  1619. allocate_oper(opidx+1);
  1620. with oper[opidx]^ do
  1621. begin
  1622. if typ<>top_ref then
  1623. begin
  1624. clearop(opidx);
  1625. new(ref);
  1626. end;
  1627. ref^:=r;
  1628. {$ifdef x86}
  1629. { We allow this exception for x86, since overloading this would be
  1630. too much of a a speed penalty}
  1631. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1632. segprefix:=ref^.segment;
  1633. {$endif}
  1634. if (cs_create_pic in current_settings.moduleswitches) and
  1635. assigned(r.symbol) and
  1636. not assigned(r.relsymbol) and
  1637. (r.refaddr=addr_no) then
  1638. internalerror(200502052);
  1639. typ:=top_ref;
  1640. if assigned(add_reg_instruction_hook) then
  1641. begin
  1642. add_reg_instruction_hook(self,ref^.base);
  1643. add_reg_instruction_hook(self,ref^.index);
  1644. end;
  1645. { mark symbol as used }
  1646. if assigned(ref^.symbol) then
  1647. ref^.symbol.increfs;
  1648. if assigned(ref^.relsymbol) then
  1649. ref^.relsymbol.increfs;
  1650. end;
  1651. end;
  1652. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  1653. begin
  1654. allocate_oper(opidx+1);
  1655. with oper[opidx]^ do
  1656. begin
  1657. if typ<>top_reg then
  1658. clearop(opidx);
  1659. reg:=r;
  1660. typ:=top_reg;
  1661. end;
  1662. if assigned(add_reg_instruction_hook) then
  1663. add_reg_instruction_hook(self,r);
  1664. {$ifdef ARM}
  1665. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1666. Due to speed considerations we don't use a virtual overridden method here.
  1667. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1668. problems with iregs getting r15.
  1669. }
  1670. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1671. {$endif ARM}
  1672. end;
  1673. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  1674. begin
  1675. allocate_oper(opidx+1);
  1676. clearop(opidx);
  1677. oper[opidx]^:=o;
  1678. { copy also the reference }
  1679. with oper[opidx]^ do
  1680. begin
  1681. case typ of
  1682. top_reg:
  1683. begin
  1684. if assigned(add_reg_instruction_hook) then
  1685. add_reg_instruction_hook(self,reg);
  1686. end;
  1687. top_ref:
  1688. begin
  1689. new(ref);
  1690. ref^:=o.ref^;
  1691. if assigned(add_reg_instruction_hook) then
  1692. begin
  1693. add_reg_instruction_hook(self,ref^.base);
  1694. add_reg_instruction_hook(self,ref^.index);
  1695. end;
  1696. end;
  1697. {$ifdef ARM}
  1698. top_shifterop:
  1699. begin
  1700. new(shifterop);
  1701. shifterop^:=o.shifterop^;
  1702. if assigned(add_reg_instruction_hook) then
  1703. add_reg_instruction_hook(self,shifterop^.rs);
  1704. end;
  1705. {$endif ARM}
  1706. {$ifdef support_llvm}
  1707. top_string:
  1708. begin
  1709. str:=strnew(o.str);
  1710. end;
  1711. {$endif support_llvm}
  1712. end;
  1713. end;
  1714. end;
  1715. procedure tai_cpu_abstract.clearop(opidx:longint);
  1716. begin
  1717. with oper[opidx]^ do
  1718. begin
  1719. case typ of
  1720. top_ref:
  1721. dispose(ref);
  1722. top_local:
  1723. dispose(localoper);
  1724. {$ifdef ARM}
  1725. top_shifterop:
  1726. dispose(shifterop);
  1727. top_regset:
  1728. dispose(regset);
  1729. {$endif ARM}
  1730. {$ifdef support_llvm}
  1731. top_string:
  1732. begin
  1733. freemem(str);
  1734. end;
  1735. {$endif support_llvm}
  1736. end;
  1737. typ:=top_none;
  1738. end;
  1739. end;
  1740. procedure tai_cpu_abstract.freeop(opidx:longint);
  1741. begin
  1742. clearop(opidx);
  1743. dispose(oper[opidx]);
  1744. end;
  1745. { ---------------------------------------------------------------------
  1746. Miscellaneous methods.
  1747. ---------------------------------------------------------------------}
  1748. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  1749. begin
  1750. condition:=c;
  1751. end;
  1752. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  1753. var
  1754. i : longint;
  1755. p : tai_cpu_abstract;
  1756. begin
  1757. p:=tai_cpu_abstract(inherited getcopy);
  1758. { make a copy of the references }
  1759. p.opercnt:=0;
  1760. p.allocate_oper(ops);
  1761. for i:=0 to ops-1 do
  1762. begin
  1763. p.oper[i]^:=oper[i]^;
  1764. case oper[i]^.typ of
  1765. top_local :
  1766. begin
  1767. new(p.oper[i]^.localoper);
  1768. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  1769. end;
  1770. top_ref :
  1771. begin
  1772. new(p.oper[i]^.ref);
  1773. p.oper[i]^.ref^:=oper[i]^.ref^;
  1774. end;
  1775. {$ifdef ARM}
  1776. top_shifterop:
  1777. begin
  1778. new(p.oper[i]^.shifterop);
  1779. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  1780. end;
  1781. {$endif ARM}
  1782. end;
  1783. end;
  1784. getcopy:=p;
  1785. end;
  1786. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  1787. begin
  1788. { When the generic RA is used this needs to be overriden, we don't use
  1789. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  1790. when tai_cpu is created (PFV) }
  1791. internalerror(200404091);
  1792. result:=false;
  1793. end;
  1794. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  1795. begin
  1796. internalerror(200404091);
  1797. result:=operand_readwrite;
  1798. end;
  1799. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  1800. begin
  1801. result := operand_read;
  1802. end;
  1803. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1804. var
  1805. i : integer;
  1806. begin
  1807. inherited ppuload(t,ppufile);
  1808. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1809. ppufile.getdata(condition,sizeof(tasmcond));
  1810. allocate_oper(ppufile.getbyte);
  1811. for i:=0 to ops-1 do
  1812. ppuloadoper(ppufile,oper[i]^);
  1813. opcode:=tasmop(ppufile.getword);
  1814. {$ifdef x86}
  1815. ppufile.getdata(segprefix,sizeof(Tregister));
  1816. {$endif x86}
  1817. is_jmp:=boolean(ppufile.getbyte);
  1818. end;
  1819. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1820. var
  1821. i : integer;
  1822. begin
  1823. inherited ppuwrite(ppufile);
  1824. ppufile.putdata(condition,sizeof(tasmcond));
  1825. ppufile.putbyte(ops);
  1826. for i:=0 to ops-1 do
  1827. ppuwriteoper(ppufile,oper[i]^);
  1828. ppufile.putword(word(opcode));
  1829. {$ifdef x86}
  1830. ppufile.putdata(segprefix,sizeof(Tregister));
  1831. {$endif x86}
  1832. ppufile.putbyte(byte(is_jmp));
  1833. end;
  1834. procedure tai_cpu_abstract.buildderefimpl;
  1835. var
  1836. i : integer;
  1837. begin
  1838. for i:=0 to ops-1 do
  1839. ppubuildderefimploper(oper[i]^);
  1840. end;
  1841. procedure tai_cpu_abstract.derefimpl;
  1842. var
  1843. i : integer;
  1844. begin
  1845. for i:=0 to ops-1 do
  1846. ppuderefoper(oper[i]^);
  1847. end;
  1848. procedure tai_cpu_abstract.resetpass1;
  1849. begin
  1850. end;
  1851. procedure tai_cpu_abstract.resetpass2;
  1852. begin
  1853. end;
  1854. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  1855. begin
  1856. result:=0;
  1857. end;
  1858. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  1859. begin
  1860. end;
  1861. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1862. begin
  1863. o.typ:=toptype(ppufile.getbyte);
  1864. o.ot:=ppufile.getlongint;
  1865. case o.typ of
  1866. top_reg :
  1867. ppufile.getdata(o.reg,sizeof(Tregister));
  1868. top_ref :
  1869. begin
  1870. new(o.ref);
  1871. {$ifdef x86}
  1872. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  1873. {$endif x86}
  1874. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  1875. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  1876. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  1877. o.ref^.scalefactor:=ppufile.getbyte;
  1878. o.ref^.offset:=ppufile.getaint;
  1879. o.ref^.symbol:=ppufile.getasmsymbol;
  1880. o.ref^.relsymbol:=ppufile.getasmsymbol;
  1881. end;
  1882. top_const :
  1883. o.val:=ppufile.getaint;
  1884. top_local :
  1885. begin
  1886. new(o.localoper);
  1887. with o.localoper^ do
  1888. begin
  1889. ppufile.getderef(localsymderef);
  1890. localsymofs:=ppufile.getaint;
  1891. localindexreg:=tregister(ppufile.getlongint);
  1892. localscale:=ppufile.getbyte;
  1893. localgetoffset:=(ppufile.getbyte<>0);
  1894. end;
  1895. end;
  1896. else
  1897. internalerror(2007010210);
  1898. end;
  1899. end;
  1900. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1901. begin
  1902. ppufile.putbyte(byte(o.typ));
  1903. ppufile.putlongint(o.ot);
  1904. case o.typ of
  1905. top_reg :
  1906. ppufile.putdata(o.reg,sizeof(Tregister));
  1907. top_ref :
  1908. begin
  1909. {$ifdef x86}
  1910. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  1911. {$endif x86}
  1912. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  1913. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  1914. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  1915. ppufile.putbyte(o.ref^.scalefactor);
  1916. ppufile.putaint(o.ref^.offset);
  1917. ppufile.putasmsymbol(o.ref^.symbol);
  1918. ppufile.putasmsymbol(o.ref^.relsymbol);
  1919. end;
  1920. top_const :
  1921. ppufile.putaint(o.val);
  1922. top_local :
  1923. begin
  1924. with o.localoper^ do
  1925. begin
  1926. ppufile.putderef(localsymderef);
  1927. ppufile.putaint(localsymofs);
  1928. ppufile.putlongint(longint(localindexreg));
  1929. ppufile.putbyte(localscale);
  1930. ppufile.putbyte(byte(localgetoffset));
  1931. end;
  1932. end;
  1933. else
  1934. internalerror(2007010211);
  1935. end;
  1936. end;
  1937. {****************************************************************************
  1938. tai_align_abstract
  1939. ****************************************************************************}
  1940. constructor tai_align_abstract.Create(b: byte);
  1941. begin
  1942. inherited Create;
  1943. typ:=ait_align;
  1944. {$ifdef EXTDEBUG}
  1945. if upper(classname)='TAI_ALIGN_ABSTRACT' then
  1946. internalerror(200709191);
  1947. {$endif EXTDEBUG}
  1948. if b in [1,2,4,8,16,32] then
  1949. aligntype := b
  1950. else
  1951. aligntype := 1;
  1952. fillsize:=0;
  1953. fillop:=0;
  1954. use_op:=false;
  1955. end;
  1956. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  1957. begin
  1958. inherited Create;
  1959. typ:=ait_align;
  1960. if b in [1,2,4,8,16,32] then
  1961. aligntype := b
  1962. else
  1963. aligntype := 1;
  1964. fillsize:=0;
  1965. fillop:=_op;
  1966. use_op:=true;
  1967. end;
  1968. constructor tai_align_abstract.Create_zeros(b: byte);
  1969. begin
  1970. inherited Create;
  1971. typ:=ait_align;
  1972. if b in [1,2,4,8,16,32] then
  1973. aligntype := b
  1974. else
  1975. aligntype := 1;
  1976. use_op:=true;
  1977. fillsize:=0;
  1978. fillop:=0;
  1979. end;
  1980. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer):pchar;
  1981. begin
  1982. if fillsize>sizeof(buf) then
  1983. internalerror(200404293);
  1984. fillchar(buf,high(buf),fillop);
  1985. calculatefillbuf:=pchar(@buf);
  1986. end;
  1987. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1988. begin
  1989. inherited ppuload(t,ppufile);
  1990. aligntype:=ppufile.getbyte;
  1991. fillsize:=0;
  1992. fillop:=ppufile.getbyte;
  1993. use_op:=boolean(ppufile.getbyte);
  1994. end;
  1995. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  1996. begin
  1997. inherited ppuwrite(ppufile);
  1998. ppufile.putbyte(aligntype);
  1999. ppufile.putbyte(fillop);
  2000. ppufile.putbyte(byte(use_op));
  2001. end;
  2002. {$ifdef support_llvm_typedef}
  2003. {****************************************************************************
  2004. tai_typedef
  2005. ****************************************************************************}
  2006. constructor tai_typedef.create(const typename: string; const typedef: ansistring);
  2007. begin
  2008. inherited Create;
  2009. typ:=ait_typedef;
  2010. typestr:=strpnew(typename);
  2011. defstr:=typedef;
  2012. end
  2013. destructor tai_typedef.destroy;
  2014. begin
  2015. stringdispose(typestr);
  2016. inherited destroy;
  2017. end;
  2018. constructor tai_typedef.ppuload(t:taitype;ppufile:tcompilerppufile);
  2019. var
  2020. len: aint;
  2021. begin
  2022. inherited ppuload(t,ppufile);
  2023. typestr:=strpnew(ppufile.getstring);
  2024. len:=ppufile.getaint;
  2025. setlength(defstr,len);
  2026. ppufile.getdata(destr[1],len);
  2027. end;
  2028. procedure tai_typedef.ppuwrite(ppufile:tcompilerppufile);
  2029. begin
  2030. inherited ppuwrite(ppufile);
  2031. ppufile.putstring(typestr^);
  2032. ppufile.putaint(length(defstr));
  2033. ppufile.putdata(defstr[1],length(defstr));
  2034. end;
  2035. {$endif support_llvm_typedef}
  2036. begin
  2037. cai_cpu:=tai_cpu_abstract;
  2038. { aasmcpu is earlier in the unit order and can
  2039. already set the cai_align }
  2040. if not assigned(cai_align) then
  2041. cai_align:=tai_align_abstract;
  2042. end.