aasmtai.pas 71 KB

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