aasmtai.pas 71 KB

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