aasmtai.pas 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486
  1. {
  2. Copyright (c) 1998-2006 by Florian Klaempfl
  3. This unit implements an abstract asmoutput class for all processor types
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { @abstract(This unit implements an abstract asm output class for all processor types)
  18. This unit implements an abstract assembler output class for all processors, these
  19. are then overriden for each assembler writer to actually write the data in these
  20. classes to an assembler file.
  21. }
  22. unit aasmtai;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. cutils,cclasses,
  27. globtype,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_start,
  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_dwarf_info,
  568. al_dwarf_abbrev,
  569. al_dwarf_line,
  570. al_picdata,
  571. al_resourcestrings,
  572. al_end);
  573. const
  574. TasmlistStr : array[tasmlist] of string[24] =(
  575. 'al_begin',
  576. 'al_stabs',
  577. 'al_procedures',
  578. 'al_globals',
  579. 'al_const',
  580. 'al_typedconsts',
  581. 'al_rotypedconsts',
  582. 'al_threadvars',
  583. 'al_imports',
  584. 'al_exports',
  585. 'al_resources',
  586. 'al_rtti',
  587. 'al_dwarf',
  588. 'al_dwarf_info',
  589. 'al_dwarf_abbrev',
  590. 'al_dwarf_line',
  591. 'al_picdata',
  592. 'al_resourcestrings',
  593. 'al_end');
  594. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized');
  595. tempallocstr : array[boolean] of string[10]=('released','allocated');
  596. stabtypestr : array[tstabtype] of string[5]=('stabs','stabn','stabd');
  597. directivestr : array[tasmdirective] of string[24]=(
  598. 'non_lazy_symbol_pointer','indirect_symbol','lazy_symbol_pointer',
  599. 'extern','nasm_import', 'tc'
  600. );
  601. var
  602. { array with all class types for tais }
  603. aiclass : taiclassarray;
  604. { Current expression list }
  605. exprasmlist : taasmoutput;
  606. { labels for BREAK and CONTINUE }
  607. aktbreaklabel,aktcontinuelabel : tasmlabel;
  608. { label when the result is true or false }
  609. truelabel,falselabel : tasmlabel;
  610. { hook to notify uses of registers }
  611. add_reg_instruction_hook : tadd_reg_instruction_proc;
  612. asmlist:array[Tasmlist] of Taasmoutput;
  613. cai_align : tai_align_class;
  614. cai_cpu : tai_cpu_class;
  615. function use_smartlink_section:boolean;
  616. function maybe_smartlink_symbol:boolean;
  617. procedure maybe_new_object_file(list:taasmoutput);
  618. procedure new_section(list:taasmoutput;Asectype:TAsmSectionType;Aname:string;Aalign:byte);
  619. procedure section_symbol_start(list:taasmoutput;const Aname:string;Asymtyp:Tasmsymtype;
  620. Aglobal:boolean;Asectype:TAsmSectionType;Aalign:byte);
  621. procedure section_symbol_end(list:taasmoutput;const Aname:string);
  622. function ppuloadai(ppufile:tcompilerppufile):tai;
  623. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  624. implementation
  625. uses
  626. strings,
  627. verbose;
  628. const
  629. pputaimarker = 254;
  630. {****************************************************************************
  631. Helpers
  632. ****************************************************************************}
  633. function ppuloadai(ppufile:tcompilerppufile):tai;
  634. var
  635. b : byte;
  636. t : taitype;
  637. begin
  638. { marker }
  639. b:=ppufile.getbyte;
  640. if b<>pputaimarker then
  641. internalerror(200208181);
  642. { load nodetype }
  643. t:=taitype(ppufile.getbyte);
  644. if t<>ait_none then
  645. begin
  646. if t>high(taitype) then
  647. internalerror(200208182);
  648. if not assigned(aiclass[t]) then
  649. internalerror(200208183);
  650. {writeln('taiload: ',taitypestr[t]);}
  651. { generate tai of the correct class }
  652. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  653. end
  654. else
  655. ppuloadai:=nil;
  656. end;
  657. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  658. begin
  659. { marker, read by ppuloadnode }
  660. ppufile.putbyte(pputaimarker);
  661. if assigned(n) then
  662. begin
  663. { type, read by ppuloadnode }
  664. ppufile.putbyte(byte(n.typ));
  665. {writeln('taiwrite: ',taitypestr[n.typ]);}
  666. n.ppuwrite(ppufile);
  667. end
  668. else
  669. ppufile.putbyte(byte(ait_none));
  670. end;
  671. function use_smartlink_section:boolean;
  672. begin
  673. result:=(af_smartlink_sections in target_asm.flags) and
  674. (tf_smartlink_sections in target_info.flags);
  675. end;
  676. function maybe_smartlink_symbol:boolean;
  677. begin
  678. result:=(cs_create_smart in aktmoduleswitches) or
  679. use_smartlink_section;
  680. end;
  681. procedure maybe_new_object_file(list:taasmoutput);
  682. begin
  683. if (cs_create_smart in aktmoduleswitches) and
  684. (not use_smartlink_section) then
  685. list.concat(tai_cutobject.create);
  686. end;
  687. procedure new_section(list:taasmoutput;Asectype:TAsmSectionType;Aname:string;Aalign:byte);
  688. begin
  689. list.concat(tai_section.create(Asectype,Aname,Aalign));
  690. list.concat(cai_align.create(Aalign));
  691. end;
  692. procedure section_symbol_start(list:taasmoutput;const Aname:string;Asymtyp:Tasmsymtype;
  693. Aglobal:boolean;Asectype:TAsmSectionType;Aalign:byte);
  694. begin
  695. maybe_new_object_file(list);
  696. list.concat(tai_section.create(Asectype,Aname,Aalign));
  697. list.concat(cai_align.create(Aalign));
  698. if Aglobal or
  699. maybe_smartlink_symbol then
  700. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  701. else
  702. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  703. end;
  704. procedure section_symbol_end(list:taasmoutput;const Aname:string);
  705. begin
  706. list.concat(tai_symbol_end.createname(Aname));
  707. end;
  708. {****************************************************************************
  709. TAI
  710. ****************************************************************************}
  711. constructor tai.Create;
  712. begin
  713. {$ifndef NOOPT}
  714. optinfo:=nil;
  715. {$endif NOOPT}
  716. end;
  717. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  718. begin
  719. typ:=t;
  720. {$ifndef NOOPT}
  721. optinfo:=nil;
  722. {$endif}
  723. end;
  724. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  725. begin
  726. end;
  727. procedure tai.buildderefimpl;
  728. begin
  729. end;
  730. procedure tai.derefimpl;
  731. begin
  732. end;
  733. {****************************************************************************
  734. TAILINEINFO
  735. ****************************************************************************}
  736. constructor tailineinfo.create;
  737. begin
  738. inherited create;
  739. if not(inlining_procedure and
  740. (cs_gdb_valgrind in aktglobalswitches)) then
  741. fileinfo:=aktfilepos;
  742. end;
  743. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  744. begin
  745. inherited ppuload(t,ppufile);
  746. ppufile.getposinfo(fileinfo);
  747. end;
  748. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  749. begin
  750. inherited ppuwrite(ppufile);
  751. ppufile.putposinfo(fileinfo);
  752. end;
  753. {****************************************************************************
  754. TAI_SIMPLE
  755. ****************************************************************************}
  756. constructor tai_simple.create(_typ : taitype);
  757. begin
  758. inherited create;
  759. typ:=_typ;
  760. end;
  761. {****************************************************************************
  762. TAI_SECTION
  763. ****************************************************************************}
  764. constructor tai_section.Create(Asectype:TAsmSectionType;Aname:string;Aalign:byte);
  765. begin
  766. inherited Create;
  767. typ:=ait_section;
  768. sectype:=asectype;
  769. secalign:=Aalign;
  770. name:=stringdup(Aname);
  771. sec:=nil;
  772. end;
  773. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  774. begin
  775. inherited ppuload(t,ppufile);
  776. sectype:=tasmsectiontype(ppufile.getbyte);
  777. secalign:=ppufile.getbyte;
  778. name:=stringdup(ppufile.getstring);
  779. sec:=nil;
  780. end;
  781. destructor tai_section.Destroy;
  782. begin
  783. stringdispose(name);
  784. end;
  785. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  786. begin
  787. inherited ppuwrite(ppufile);
  788. ppufile.putbyte(byte(sectype));
  789. ppufile.putbyte(secalign);
  790. ppufile.putstring(name^);
  791. end;
  792. {****************************************************************************
  793. TAI_DATABLOCK
  794. ****************************************************************************}
  795. constructor tai_datablock.Create(const _name : string;_size : longint);
  796. begin
  797. inherited Create;
  798. typ:=ait_datablock;
  799. sym:=objectlibrary.newasmsymbol(_name,AB_LOCAL,AT_DATA);
  800. { keep things aligned }
  801. if _size<=0 then
  802. _size:=4;
  803. size:=_size;
  804. is_global:=false;
  805. end;
  806. constructor tai_datablock.Create_global(const _name : string;_size : longint);
  807. begin
  808. inherited Create;
  809. typ:=ait_datablock;
  810. sym:=objectlibrary.newasmsymbol(_name,AB_GLOBAL,AT_DATA);
  811. { keep things aligned }
  812. if _size<=0 then
  813. _size:=4;
  814. size:=_size;
  815. is_global:=true;
  816. end;
  817. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  818. begin
  819. inherited Create;
  820. sym:=ppufile.getasmsymbol;
  821. size:=ppufile.getlongint;
  822. is_global:=boolean(ppufile.getbyte);
  823. end;
  824. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  825. begin
  826. inherited ppuwrite(ppufile);
  827. ppufile.putasmsymbol(sym);
  828. ppufile.putlongint(size);
  829. ppufile.putbyte(byte(is_global));
  830. end;
  831. procedure tai_datablock.derefimpl;
  832. begin
  833. objectlibrary.DerefAsmsymbol(sym);
  834. end;
  835. {****************************************************************************
  836. TAI_SYMBOL
  837. ****************************************************************************}
  838. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  839. begin
  840. inherited Create;
  841. typ:=ait_symbol;
  842. sym:=_sym;
  843. size:=siz;
  844. sym.defbind:=AB_LOCAL;
  845. is_global:=false;
  846. end;
  847. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  848. begin
  849. inherited Create;
  850. typ:=ait_symbol;
  851. sym:=_sym;
  852. size:=siz;
  853. sym.defbind:=AB_GLOBAL;
  854. is_global:=true;
  855. end;
  856. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  857. begin
  858. inherited Create;
  859. typ:=ait_symbol;
  860. sym:=objectlibrary.newasmsymbol(_name,AB_LOCAL,_symtyp);
  861. size:=siz;
  862. is_global:=false;
  863. end;
  864. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  865. begin
  866. inherited Create;
  867. typ:=ait_symbol;
  868. sym:=objectlibrary.newasmsymbol(_name,AB_GLOBAL,_symtyp);
  869. size:=siz;
  870. is_global:=true;
  871. end;
  872. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  873. begin
  874. inherited ppuload(t,ppufile);
  875. sym:=ppufile.getasmsymbol;
  876. size:=ppufile.getlongint;
  877. is_global:=boolean(ppufile.getbyte);
  878. end;
  879. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  880. begin
  881. inherited ppuwrite(ppufile);
  882. ppufile.putasmsymbol(sym);
  883. ppufile.putlongint(size);
  884. ppufile.putbyte(byte(is_global));
  885. end;
  886. procedure tai_symbol.derefimpl;
  887. begin
  888. objectlibrary.DerefAsmsymbol(sym);
  889. end;
  890. {****************************************************************************
  891. TAI_SYMBOL_END
  892. ****************************************************************************}
  893. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  894. begin
  895. inherited Create;
  896. typ:=ait_symbol_end;
  897. sym:=_sym;
  898. end;
  899. constructor tai_symbol_end.Createname(const _name : string);
  900. begin
  901. inherited Create;
  902. typ:=ait_symbol_end;
  903. sym:=objectlibrary.newasmsymbol(_name,AB_GLOBAL,AT_NONE);
  904. end;
  905. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  906. begin
  907. inherited ppuload(t,ppufile);
  908. sym:=ppufile.getasmsymbol;
  909. end;
  910. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  911. begin
  912. inherited ppuwrite(ppufile);
  913. ppufile.putasmsymbol(sym);
  914. end;
  915. procedure tai_symbol_end.derefimpl;
  916. begin
  917. objectlibrary.DerefAsmsymbol(sym);
  918. end;
  919. {****************************************************************************
  920. TAI_SYMBOL_END
  921. ****************************************************************************}
  922. constructor tai_directive.Create(_directive:tasmdirective;const _name:string);
  923. begin
  924. inherited Create;
  925. typ:=ait_directive;
  926. name:=stringdup(_name);
  927. directive:=_directive;
  928. end;
  929. destructor tai_directive.Destroy;
  930. begin
  931. stringdispose(name);
  932. end;
  933. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  934. begin
  935. inherited ppuload(t,ppufile);
  936. name:=stringdup(ppufile.getstring);
  937. directive:=tasmdirective(ppufile.getbyte);
  938. end;
  939. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  940. begin
  941. inherited ppuwrite(ppufile);
  942. ppufile.putstring(name^);
  943. ppufile.putbyte(byte(directive));
  944. end;
  945. {****************************************************************************
  946. TAI_CONST
  947. ****************************************************************************}
  948. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  949. begin
  950. inherited Create;
  951. typ:=ait_const;
  952. consttype:=_typ;
  953. value:=_value;
  954. sym:=nil;
  955. endsym:=nil;
  956. end;
  957. constructor tai_const.Create_128bit(_value : int64);
  958. begin
  959. inherited Create;
  960. typ:=ait_const;
  961. consttype:=aitconst_128bit;
  962. value:=_value;
  963. sym:=nil;
  964. endsym:=nil;
  965. end;
  966. constructor tai_const.Create_64bit(_value : int64);
  967. begin
  968. inherited Create;
  969. typ:=ait_const;
  970. consttype:=aitconst_64bit;
  971. value:=_value;
  972. sym:=nil;
  973. endsym:=nil;
  974. end;
  975. constructor tai_const.Create_32bit(_value : longint);
  976. begin
  977. inherited Create;
  978. typ:=ait_const;
  979. consttype:=aitconst_32bit;
  980. value:=_value;
  981. sym:=nil;
  982. endsym:=nil;
  983. end;
  984. constructor tai_const.Create_16bit(_value : word);
  985. begin
  986. inherited Create;
  987. typ:=ait_const;
  988. consttype:=aitconst_16bit;
  989. value:=_value;
  990. sym:=nil;
  991. endsym:=nil;
  992. end;
  993. constructor tai_const.Create_8bit(_value : byte);
  994. begin
  995. inherited Create;
  996. typ:=ait_const;
  997. consttype:=aitconst_8bit;
  998. value:=_value;
  999. sym:=nil;
  1000. endsym:=nil;
  1001. end;
  1002. constructor tai_const.Create_sleb128bit(_value : int64);
  1003. begin
  1004. inherited Create;
  1005. typ:=ait_const;
  1006. consttype:=aitconst_sleb128bit;
  1007. value:=_value;
  1008. sym:=nil;
  1009. endsym:=nil;
  1010. end;
  1011. constructor tai_const.Create_uleb128bit(_value : qword);
  1012. begin
  1013. inherited Create;
  1014. typ:=ait_const;
  1015. consttype:=aitconst_uleb128bit;
  1016. value:=int64(_value);
  1017. sym:=nil;
  1018. endsym:=nil;
  1019. end;
  1020. constructor tai_const.Create_aint(_value : aint);
  1021. begin
  1022. inherited Create;
  1023. typ:=ait_const;
  1024. consttype:=aitconst_aint;
  1025. value:=_value;
  1026. sym:=nil;
  1027. endsym:=nil;
  1028. end;
  1029. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1030. begin
  1031. inherited Create;
  1032. typ:=ait_const;
  1033. consttype:=aitconst_ptr;
  1034. { sym is allowed to be nil, this is used to write nil pointers }
  1035. sym:=_sym;
  1036. endsym:=nil;
  1037. value:=0;
  1038. { update sym info }
  1039. if assigned(sym) then
  1040. sym.increfs;
  1041. end;
  1042. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  1043. begin
  1044. inherited Create;
  1045. typ:=ait_const;
  1046. consttype:=aitconst_ptr;
  1047. if not assigned(_sym) then
  1048. internalerror(200404121);
  1049. sym:=_sym;
  1050. endsym:=nil;
  1051. value:=ofs;
  1052. { update sym info }
  1053. sym.increfs;
  1054. end;
  1055. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1056. begin
  1057. inherited Create;
  1058. typ:=ait_const;
  1059. consttype:=_typ;
  1060. sym:=_sym;
  1061. endsym:=_endsym;
  1062. value:=0;
  1063. { update sym info }
  1064. sym.increfs;
  1065. endsym.increfs;
  1066. end;
  1067. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1068. begin
  1069. inherited Create;
  1070. typ:=ait_const;
  1071. consttype:=aitconst_rva_symbol;
  1072. sym:=_sym;
  1073. endsym:=nil;
  1074. value:=0;
  1075. { update sym info }
  1076. sym.increfs;
  1077. end;
  1078. constructor tai_const.Create_indirect_sym(_sym:tasmsymbol);
  1079. begin
  1080. inherited Create;
  1081. typ:=ait_const;
  1082. consttype:=aitconst_indirect_symbol;
  1083. sym:=_sym;
  1084. endsym:=nil;
  1085. value:=0;
  1086. { update sym info }
  1087. sym.increfs;
  1088. end;
  1089. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:aint);
  1090. begin
  1091. inherited Create;
  1092. typ:=ait_const;
  1093. consttype:=aitconst_ptr;
  1094. sym:=objectlibrary.newasmsymbol(name,AB_EXTERNAL,_symtyp);
  1095. endsym:=nil;
  1096. value:=ofs;
  1097. { update sym info }
  1098. sym.increfs;
  1099. end;
  1100. constructor tai_const.Createname_rva(const name:string);
  1101. begin
  1102. inherited Create;
  1103. typ:=ait_const;
  1104. consttype:=aitconst_rva_symbol;
  1105. sym:=objectlibrary.newasmsymbol(name,AB_EXTERNAL,AT_FUNCTION);
  1106. endsym:=nil;
  1107. value:=0;
  1108. { update sym info }
  1109. sym.increfs;
  1110. end;
  1111. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1112. begin
  1113. inherited ppuload(t,ppufile);
  1114. consttype:=taiconst_type(ppufile.getbyte);
  1115. sym:=ppufile.getasmsymbol;
  1116. endsym:=ppufile.getasmsymbol;
  1117. value:=ppufile.getint64;
  1118. end;
  1119. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1120. begin
  1121. inherited ppuwrite(ppufile);
  1122. ppufile.putbyte(byte(consttype));
  1123. ppufile.putasmsymbol(sym);
  1124. ppufile.putasmsymbol(endsym);
  1125. ppufile.putint64(value);
  1126. end;
  1127. procedure tai_const.derefimpl;
  1128. begin
  1129. objectlibrary.DerefAsmsymbol(sym);
  1130. objectlibrary.DerefAsmsymbol(endsym);
  1131. end;
  1132. function tai_const.getcopy:tlinkedlistitem;
  1133. begin
  1134. getcopy:=inherited getcopy;
  1135. { we need to increase the reference number }
  1136. sym.increfs;
  1137. if assigned(endsym) then
  1138. endsym.increfs;
  1139. end;
  1140. function tai_const.size:longint;
  1141. begin
  1142. case consttype of
  1143. aitconst_8bit :
  1144. result:=1;
  1145. aitconst_16bit :
  1146. result:=2;
  1147. aitconst_32bit :
  1148. result:=4;
  1149. aitconst_64bit :
  1150. result:=8;
  1151. aitconst_indirect_symbol,
  1152. aitconst_rva_symbol :
  1153. result:=sizeof(aint);
  1154. end;
  1155. end;
  1156. {****************************************************************************
  1157. TAI_real_32bit
  1158. ****************************************************************************}
  1159. constructor tai_real_32bit.Create(_value : ts32real);
  1160. begin
  1161. inherited Create;
  1162. typ:=ait_real_32bit;
  1163. value:=_value;
  1164. end;
  1165. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1166. begin
  1167. inherited ppuload(t,ppufile);
  1168. value:=ppufile.getreal;
  1169. end;
  1170. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  1171. begin
  1172. inherited ppuwrite(ppufile);
  1173. ppufile.putreal(value);
  1174. end;
  1175. {****************************************************************************
  1176. TAI_real_64bit
  1177. ****************************************************************************}
  1178. constructor tai_real_64bit.Create(_value : ts64real);
  1179. begin
  1180. inherited Create;
  1181. typ:=ait_real_64bit;
  1182. value:=_value;
  1183. end;
  1184. {$ifdef ARM}
  1185. constructor tai_real_64bit.Create_hiloswapped(_value : ts64real);
  1186. begin
  1187. inherited Create;
  1188. typ:=ait_real_64bit;
  1189. value:=_value;
  1190. formatoptions:=fo_hiloswapped;
  1191. end;
  1192. {$endif ARM}
  1193. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1194. begin
  1195. inherited ppuload(t,ppufile);
  1196. value:=ppufile.getreal;
  1197. {$ifdef ARM}
  1198. formatoptions:=tformatoptions(ppufile.getbyte);
  1199. {$endif ARM}
  1200. end;
  1201. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  1202. begin
  1203. inherited ppuwrite(ppufile);
  1204. ppufile.putreal(value);
  1205. {$ifdef ARM}
  1206. ppufile.putbyte(byte(formatoptions));
  1207. {$endif ARM}
  1208. end;
  1209. {****************************************************************************
  1210. TAI_real_80bit
  1211. ****************************************************************************}
  1212. constructor tai_real_80bit.Create(_value : ts80real);
  1213. begin
  1214. inherited Create;
  1215. typ:=ait_real_80bit;
  1216. value:=_value;
  1217. end;
  1218. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1219. begin
  1220. inherited ppuload(t,ppufile);
  1221. value:=ppufile.getreal;
  1222. end;
  1223. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  1224. begin
  1225. inherited ppuwrite(ppufile);
  1226. ppufile.putreal(value);
  1227. end;
  1228. {****************************************************************************
  1229. TAI_real_80bit
  1230. ****************************************************************************}
  1231. constructor tai_real_128bit.Create(_value : ts128real);
  1232. begin
  1233. inherited Create;
  1234. typ:=ait_real_128bit;
  1235. value:=_value;
  1236. end;
  1237. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1238. begin
  1239. inherited ppuload(t,ppufile);
  1240. value:=ppufile.getreal;
  1241. end;
  1242. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  1243. begin
  1244. inherited ppuwrite(ppufile);
  1245. ppufile.putreal(value);
  1246. end;
  1247. {****************************************************************************
  1248. Tai_comp_64bit
  1249. ****************************************************************************}
  1250. constructor tai_comp_64bit.Create(_value : ts64comp);
  1251. begin
  1252. inherited Create;
  1253. typ:=ait_comp_64bit;
  1254. value:=_value;
  1255. end;
  1256. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1257. begin
  1258. inherited ppuload(t,ppufile);
  1259. ppufile.putdata(value,sizeof(value));
  1260. end;
  1261. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  1262. begin
  1263. inherited ppuwrite(ppufile);
  1264. ppufile.getdata(value,sizeof(value));
  1265. end;
  1266. {****************************************************************************
  1267. TAI_STRING
  1268. ****************************************************************************}
  1269. constructor tai_string.Create(const _str : string);
  1270. begin
  1271. inherited Create;
  1272. typ:=ait_string;
  1273. len:=length(_str);
  1274. getmem(str,len+1);
  1275. strpcopy(str,_str);
  1276. end;
  1277. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1278. begin
  1279. inherited Create;
  1280. typ:=ait_string;
  1281. str:=_str;
  1282. len:=length;
  1283. end;
  1284. destructor tai_string.destroy;
  1285. begin
  1286. if str<>nil then
  1287. freemem(str);
  1288. inherited Destroy;
  1289. end;
  1290. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1291. begin
  1292. inherited ppuload(t,ppufile);
  1293. len:=ppufile.getlongint;
  1294. getmem(str,len);
  1295. ppufile.getdata(str^,len);
  1296. end;
  1297. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1298. begin
  1299. inherited ppuwrite(ppufile);
  1300. ppufile.putlongint(len);
  1301. ppufile.putdata(str^,len);
  1302. end;
  1303. function tai_string.getcopy : tlinkedlistitem;
  1304. var
  1305. p : tlinkedlistitem;
  1306. begin
  1307. p:=inherited getcopy;
  1308. getmem(tai_string(p).str,len);
  1309. move(str^,tai_string(p).str^,len);
  1310. getcopy:=p;
  1311. end;
  1312. {****************************************************************************
  1313. TAI_LABEL
  1314. ****************************************************************************}
  1315. constructor tai_label.create(_l : tasmlabel);
  1316. begin
  1317. inherited Create;
  1318. typ:=ait_label;
  1319. l:=_l;
  1320. l.is_set:=true;
  1321. is_global:=(l.defbind=AB_GLOBAL);
  1322. end;
  1323. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1324. begin
  1325. inherited ppuload(t,ppufile);
  1326. l:=tasmlabel(ppufile.getasmsymbol);
  1327. is_global:=boolean(ppufile.getbyte);
  1328. end;
  1329. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1330. begin
  1331. inherited ppuwrite(ppufile);
  1332. ppufile.putasmsymbol(l);
  1333. ppufile.putbyte(byte(is_global));
  1334. end;
  1335. procedure tai_label.derefimpl;
  1336. begin
  1337. objectlibrary.DerefAsmsymbol(tasmsymbol(l));
  1338. l.is_set:=true;
  1339. end;
  1340. {****************************************************************************
  1341. tai_comment comment to be inserted in the assembler file
  1342. ****************************************************************************}
  1343. constructor tai_comment.Create(_str : pchar);
  1344. begin
  1345. inherited Create;
  1346. typ:=ait_comment;
  1347. str:=_str;
  1348. end;
  1349. destructor tai_comment.destroy;
  1350. begin
  1351. strdispose(str);
  1352. inherited Destroy;
  1353. end;
  1354. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1355. var
  1356. len : longint;
  1357. begin
  1358. inherited ppuload(t,ppufile);
  1359. len:=ppufile.getlongint;
  1360. getmem(str,len+1);
  1361. ppufile.getdata(str^,len);
  1362. str[len]:=#0;
  1363. end;
  1364. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1365. var
  1366. len : longint;
  1367. begin
  1368. inherited ppuwrite(ppufile);
  1369. len:=strlen(str);
  1370. ppufile.putlongint(len);
  1371. ppufile.putdata(str^,len);
  1372. end;
  1373. function tai_comment.getcopy : tlinkedlistitem;
  1374. var
  1375. p : tlinkedlistitem;
  1376. begin
  1377. p:=inherited getcopy;
  1378. getmem(tai_comment(p).str,strlen(str)+1);
  1379. move(str^,tai_comment(p).str^,strlen(str)+1);
  1380. getcopy:=p;
  1381. end;
  1382. {****************************************************************************
  1383. TAI_STABS
  1384. ****************************************************************************}
  1385. constructor tai_stab.create(_stabtype:tstabtype;_str : pchar);
  1386. begin
  1387. inherited create;
  1388. typ:=ait_stab;
  1389. str:=_str;
  1390. stabtype:=_stabtype;
  1391. end;
  1392. constructor tai_stab.create_str(_stabtype:tstabtype;const s:string);
  1393. begin
  1394. self.create(_stabtype,strpnew(s));
  1395. end;
  1396. destructor tai_stab.destroy;
  1397. begin
  1398. strdispose(str);
  1399. inherited destroy;
  1400. end;
  1401. {****************************************************************************
  1402. TAI_FORCE_LINE
  1403. ****************************************************************************}
  1404. constructor tai_force_line.create;
  1405. begin
  1406. inherited create;
  1407. typ:=ait_force_line;
  1408. end;
  1409. {****************************************************************************
  1410. TAI_FUNCTION_NAME
  1411. ****************************************************************************}
  1412. constructor tai_function_name.create(const s:string);
  1413. begin
  1414. inherited create;
  1415. typ:=ait_function_name;
  1416. funcname:=stringdup(s);
  1417. end;
  1418. destructor tai_function_name.destroy;
  1419. begin
  1420. stringdispose(funcname);
  1421. inherited destroy;
  1422. end;
  1423. {****************************************************************************
  1424. TAI_CUTOBJECT
  1425. ****************************************************************************}
  1426. constructor tai_cutobject.Create;
  1427. begin
  1428. inherited Create;
  1429. typ:=ait_cutobject;
  1430. place:=cut_normal;
  1431. end;
  1432. constructor tai_cutobject.Create_begin;
  1433. begin
  1434. inherited Create;
  1435. typ:=ait_cutobject;
  1436. place:=cut_begin;
  1437. end;
  1438. constructor tai_cutobject.Create_end;
  1439. begin
  1440. inherited Create;
  1441. typ:=ait_cutobject;
  1442. place:=cut_end;
  1443. end;
  1444. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1445. begin
  1446. inherited ppuload(t,ppufile);
  1447. place:=TCutPlace(ppufile.getbyte);
  1448. end;
  1449. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1450. begin
  1451. inherited ppuwrite(ppufile);
  1452. ppufile.putbyte(byte(place));
  1453. end;
  1454. {****************************************************************************
  1455. Tai_Marker
  1456. ****************************************************************************}
  1457. constructor Tai_Marker.Create(_Kind: TMarker);
  1458. begin
  1459. Inherited Create;
  1460. typ := ait_marker;
  1461. Kind := _Kind;
  1462. end;
  1463. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1464. begin
  1465. inherited ppuload(t,ppufile);
  1466. kind:=TMarker(ppufile.getbyte);
  1467. end;
  1468. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1469. begin
  1470. inherited ppuwrite(ppufile);
  1471. ppufile.putbyte(byte(kind));
  1472. end;
  1473. {*****************************************************************************
  1474. tai_tempalloc
  1475. *****************************************************************************}
  1476. constructor tai_tempalloc.alloc(pos,size:longint);
  1477. begin
  1478. inherited Create;
  1479. typ:=ait_tempalloc;
  1480. allocation:=true;
  1481. temppos:=pos;
  1482. tempsize:=size;
  1483. {$ifdef EXTDEBUG}
  1484. problem:=nil;
  1485. {$endif EXTDEBUG}
  1486. end;
  1487. destructor tai_tempalloc.destroy;
  1488. begin
  1489. {$ifdef EXTDEBUG}
  1490. stringdispose(problem);
  1491. {$endif EXTDEBUG}
  1492. inherited destroy;
  1493. end;
  1494. constructor tai_tempalloc.dealloc(pos,size:longint);
  1495. begin
  1496. inherited Create;
  1497. typ:=ait_tempalloc;
  1498. allocation:=false;
  1499. temppos:=pos;
  1500. tempsize:=size;
  1501. {$ifdef EXTDEBUG}
  1502. problem:=nil;
  1503. {$endif EXTDEBUG}
  1504. end;
  1505. {$ifdef EXTDEBUG}
  1506. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1507. begin
  1508. inherited Create;
  1509. typ:=ait_tempalloc;
  1510. allocation:=false;
  1511. temppos:=pos;
  1512. tempsize:=size;
  1513. problem:=stringdup(st);
  1514. end;
  1515. {$endif EXTDEBUG}
  1516. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1517. begin
  1518. inherited ppuload(t,ppufile);
  1519. temppos:=ppufile.getlongint;
  1520. tempsize:=ppufile.getlongint;
  1521. allocation:=boolean(ppufile.getbyte);
  1522. {$ifdef EXTDEBUG}
  1523. problem:=nil;
  1524. {$endif EXTDEBUG}
  1525. end;
  1526. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1527. begin
  1528. inherited ppuwrite(ppufile);
  1529. ppufile.putlongint(temppos);
  1530. ppufile.putlongint(tempsize);
  1531. ppufile.putbyte(byte(allocation));
  1532. end;
  1533. {*****************************************************************************
  1534. tai_regalloc
  1535. *****************************************************************************}
  1536. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  1537. begin
  1538. inherited create;
  1539. typ:=ait_regalloc;
  1540. ratype:=ra_alloc;
  1541. reg:=r;
  1542. { ainstr must be an instruction }
  1543. if assigned(ainstr) and
  1544. (ainstr.typ<>ait_instruction) then
  1545. internalerror(200411011);
  1546. instr:=ainstr;
  1547. end;
  1548. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  1549. begin
  1550. inherited create;
  1551. typ:=ait_regalloc;
  1552. ratype:=ra_dealloc;
  1553. reg:=r;
  1554. { ainstr must be an instruction }
  1555. if assigned(ainstr) and
  1556. (ainstr.typ<>ait_instruction) then
  1557. internalerror(200411012);
  1558. instr:=ainstr;
  1559. end;
  1560. constructor tai_regalloc.sync(r : tregister);
  1561. begin
  1562. inherited create;
  1563. typ:=ait_regalloc;
  1564. ratype:=ra_sync;
  1565. reg:=r;
  1566. end;
  1567. constructor tai_regalloc.resize(r : tregister);
  1568. begin
  1569. inherited create;
  1570. typ:=ait_regalloc;
  1571. ratype:=ra_resize;
  1572. reg:=r;
  1573. end;
  1574. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1575. begin
  1576. inherited ppuload(t,ppufile);
  1577. ppufile.getdata(reg,sizeof(Tregister));
  1578. ratype:=tregalloctype(ppufile.getbyte);
  1579. end;
  1580. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1581. begin
  1582. inherited ppuwrite(ppufile);
  1583. ppufile.putdata(reg,sizeof(Tregister));
  1584. ppufile.putbyte(byte(ratype));
  1585. end;
  1586. {****************************************************************************
  1587. tai_file
  1588. ****************************************************************************}
  1589. constructor tai_file.Create(_str : string;_idx : longint);
  1590. begin
  1591. inherited Create;
  1592. typ:=ait_file;
  1593. str:=strpnew(_str);
  1594. idx:=_idx;
  1595. end;
  1596. destructor tai_file.destroy;
  1597. begin
  1598. strdispose(str);
  1599. inherited Destroy;
  1600. end;
  1601. constructor tai_file.ppuload(t:taitype;ppufile:tcompilerppufile);
  1602. var
  1603. len : longint;
  1604. begin
  1605. inherited ppuload(t,ppufile);
  1606. len:=ppufile.getlongint;
  1607. getmem(str,len+1);
  1608. ppufile.getdata(str^,len);
  1609. str[len]:=#0;
  1610. idx:=ppufile.getlongint;
  1611. end;
  1612. procedure tai_file.ppuwrite(ppufile:tcompilerppufile);
  1613. var
  1614. len : longint;
  1615. begin
  1616. inherited ppuwrite(ppufile);
  1617. len:=strlen(str);
  1618. ppufile.putlongint(len);
  1619. ppufile.putdata(str^,len);
  1620. ppufile.putlongint(idx);
  1621. end;
  1622. function tai_file.getcopy : tlinkedlistitem;
  1623. var
  1624. p : tlinkedlistitem;
  1625. begin
  1626. p:=inherited getcopy;
  1627. getmem(tai_comment(p).str,strlen(str)+1);
  1628. move(str^,tai_comment(p).str^,strlen(str)+1);
  1629. getcopy:=p;
  1630. end;
  1631. {****************************************************************************
  1632. tai_loc
  1633. ****************************************************************************}
  1634. constructor tai_loc.Create(_fileidx,_line,_column : longint);
  1635. begin
  1636. inherited Create;
  1637. typ:=ait_loc;
  1638. fileidx:=_fileidx;
  1639. line:=_line;
  1640. column:=_column;
  1641. end;
  1642. constructor tai_loc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1643. begin
  1644. inherited ppuload(t,ppufile);
  1645. fileidx:=ppufile.getlongint;
  1646. line:=ppufile.getlongint;
  1647. column:=ppufile.getlongint;
  1648. end;
  1649. procedure tai_loc.ppuwrite(ppufile:tcompilerppufile);
  1650. begin
  1651. inherited ppuwrite(ppufile);
  1652. ppufile.putlongint(fileidx);
  1653. ppufile.putlongint(line);
  1654. ppufile.putlongint(column);
  1655. end;
  1656. {*****************************************************************************
  1657. TaiInstruction
  1658. *****************************************************************************}
  1659. constructor tai_cpu_abstract.Create(op : tasmop);
  1660. begin
  1661. inherited create;
  1662. typ:=ait_instruction;
  1663. is_jmp:=false;
  1664. opcode:=op;
  1665. ops:=0;
  1666. fillchar(condition,sizeof(condition),0);
  1667. fillchar(oper,sizeof(oper),0);
  1668. end;
  1669. destructor tai_cpu_abstract.Destroy;
  1670. var
  1671. i : integer;
  1672. begin
  1673. for i:=0 to opercnt-1 do
  1674. begin
  1675. clearop(i);
  1676. dispose(oper[i]);
  1677. end;
  1678. inherited destroy;
  1679. end;
  1680. { ---------------------------------------------------------------------
  1681. Loading of operands.
  1682. ---------------------------------------------------------------------}
  1683. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  1684. begin
  1685. while (opers>opercnt) do
  1686. begin
  1687. new(oper[opercnt]);
  1688. fillchar(oper[opercnt]^,sizeof(toper),0);
  1689. inc(opercnt);
  1690. end;
  1691. end;
  1692. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  1693. begin
  1694. allocate_oper(opidx+1);
  1695. with oper[opidx]^ do
  1696. begin
  1697. if typ<>top_const then
  1698. clearop(opidx);
  1699. val:=l;
  1700. typ:=top_const;
  1701. end;
  1702. end;
  1703. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1704. var
  1705. r : treference;
  1706. begin
  1707. reference_reset_symbol(r,s,sofs);
  1708. r.refaddr:=addr_full;
  1709. loadref(opidx,r);
  1710. end;
  1711. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  1712. begin
  1713. if not assigned(s) then
  1714. internalerror(200204251);
  1715. allocate_oper(opidx+1);
  1716. with oper[opidx]^ do
  1717. begin
  1718. if typ<>top_local then
  1719. begin
  1720. clearop(opidx);
  1721. new(localoper);
  1722. end;
  1723. with oper[opidx]^.localoper^ do
  1724. begin
  1725. localsym:=s;
  1726. localsymofs:=sofs;
  1727. localindexreg:=indexreg;
  1728. localscale:=scale;
  1729. localgetoffset:=getoffset;
  1730. localforceref:=forceref;
  1731. end;
  1732. typ:=top_local;
  1733. end;
  1734. end;
  1735. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  1736. begin
  1737. allocate_oper(opidx+1);
  1738. with oper[opidx]^ do
  1739. begin
  1740. if typ<>top_ref then
  1741. begin
  1742. clearop(opidx);
  1743. new(ref);
  1744. end;
  1745. ref^:=r;
  1746. {$ifdef x86}
  1747. { We allow this exception for x86, since overloading this would be
  1748. too much of a a speed penalty}
  1749. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1750. segprefix:=ref^.segment;
  1751. {$endif}
  1752. {$ifdef extdebug}
  1753. if (cs_create_pic in aktmoduleswitches) and
  1754. assigned(r.symbol) and
  1755. (r.refaddr=addr_no) then
  1756. internalerror(200502052);
  1757. {$endif}
  1758. typ:=top_ref;
  1759. if assigned(add_reg_instruction_hook) then
  1760. begin
  1761. add_reg_instruction_hook(self,ref^.base);
  1762. add_reg_instruction_hook(self,ref^.index);
  1763. end;
  1764. { mark symbol as used }
  1765. if assigned(ref^.symbol) then
  1766. ref^.symbol.increfs;
  1767. end;
  1768. end;
  1769. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  1770. begin
  1771. allocate_oper(opidx+1);
  1772. with oper[opidx]^ do
  1773. begin
  1774. if typ<>top_reg then
  1775. clearop(opidx);
  1776. reg:=r;
  1777. typ:=top_reg;
  1778. end;
  1779. if assigned(add_reg_instruction_hook) then
  1780. add_reg_instruction_hook(self,r);
  1781. {$ifdef ARM}
  1782. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1783. Due to speed considerations we don't use a virtual overridden method here.
  1784. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1785. problems with iregs getting r15.
  1786. }
  1787. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1788. {$endif ARM}
  1789. end;
  1790. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  1791. begin
  1792. allocate_oper(opidx+1);
  1793. clearop(opidx);
  1794. oper[opidx]^:=o;
  1795. { copy also the reference }
  1796. with oper[opidx]^ do
  1797. begin
  1798. case typ of
  1799. top_reg:
  1800. begin
  1801. if assigned(add_reg_instruction_hook) then
  1802. add_reg_instruction_hook(self,reg);
  1803. end;
  1804. top_ref:
  1805. begin
  1806. new(ref);
  1807. ref^:=o.ref^;
  1808. if assigned(add_reg_instruction_hook) then
  1809. begin
  1810. add_reg_instruction_hook(self,ref^.base);
  1811. add_reg_instruction_hook(self,ref^.index);
  1812. end;
  1813. end;
  1814. {$ifdef ARM}
  1815. top_shifterop:
  1816. begin
  1817. new(shifterop);
  1818. shifterop^:=o.shifterop^;
  1819. if assigned(add_reg_instruction_hook) then
  1820. add_reg_instruction_hook(self,shifterop^.rs);
  1821. end;
  1822. {$endif ARM}
  1823. end;
  1824. end;
  1825. end;
  1826. procedure tai_cpu_abstract.clearop(opidx:longint);
  1827. begin
  1828. with oper[opidx]^ do
  1829. begin
  1830. case typ of
  1831. top_ref:
  1832. dispose(ref);
  1833. top_local:
  1834. dispose(localoper);
  1835. {$ifdef ARM}
  1836. top_shifterop:
  1837. dispose(shifterop);
  1838. top_regset:
  1839. dispose(regset);
  1840. {$endif ARM}
  1841. end;
  1842. typ:=top_none;
  1843. end;
  1844. end;
  1845. { ---------------------------------------------------------------------
  1846. Miscellaneous methods.
  1847. ---------------------------------------------------------------------}
  1848. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  1849. begin
  1850. condition:=c;
  1851. end;
  1852. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  1853. var
  1854. i : longint;
  1855. p : tai_cpu_abstract;
  1856. begin
  1857. p:=tai_cpu_abstract(inherited getcopy);
  1858. { make a copy of the references }
  1859. p.opercnt:=0;
  1860. p.allocate_oper(ops);
  1861. for i:=0 to ops-1 do
  1862. begin
  1863. p.oper[i]^:=oper[i]^;
  1864. case oper[i]^.typ of
  1865. top_local :
  1866. begin
  1867. new(p.oper[i]^.localoper);
  1868. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  1869. end;
  1870. top_ref :
  1871. begin
  1872. new(p.oper[i]^.ref);
  1873. p.oper[i]^.ref^:=oper[i]^.ref^;
  1874. end;
  1875. {$ifdef ARM}
  1876. top_shifterop:
  1877. begin
  1878. new(p.oper[i]^.shifterop);
  1879. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  1880. end;
  1881. {$endif ARM}
  1882. end;
  1883. end;
  1884. getcopy:=p;
  1885. end;
  1886. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  1887. begin
  1888. { When the generic RA is used this needs to be overriden, we don't use
  1889. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  1890. when tai_cpu is created (PFV) }
  1891. internalerror(200404091);
  1892. result:=false;
  1893. end;
  1894. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  1895. begin
  1896. internalerror(200404091);
  1897. result:=operand_readwrite;
  1898. end;
  1899. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  1900. begin
  1901. result := operand_read;
  1902. end;
  1903. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1904. var
  1905. i : integer;
  1906. begin
  1907. inherited ppuload(t,ppufile);
  1908. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1909. ppufile.getdata(condition,sizeof(tasmcond));
  1910. allocate_oper(ppufile.getbyte);
  1911. for i:=0 to ops-1 do
  1912. ppuloadoper(ppufile,oper[i]^);
  1913. opcode:=tasmop(ppufile.getword);
  1914. {$ifdef x86}
  1915. ppufile.getdata(segprefix,sizeof(Tregister));
  1916. {$endif x86}
  1917. is_jmp:=boolean(ppufile.getbyte);
  1918. end;
  1919. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1920. var
  1921. i : integer;
  1922. begin
  1923. inherited ppuwrite(ppufile);
  1924. ppufile.putdata(condition,sizeof(tasmcond));
  1925. ppufile.putbyte(ops);
  1926. for i:=0 to ops-1 do
  1927. ppuwriteoper(ppufile,oper[i]^);
  1928. ppufile.putword(word(opcode));
  1929. {$ifdef x86}
  1930. ppufile.putdata(segprefix,sizeof(Tregister));
  1931. {$endif x86}
  1932. ppufile.putbyte(byte(is_jmp));
  1933. end;
  1934. procedure tai_cpu_abstract.buildderefimpl;
  1935. var
  1936. i : integer;
  1937. begin
  1938. for i:=0 to ops-1 do
  1939. ppubuildderefimploper(oper[i]^);
  1940. end;
  1941. procedure tai_cpu_abstract.derefimpl;
  1942. var
  1943. i : integer;
  1944. begin
  1945. for i:=0 to ops-1 do
  1946. ppuderefoper(oper[i]^);
  1947. end;
  1948. {****************************************************************************
  1949. tai_align_abstract
  1950. ****************************************************************************}
  1951. constructor tai_align_abstract.Create(b: byte);
  1952. begin
  1953. inherited Create;
  1954. typ:=ait_align;
  1955. if b in [1,2,4,8,16,32] then
  1956. aligntype := b
  1957. else
  1958. aligntype := 1;
  1959. fillsize:=0;
  1960. fillop:=0;
  1961. use_op:=false;
  1962. end;
  1963. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  1964. begin
  1965. inherited Create;
  1966. typ:=ait_align;
  1967. if b in [1,2,4,8,16,32] then
  1968. aligntype := b
  1969. else
  1970. aligntype := 1;
  1971. fillsize:=0;
  1972. fillop:=_op;
  1973. use_op:=true;
  1974. end;
  1975. constructor tai_align_abstract.Create_zeros(b: byte);
  1976. begin
  1977. inherited Create;
  1978. typ:=ait_align;
  1979. if b in [1,2,4,8,16,32] then
  1980. aligntype := b
  1981. else
  1982. aligntype := 1;
  1983. use_op:=true;
  1984. fillsize:=0;
  1985. fillop:=0;
  1986. end;
  1987. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer):pchar;
  1988. begin
  1989. if fillsize>sizeof(buf) then
  1990. internalerror(200404293);
  1991. fillchar(buf,high(buf),fillop);
  1992. calculatefillbuf:=pchar(@buf);
  1993. end;
  1994. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1995. begin
  1996. inherited ppuload(t,ppufile);
  1997. aligntype:=ppufile.getbyte;
  1998. fillsize:=0;
  1999. fillop:=ppufile.getbyte;
  2000. use_op:=boolean(ppufile.getbyte);
  2001. end;
  2002. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2003. begin
  2004. inherited ppuwrite(ppufile);
  2005. ppufile.putbyte(aligntype);
  2006. ppufile.putbyte(fillop);
  2007. ppufile.putbyte(byte(use_op));
  2008. end;
  2009. {*****************************************************************************
  2010. TAAsmOutput
  2011. *****************************************************************************}
  2012. constructor taasmoutput.create;
  2013. begin
  2014. inherited create;
  2015. { make sure the optimizer won't remove the first tai of this list}
  2016. insert(tai_marker.create(marker_blockstart));
  2017. end;
  2018. function taasmoutput.empty : boolean;
  2019. begin
  2020. { there is always a marker_blockstart available,
  2021. see taasmoutput.create }
  2022. result:=(count<=1);
  2023. end;
  2024. function taasmoutput.getlasttaifilepos : pfileposinfo;
  2025. var
  2026. hp : tlinkedlistitem;
  2027. begin
  2028. getlasttaifilepos := nil;
  2029. if assigned(last) then
  2030. begin
  2031. { find the last file information record }
  2032. if not (tai(last).typ in SkipLineInfo) then
  2033. getlasttaifilepos:=@tailineinfo(last).fileinfo
  2034. else
  2035. { go through list backwards to find the first entry
  2036. with line information
  2037. }
  2038. begin
  2039. hp:=tai(last);
  2040. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  2041. hp:=hp.Previous;
  2042. { found entry }
  2043. if assigned(hp) then
  2044. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  2045. end;
  2046. end;
  2047. end;
  2048. procedure Taasmoutput.InsertAfter(Item,Loc : TLinkedListItem);
  2049. begin
  2050. { This is not possible because it is not sure that the
  2051. tai at Loc has taifileinfo as parent }
  2052. {if assigned(Loc) then
  2053. tailineinfo(Item).fileinfo:=tailineinfo(Loc).fileinfo;}
  2054. inherited InsertAfter(Item,Loc);
  2055. end;
  2056. begin
  2057. cai_cpu:=tai_cpu_abstract;
  2058. cai_align:=tai_align_abstract;
  2059. end.