aasmtai.pas 72 KB

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