aasmtai.pas 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513
  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,ogbase;
  32. type
  33. { keep the number of elements in this enumeration less or equal than 32 as long
  34. as FPC knows only 4 byte and 32 byte sets (FK) }
  35. taitype = (
  36. ait_none,
  37. ait_align,
  38. ait_section,
  39. ait_comment,
  40. ait_string,
  41. ait_instruction,
  42. ait_datablock,
  43. ait_symbol,
  44. { needed to calc the size of a symbol }
  45. ait_symbol_end,
  46. ait_directive,
  47. ait_label,
  48. ait_const,
  49. ait_real_32bit,
  50. ait_real_64bit,
  51. ait_real_80bit,
  52. ait_comp_64bit,
  53. ait_real_128bit,
  54. ait_stab,
  55. ait_force_line,
  56. ait_function_name,
  57. {$ifdef alpha}
  58. { the follow is for the DEC Alpha }
  59. ait_frame,
  60. ait_ent,
  61. {$endif alpha}
  62. {$ifdef ia64}
  63. ait_bundle,
  64. ait_stop,
  65. {$endif ia64}
  66. {$ifdef m68k}
  67. ait_labeled_instruction,
  68. {$endif m68k}
  69. { 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, asd_mod_init_func, asd_mod_term_func);
  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. labsym : tasmlabel;
  288. constructor Create(_labsym : 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 : TObjSection; { 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_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  343. constructor Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  344. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  345. constructor Create_rva_sym(_sym:tasmsymbol);
  346. constructor Create_indirect_sym(_sym:tasmsymbol);
  347. constructor Createname(const name:string;_symtyp:Tasmsymtype;ofs:aint);
  348. constructor Createname_rva(const name:string);
  349. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  350. procedure ppuwrite(ppufile:tcompilerppufile);override;
  351. procedure derefimpl;override;
  352. function getcopy:tlinkedlistitem;override;
  353. function size:longint;
  354. end;
  355. { Generates a single float (32 bit real) }
  356. tai_real_32bit = class(tai)
  357. value : ts32real;
  358. constructor Create(_value : ts32real);
  359. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  360. procedure ppuwrite(ppufile:tcompilerppufile);override;
  361. end;
  362. tformatoptions = (fo_none,fo_hiloswapped);
  363. { Generates a double float (64 bit real) }
  364. tai_real_64bit = class(tai)
  365. value : ts64real;
  366. {$ifdef ARM}
  367. formatoptions : tformatoptions;
  368. constructor Create_hiloswapped(_value : ts64real);
  369. {$endif ARM}
  370. constructor Create(_value : ts64real);
  371. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  372. procedure ppuwrite(ppufile:tcompilerppufile);override;
  373. end;
  374. { Generates an extended float (80 bit real) }
  375. tai_real_80bit = class(tai)
  376. value : ts80real;
  377. constructor Create(_value : ts80real);
  378. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  379. procedure ppuwrite(ppufile:tcompilerppufile);override;
  380. end;
  381. { Generates an float128 (128 bit real) }
  382. tai_real_128bit = class(tai)
  383. value : ts128real;
  384. constructor Create(_value : ts128real);
  385. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  386. procedure ppuwrite(ppufile:tcompilerppufile);override;
  387. end;
  388. { Generates a comp int (integer over 64 bits)
  389. This is Intel 80x86 specific, and is not
  390. really supported on other processors.
  391. }
  392. tai_comp_64bit = class(tai)
  393. value : ts64comp;
  394. constructor Create(_value : ts64comp);
  395. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  396. procedure ppuwrite(ppufile:tcompilerppufile);override;
  397. end;
  398. tstabtype = (stab_stabs,stab_stabn,stab_stabd);
  399. tai_stab = class(tai)
  400. str : pchar;
  401. stabtype : tstabtype;
  402. constructor Create(_stabtype:tstabtype;_str : pchar);
  403. constructor Create_str(_stabtype:tstabtype;const s:string);
  404. destructor Destroy;override;
  405. end;
  406. tai_force_line = class(tailineinfo)
  407. constructor Create;
  408. end;
  409. tai_function_name = class(tai)
  410. funcname : pstring;
  411. constructor create(const s:string);
  412. destructor destroy;override;
  413. end;
  414. { Insert a cut to split assembler into several smaller files }
  415. tai_cutobject = class(tai)
  416. place : tcutplace;
  417. constructor Create;
  418. constructor Create_begin;
  419. constructor Create_end;
  420. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  421. procedure ppuwrite(ppufile:tcompilerppufile);override;
  422. end;
  423. { Insert a marker for assembler and inline blocks }
  424. tai_marker = class(tai)
  425. Kind: TMarker;
  426. Constructor Create(_Kind: TMarker);
  427. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  428. procedure ppuwrite(ppufile:tcompilerppufile);override;
  429. end;
  430. tai_tempalloc = class(tai)
  431. allocation : boolean;
  432. {$ifdef EXTDEBUG}
  433. problem : pstring;
  434. {$endif EXTDEBUG}
  435. temppos,
  436. tempsize : longint;
  437. constructor alloc(pos,size:longint);
  438. constructor dealloc(pos,size:longint);
  439. {$ifdef EXTDEBUG}
  440. constructor allocinfo(pos,size:longint;const st:string);
  441. {$endif EXTDEBUG}
  442. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  443. destructor destroy;override;
  444. procedure ppuwrite(ppufile:tcompilerppufile);override;
  445. end;
  446. tai_regalloc = class(tai)
  447. reg : tregister;
  448. ratype : TRegAllocType;
  449. { reg(de)alloc belongs to this instruction, this
  450. is only used for automatic inserted (de)alloc for
  451. imaginary register and required for spilling code }
  452. instr : tai;
  453. constructor alloc(r : tregister;ainstr:tai);
  454. constructor dealloc(r : tregister;ainstr:tai);
  455. constructor sync(r : tregister);
  456. constructor resize(r : tregister);
  457. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  458. procedure ppuwrite(ppufile:tcompilerppufile);override;
  459. end;
  460. { Generates a dwarf file location }
  461. tai_file = class(tai)
  462. str : pchar;
  463. idx : longint;
  464. constructor Create(_str : string);
  465. destructor Destroy; override;
  466. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  467. procedure ppuwrite(ppufile:tcompilerppufile);override;
  468. function getcopy:tlinkedlistitem;override;
  469. end;
  470. { Generates a dwarf line location }
  471. tai_loc = class(tai)
  472. fileentry : tai_file;
  473. line,
  474. column : longint;
  475. constructor Create(_fileidx : tai_file;_line,_column : longint);
  476. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  477. procedure ppuwrite(ppufile:tcompilerppufile);override;
  478. end;
  479. Taasmoutput=class;
  480. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  481. Trggetproc=procedure(list:Taasmoutput;position:Tai;subreg:Tsubregister;var result:Tregister) of object;
  482. Trgungetproc=procedure(list:Taasmoutput;position:Tai;r:Tregister) of object;
  483. { Class template for assembler instructions
  484. }
  485. tai_cpu_abstract = class(tailineinfo)
  486. protected
  487. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;abstract;
  488. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;abstract;
  489. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  490. procedure ppuderefoper(var o:toper);virtual;abstract;
  491. public
  492. { Condition flags for instruction }
  493. condition : TAsmCond;
  494. { Number of operands to instruction }
  495. ops : byte;
  496. { Number of allocate oper structures }
  497. opercnt : byte;
  498. { Operands of instruction }
  499. oper : array[0..max_operands-1] of poper;
  500. { Actual opcode of instruction }
  501. opcode : tasmop;
  502. {$ifdef x86}
  503. segprefix : tregister;
  504. {$endif x86}
  505. { true if instruction is a jmp }
  506. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  507. Constructor Create(op : tasmop);virtual;
  508. Destructor Destroy;override;
  509. function getcopy:TLinkedListItem;override;
  510. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  511. procedure ppuwrite(ppufile:tcompilerppufile);override;
  512. procedure buildderefimpl;override;
  513. procedure derefimpl;override;
  514. procedure SetCondition(const c:TAsmCond);
  515. procedure allocate_oper(opers:longint);
  516. procedure loadconst(opidx:longint;l:aint);
  517. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  518. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  519. procedure loadref(opidx:longint;const r:treference);
  520. procedure loadreg(opidx:longint;r:tregister);
  521. procedure loadoper(opidx:longint;o:toper);
  522. procedure clearop(opidx:longint);
  523. { register allocator }
  524. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  525. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  526. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  527. function Pass1(objdata:TObjData):longint;virtual;abstract;
  528. procedure Pass2(objdata:TObjData);virtual;abstract;
  529. procedure resetpass1; virtual;
  530. procedure resetpass2; virtual;
  531. end;
  532. tai_cpu_class = class of tai_cpu_abstract;
  533. { alignment for operator }
  534. tai_align_abstract = class(tai)
  535. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  536. fillsize : byte; { real size to fill }
  537. fillop : byte; { value to fill with - optional }
  538. use_op : boolean;
  539. constructor Create(b:byte);virtual;
  540. constructor Create_op(b: byte; _op: byte);virtual;
  541. constructor Create_zeros(b:byte);
  542. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  543. procedure ppuwrite(ppufile:tcompilerppufile);override;
  544. function calculatefillbuf(var buf : tfillbuffer):pchar;virtual;
  545. end;
  546. tai_align_class = class of tai_align_abstract;
  547. taasmoutput = class(tlinkedlist)
  548. constructor create;
  549. function empty : boolean;
  550. function getlasttaifilepos : pfileposinfo;
  551. procedure InsertAfter(Item,Loc : TLinkedListItem);override;
  552. end;
  553. { Type of asmlists. The order is important for the layout of the
  554. information in the .o file. The stabs for the types must be defined
  555. before they can be referenced and therefor they need to be written
  556. first (PFV) }
  557. Tasmlist=(al_start,
  558. al_stabs,
  559. al_procedures,
  560. al_globals,
  561. al_const,
  562. al_typedconsts,
  563. al_rotypedconsts,
  564. al_threadvars,
  565. al_imports,
  566. al_exports,
  567. al_resources,
  568. al_rtti,
  569. al_dwarf,
  570. al_dwarf_info,
  571. al_dwarf_abbrev,
  572. al_dwarf_line,
  573. al_picdata,
  574. al_resourcestrings,
  575. al_end);
  576. const
  577. TasmlistStr : array[tasmlist] of string[24] =(
  578. 'al_begin',
  579. 'al_stabs',
  580. 'al_procedures',
  581. 'al_globals',
  582. 'al_const',
  583. 'al_typedconsts',
  584. 'al_rotypedconsts',
  585. 'al_threadvars',
  586. 'al_imports',
  587. 'al_exports',
  588. 'al_resources',
  589. 'al_rtti',
  590. 'al_dwarf',
  591. 'al_dwarf_info',
  592. 'al_dwarf_abbrev',
  593. 'al_dwarf_line',
  594. 'al_picdata',
  595. 'al_resourcestrings',
  596. 'al_end');
  597. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized');
  598. tempallocstr : array[boolean] of string[10]=('released','allocated');
  599. stabtypestr : array[tstabtype] of string[5]=('stabs','stabn','stabd');
  600. directivestr : array[tasmdirective] of string[23]=(
  601. 'non_lazy_symbol_pointer','indirect_symbol','lazy_symbol_pointer',
  602. 'extern','nasm_import', 'tc', 'mod_init_func', 'mod_term_func'
  603. );
  604. var
  605. { array with all class types for tais }
  606. aiclass : taiclassarray;
  607. { Current expression list }
  608. exprasmlist : taasmoutput;
  609. { labels for BREAK and CONTINUE }
  610. aktbreaklabel,aktcontinuelabel : tasmlabel;
  611. { label when the result is true or false }
  612. truelabel,falselabel : tasmlabel;
  613. { hook to notify uses of registers }
  614. add_reg_instruction_hook : tadd_reg_instruction_proc;
  615. asmlist:array[Tasmlist] of Taasmoutput;
  616. cai_align : tai_align_class;
  617. cai_cpu : tai_cpu_class;
  618. function use_smartlink_section:boolean;
  619. function maybe_smartlink_symbol:boolean;
  620. procedure maybe_new_object_file(list:taasmoutput);
  621. procedure new_section(list:taasmoutput;Asectype:TAsmSectiontype;Aname:string;Aalign:byte);
  622. procedure section_symbol_start(list:taasmoutput;const Aname:string;Asymtyp:Tasmsymtype;
  623. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  624. procedure section_symbol_end(list:taasmoutput;const Aname:string);
  625. function ppuloadai(ppufile:tcompilerppufile):tai;
  626. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  627. implementation
  628. uses
  629. strings,
  630. verbose;
  631. const
  632. pputaimarker = 254;
  633. {****************************************************************************
  634. Helpers
  635. ****************************************************************************}
  636. function ppuloadai(ppufile:tcompilerppufile):tai;
  637. var
  638. b : byte;
  639. t : taitype;
  640. begin
  641. { marker }
  642. b:=ppufile.getbyte;
  643. if b<>pputaimarker then
  644. internalerror(200208181);
  645. { load nodetype }
  646. t:=taitype(ppufile.getbyte);
  647. if t<>ait_none then
  648. begin
  649. if t>high(taitype) then
  650. internalerror(200208182);
  651. if not assigned(aiclass[t]) then
  652. internalerror(200208183);
  653. {writeln('taiload: ',taitypestr[t]);}
  654. { generate tai of the correct class }
  655. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  656. end
  657. else
  658. ppuloadai:=nil;
  659. end;
  660. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  661. begin
  662. { marker, read by ppuloadnode }
  663. ppufile.putbyte(pputaimarker);
  664. if assigned(n) then
  665. begin
  666. { type, read by ppuloadnode }
  667. ppufile.putbyte(byte(n.typ));
  668. {writeln('taiwrite: ',taitypestr[n.typ]);}
  669. n.ppuwrite(ppufile);
  670. end
  671. else
  672. ppufile.putbyte(byte(ait_none));
  673. end;
  674. function use_smartlink_section:boolean;
  675. begin
  676. result:=(af_smartlink_sections in target_asm.flags) and
  677. (tf_smartlink_sections in target_info.flags);
  678. end;
  679. function maybe_smartlink_symbol:boolean;
  680. begin
  681. result:=(cs_create_smart in aktmoduleswitches) or
  682. use_smartlink_section;
  683. end;
  684. procedure maybe_new_object_file(list:taasmoutput);
  685. begin
  686. if (cs_create_smart in aktmoduleswitches) and
  687. (not use_smartlink_section) then
  688. list.concat(tai_cutobject.create);
  689. end;
  690. procedure new_section(list:taasmoutput;Asectype:TAsmSectiontype;Aname:string;Aalign:byte);
  691. begin
  692. list.concat(tai_section.create(Asectype,Aname,Aalign));
  693. list.concat(cai_align.create(Aalign));
  694. end;
  695. procedure section_symbol_start(list:taasmoutput;const Aname:string;Asymtyp:Tasmsymtype;
  696. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  697. begin
  698. maybe_new_object_file(list);
  699. list.concat(tai_section.create(Asectype,Aname,Aalign));
  700. list.concat(cai_align.create(Aalign));
  701. if Aglobal or
  702. maybe_smartlink_symbol then
  703. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  704. else
  705. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  706. end;
  707. procedure section_symbol_end(list:taasmoutput;const Aname:string);
  708. begin
  709. list.concat(tai_symbol_end.createname(Aname));
  710. end;
  711. {****************************************************************************
  712. TAI
  713. ****************************************************************************}
  714. constructor tai.Create;
  715. begin
  716. {$ifndef NOOPT}
  717. optinfo:=nil;
  718. {$endif NOOPT}
  719. end;
  720. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  721. begin
  722. typ:=t;
  723. {$ifndef NOOPT}
  724. optinfo:=nil;
  725. {$endif}
  726. end;
  727. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  728. begin
  729. end;
  730. procedure tai.buildderefimpl;
  731. begin
  732. end;
  733. procedure tai.derefimpl;
  734. begin
  735. end;
  736. {****************************************************************************
  737. TAILINEINFO
  738. ****************************************************************************}
  739. constructor tailineinfo.create;
  740. begin
  741. inherited create;
  742. if not(inlining_procedure and
  743. (cs_gdb_valgrind in aktglobalswitches)) then
  744. fileinfo:=aktfilepos;
  745. end;
  746. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  747. begin
  748. inherited ppuload(t,ppufile);
  749. ppufile.getposinfo(fileinfo);
  750. end;
  751. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  752. begin
  753. inherited ppuwrite(ppufile);
  754. ppufile.putposinfo(fileinfo);
  755. end;
  756. {****************************************************************************
  757. TAI_SIMPLE
  758. ****************************************************************************}
  759. constructor tai_simple.create(_typ : taitype);
  760. begin
  761. inherited create;
  762. typ:=_typ;
  763. end;
  764. {****************************************************************************
  765. TAI_SECTION
  766. ****************************************************************************}
  767. constructor tai_section.Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte);
  768. begin
  769. inherited Create;
  770. typ:=ait_section;
  771. sectype:=asectype;
  772. secalign:=Aalign;
  773. name:=stringdup(Aname);
  774. sec:=nil;
  775. end;
  776. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  777. begin
  778. inherited ppuload(t,ppufile);
  779. sectype:=TAsmSectiontype(ppufile.getbyte);
  780. secalign:=ppufile.getbyte;
  781. name:=stringdup(ppufile.getstring);
  782. sec:=nil;
  783. end;
  784. destructor tai_section.Destroy;
  785. begin
  786. stringdispose(name);
  787. end;
  788. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  789. begin
  790. inherited ppuwrite(ppufile);
  791. ppufile.putbyte(byte(sectype));
  792. ppufile.putbyte(secalign);
  793. ppufile.putstring(name^);
  794. end;
  795. {****************************************************************************
  796. TAI_DATABLOCK
  797. ****************************************************************************}
  798. constructor tai_datablock.Create(const _name : string;_size : longint);
  799. begin
  800. inherited Create;
  801. typ:=ait_datablock;
  802. sym:=objectlibrary.newasmsymbol(_name,AB_LOCAL,AT_DATA);
  803. { keep things aligned }
  804. if _size<=0 then
  805. _size:=4;
  806. size:=_size;
  807. is_global:=false;
  808. end;
  809. constructor tai_datablock.Create_global(const _name : string;_size : longint);
  810. begin
  811. inherited Create;
  812. typ:=ait_datablock;
  813. sym:=objectlibrary.newasmsymbol(_name,AB_GLOBAL,AT_DATA);
  814. { keep things aligned }
  815. if _size<=0 then
  816. _size:=4;
  817. size:=_size;
  818. is_global:=true;
  819. end;
  820. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  821. begin
  822. inherited Create;
  823. sym:=ppufile.getasmsymbol;
  824. size:=ppufile.getlongint;
  825. is_global:=boolean(ppufile.getbyte);
  826. end;
  827. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  828. begin
  829. inherited ppuwrite(ppufile);
  830. ppufile.putasmsymbol(sym);
  831. ppufile.putlongint(size);
  832. ppufile.putbyte(byte(is_global));
  833. end;
  834. procedure tai_datablock.derefimpl;
  835. begin
  836. end;
  837. {****************************************************************************
  838. TAI_SYMBOL
  839. ****************************************************************************}
  840. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  841. begin
  842. inherited Create;
  843. typ:=ait_symbol;
  844. sym:=_sym;
  845. size:=siz;
  846. sym.bind:=AB_LOCAL;
  847. is_global:=false;
  848. end;
  849. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  850. begin
  851. inherited Create;
  852. typ:=ait_symbol;
  853. sym:=_sym;
  854. size:=siz;
  855. sym.bind:=AB_GLOBAL;
  856. is_global:=true;
  857. end;
  858. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  859. begin
  860. inherited Create;
  861. typ:=ait_symbol;
  862. sym:=objectlibrary.newasmsymbol(_name,AB_LOCAL,_symtyp);
  863. size:=siz;
  864. is_global:=false;
  865. end;
  866. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  867. begin
  868. inherited Create;
  869. typ:=ait_symbol;
  870. sym:=objectlibrary.newasmsymbol(_name,AB_GLOBAL,_symtyp);
  871. size:=siz;
  872. is_global:=true;
  873. end;
  874. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  875. begin
  876. inherited ppuload(t,ppufile);
  877. sym:=ppufile.getasmsymbol;
  878. size:=ppufile.getlongint;
  879. is_global:=boolean(ppufile.getbyte);
  880. end;
  881. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  882. begin
  883. inherited ppuwrite(ppufile);
  884. ppufile.putasmsymbol(sym);
  885. ppufile.putlongint(size);
  886. ppufile.putbyte(byte(is_global));
  887. end;
  888. procedure tai_symbol.derefimpl;
  889. begin
  890. end;
  891. {****************************************************************************
  892. TAI_SYMBOL_END
  893. ****************************************************************************}
  894. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  895. begin
  896. inherited Create;
  897. typ:=ait_symbol_end;
  898. sym:=_sym;
  899. end;
  900. constructor tai_symbol_end.Createname(const _name : string);
  901. begin
  902. inherited Create;
  903. typ:=ait_symbol_end;
  904. sym:=objectlibrary.newasmsymbol(_name,AB_GLOBAL,AT_NONE);
  905. end;
  906. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  907. begin
  908. inherited ppuload(t,ppufile);
  909. sym:=ppufile.getasmsymbol;
  910. end;
  911. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  912. begin
  913. inherited ppuwrite(ppufile);
  914. ppufile.putasmsymbol(sym);
  915. end;
  916. procedure tai_symbol_end.derefimpl;
  917. begin
  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_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  1030. begin
  1031. inherited Create;
  1032. typ:=ait_const;
  1033. consttype:=_typ;
  1034. sym:=_sym;
  1035. endsym:=nil;
  1036. value:=0;
  1037. { update sym info }
  1038. if assigned(sym) then
  1039. sym.increfs;
  1040. end;
  1041. constructor tai_const.Create_sym(_sym:tasmsymbol);
  1042. begin
  1043. inherited Create;
  1044. typ:=ait_const;
  1045. consttype:=aitconst_ptr;
  1046. { sym is allowed to be nil, this is used to write nil pointers }
  1047. sym:=_sym;
  1048. endsym:=nil;
  1049. value:=0;
  1050. { update sym info }
  1051. if assigned(sym) then
  1052. sym.increfs;
  1053. end;
  1054. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  1055. begin
  1056. inherited Create;
  1057. typ:=ait_const;
  1058. consttype:=aitconst_ptr;
  1059. if not assigned(_sym) then
  1060. internalerror(200404121);
  1061. sym:=_sym;
  1062. endsym:=nil;
  1063. value:=ofs;
  1064. { update sym info }
  1065. sym.increfs;
  1066. end;
  1067. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1068. begin
  1069. inherited Create;
  1070. typ:=ait_const;
  1071. consttype:=_typ;
  1072. sym:=_sym;
  1073. endsym:=_endsym;
  1074. value:=0;
  1075. { update sym info }
  1076. sym.increfs;
  1077. endsym.increfs;
  1078. end;
  1079. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1080. begin
  1081. inherited Create;
  1082. typ:=ait_const;
  1083. consttype:=aitconst_rva_symbol;
  1084. sym:=_sym;
  1085. endsym:=nil;
  1086. value:=0;
  1087. { update sym info }
  1088. sym.increfs;
  1089. end;
  1090. constructor tai_const.Create_indirect_sym(_sym:tasmsymbol);
  1091. begin
  1092. inherited Create;
  1093. typ:=ait_const;
  1094. consttype:=aitconst_indirect_symbol;
  1095. sym:=_sym;
  1096. endsym:=nil;
  1097. value:=0;
  1098. { update sym info }
  1099. sym.increfs;
  1100. end;
  1101. constructor tai_const.Createname(const name:string;_symtyp:Tasmsymtype;ofs:aint);
  1102. begin
  1103. inherited Create;
  1104. typ:=ait_const;
  1105. consttype:=aitconst_ptr;
  1106. sym:=objectlibrary.newasmsymbol(name,AB_EXTERNAL,_symtyp);
  1107. endsym:=nil;
  1108. value:=ofs;
  1109. { update sym info }
  1110. sym.increfs;
  1111. end;
  1112. constructor tai_const.Createname_rva(const name:string);
  1113. begin
  1114. inherited Create;
  1115. typ:=ait_const;
  1116. consttype:=aitconst_rva_symbol;
  1117. sym:=objectlibrary.newasmsymbol(name,AB_EXTERNAL,AT_FUNCTION);
  1118. endsym:=nil;
  1119. value:=0;
  1120. { update sym info }
  1121. sym.increfs;
  1122. end;
  1123. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1124. begin
  1125. inherited ppuload(t,ppufile);
  1126. consttype:=taiconst_type(ppufile.getbyte);
  1127. sym:=ppufile.getasmsymbol;
  1128. endsym:=ppufile.getasmsymbol;
  1129. value:=ppufile.getint64;
  1130. end;
  1131. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1132. begin
  1133. inherited ppuwrite(ppufile);
  1134. ppufile.putbyte(byte(consttype));
  1135. ppufile.putasmsymbol(sym);
  1136. ppufile.putasmsymbol(endsym);
  1137. ppufile.putint64(value);
  1138. end;
  1139. procedure tai_const.derefimpl;
  1140. begin
  1141. end;
  1142. function tai_const.getcopy:tlinkedlistitem;
  1143. begin
  1144. getcopy:=inherited getcopy;
  1145. { we need to increase the reference number }
  1146. sym.increfs;
  1147. if assigned(endsym) then
  1148. endsym.increfs;
  1149. end;
  1150. function tai_const.size:longint;
  1151. begin
  1152. case consttype of
  1153. aitconst_8bit :
  1154. result:=1;
  1155. aitconst_16bit :
  1156. result:=2;
  1157. aitconst_32bit :
  1158. result:=4;
  1159. aitconst_64bit :
  1160. result:=8;
  1161. aitconst_indirect_symbol,
  1162. aitconst_rva_symbol :
  1163. result:=sizeof(aint);
  1164. end;
  1165. end;
  1166. {****************************************************************************
  1167. TAI_real_32bit
  1168. ****************************************************************************}
  1169. constructor tai_real_32bit.Create(_value : ts32real);
  1170. begin
  1171. inherited Create;
  1172. typ:=ait_real_32bit;
  1173. value:=_value;
  1174. end;
  1175. constructor tai_real_32bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1176. begin
  1177. inherited ppuload(t,ppufile);
  1178. value:=ppufile.getreal;
  1179. end;
  1180. procedure tai_real_32bit.ppuwrite(ppufile:tcompilerppufile);
  1181. begin
  1182. inherited ppuwrite(ppufile);
  1183. ppufile.putreal(value);
  1184. end;
  1185. {****************************************************************************
  1186. TAI_real_64bit
  1187. ****************************************************************************}
  1188. constructor tai_real_64bit.Create(_value : ts64real);
  1189. begin
  1190. inherited Create;
  1191. typ:=ait_real_64bit;
  1192. value:=_value;
  1193. end;
  1194. {$ifdef ARM}
  1195. constructor tai_real_64bit.Create_hiloswapped(_value : ts64real);
  1196. begin
  1197. inherited Create;
  1198. typ:=ait_real_64bit;
  1199. value:=_value;
  1200. formatoptions:=fo_hiloswapped;
  1201. end;
  1202. {$endif ARM}
  1203. constructor tai_real_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1204. begin
  1205. inherited ppuload(t,ppufile);
  1206. value:=ppufile.getreal;
  1207. {$ifdef ARM}
  1208. formatoptions:=tformatoptions(ppufile.getbyte);
  1209. {$endif ARM}
  1210. end;
  1211. procedure tai_real_64bit.ppuwrite(ppufile:tcompilerppufile);
  1212. begin
  1213. inherited ppuwrite(ppufile);
  1214. ppufile.putreal(value);
  1215. {$ifdef ARM}
  1216. ppufile.putbyte(byte(formatoptions));
  1217. {$endif ARM}
  1218. end;
  1219. {****************************************************************************
  1220. TAI_real_80bit
  1221. ****************************************************************************}
  1222. constructor tai_real_80bit.Create(_value : ts80real);
  1223. begin
  1224. inherited Create;
  1225. typ:=ait_real_80bit;
  1226. value:=_value;
  1227. end;
  1228. constructor tai_real_80bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1229. begin
  1230. inherited ppuload(t,ppufile);
  1231. value:=ppufile.getreal;
  1232. end;
  1233. procedure tai_real_80bit.ppuwrite(ppufile:tcompilerppufile);
  1234. begin
  1235. inherited ppuwrite(ppufile);
  1236. ppufile.putreal(value);
  1237. end;
  1238. {****************************************************************************
  1239. TAI_real_80bit
  1240. ****************************************************************************}
  1241. constructor tai_real_128bit.Create(_value : ts128real);
  1242. begin
  1243. inherited Create;
  1244. typ:=ait_real_128bit;
  1245. value:=_value;
  1246. end;
  1247. constructor tai_real_128bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1248. begin
  1249. inherited ppuload(t,ppufile);
  1250. value:=ppufile.getreal;
  1251. end;
  1252. procedure tai_real_128bit.ppuwrite(ppufile:tcompilerppufile);
  1253. begin
  1254. inherited ppuwrite(ppufile);
  1255. ppufile.putreal(value);
  1256. end;
  1257. {****************************************************************************
  1258. Tai_comp_64bit
  1259. ****************************************************************************}
  1260. constructor tai_comp_64bit.Create(_value : ts64comp);
  1261. begin
  1262. inherited Create;
  1263. typ:=ait_comp_64bit;
  1264. value:=_value;
  1265. end;
  1266. constructor tai_comp_64bit.ppuload(t:taitype;ppufile:tcompilerppufile);
  1267. begin
  1268. inherited ppuload(t,ppufile);
  1269. ppufile.putdata(value,sizeof(value));
  1270. end;
  1271. procedure tai_comp_64bit.ppuwrite(ppufile:tcompilerppufile);
  1272. begin
  1273. inherited ppuwrite(ppufile);
  1274. ppufile.getdata(value,sizeof(value));
  1275. end;
  1276. {****************************************************************************
  1277. TAI_STRING
  1278. ****************************************************************************}
  1279. constructor tai_string.Create(const _str : string);
  1280. begin
  1281. inherited Create;
  1282. typ:=ait_string;
  1283. len:=length(_str);
  1284. getmem(str,len+1);
  1285. strpcopy(str,_str);
  1286. end;
  1287. constructor tai_string.Create_pchar(_str : pchar;length : longint);
  1288. begin
  1289. inherited Create;
  1290. typ:=ait_string;
  1291. str:=_str;
  1292. len:=length;
  1293. end;
  1294. destructor tai_string.destroy;
  1295. begin
  1296. if str<>nil then
  1297. freemem(str);
  1298. inherited Destroy;
  1299. end;
  1300. constructor tai_string.ppuload(t:taitype;ppufile:tcompilerppufile);
  1301. begin
  1302. inherited ppuload(t,ppufile);
  1303. len:=ppufile.getlongint;
  1304. getmem(str,len);
  1305. ppufile.getdata(str^,len);
  1306. end;
  1307. procedure tai_string.ppuwrite(ppufile:tcompilerppufile);
  1308. begin
  1309. inherited ppuwrite(ppufile);
  1310. ppufile.putlongint(len);
  1311. ppufile.putdata(str^,len);
  1312. end;
  1313. function tai_string.getcopy : tlinkedlistitem;
  1314. var
  1315. p : tlinkedlistitem;
  1316. begin
  1317. p:=inherited getcopy;
  1318. getmem(tai_string(p).str,len);
  1319. move(str^,tai_string(p).str^,len);
  1320. getcopy:=p;
  1321. end;
  1322. {****************************************************************************
  1323. TAI_LABEL
  1324. ****************************************************************************}
  1325. constructor tai_label.create(_labsym : tasmlabel);
  1326. begin
  1327. inherited Create;
  1328. typ:=ait_label;
  1329. labsym:=_labsym;
  1330. labsym.is_set:=true;
  1331. is_global:=(labsym.bind=AB_GLOBAL);
  1332. end;
  1333. constructor tai_label.ppuload(t:taitype;ppufile:tcompilerppufile);
  1334. begin
  1335. inherited ppuload(t,ppufile);
  1336. labsym:=tasmlabel(ppufile.getasmsymbol);
  1337. is_global:=boolean(ppufile.getbyte);
  1338. end;
  1339. procedure tai_label.ppuwrite(ppufile:tcompilerppufile);
  1340. begin
  1341. inherited ppuwrite(ppufile);
  1342. ppufile.putasmsymbol(labsym);
  1343. ppufile.putbyte(byte(is_global));
  1344. end;
  1345. procedure tai_label.derefimpl;
  1346. begin
  1347. labsym.is_set:=true;
  1348. end;
  1349. {****************************************************************************
  1350. tai_comment comment to be inserted in the assembler file
  1351. ****************************************************************************}
  1352. constructor tai_comment.Create(_str : pchar);
  1353. begin
  1354. inherited Create;
  1355. typ:=ait_comment;
  1356. str:=_str;
  1357. end;
  1358. destructor tai_comment.destroy;
  1359. begin
  1360. strdispose(str);
  1361. inherited Destroy;
  1362. end;
  1363. constructor tai_comment.ppuload(t:taitype;ppufile:tcompilerppufile);
  1364. var
  1365. len : longint;
  1366. begin
  1367. inherited ppuload(t,ppufile);
  1368. len:=ppufile.getlongint;
  1369. getmem(str,len+1);
  1370. ppufile.getdata(str^,len);
  1371. str[len]:=#0;
  1372. end;
  1373. procedure tai_comment.ppuwrite(ppufile:tcompilerppufile);
  1374. var
  1375. len : longint;
  1376. begin
  1377. inherited ppuwrite(ppufile);
  1378. len:=strlen(str);
  1379. ppufile.putlongint(len);
  1380. ppufile.putdata(str^,len);
  1381. end;
  1382. function tai_comment.getcopy : tlinkedlistitem;
  1383. var
  1384. p : tlinkedlistitem;
  1385. begin
  1386. p:=inherited getcopy;
  1387. getmem(tai_comment(p).str,strlen(str)+1);
  1388. move(str^,tai_comment(p).str^,strlen(str)+1);
  1389. getcopy:=p;
  1390. end;
  1391. {****************************************************************************
  1392. TAI_STABS
  1393. ****************************************************************************}
  1394. constructor tai_stab.create(_stabtype:tstabtype;_str : pchar);
  1395. begin
  1396. inherited create;
  1397. typ:=ait_stab;
  1398. str:=_str;
  1399. stabtype:=_stabtype;
  1400. end;
  1401. constructor tai_stab.create_str(_stabtype:tstabtype;const s:string);
  1402. begin
  1403. self.create(_stabtype,strpnew(s));
  1404. end;
  1405. destructor tai_stab.destroy;
  1406. begin
  1407. strdispose(str);
  1408. inherited destroy;
  1409. end;
  1410. {****************************************************************************
  1411. TAI_FORCE_LINE
  1412. ****************************************************************************}
  1413. constructor tai_force_line.create;
  1414. begin
  1415. inherited create;
  1416. typ:=ait_force_line;
  1417. end;
  1418. {****************************************************************************
  1419. TAI_FUNCTION_NAME
  1420. ****************************************************************************}
  1421. constructor tai_function_name.create(const s:string);
  1422. begin
  1423. inherited create;
  1424. typ:=ait_function_name;
  1425. funcname:=stringdup(s);
  1426. end;
  1427. destructor tai_function_name.destroy;
  1428. begin
  1429. stringdispose(funcname);
  1430. inherited destroy;
  1431. end;
  1432. {****************************************************************************
  1433. TAI_CUTOBJECT
  1434. ****************************************************************************}
  1435. constructor tai_cutobject.Create;
  1436. begin
  1437. inherited Create;
  1438. typ:=ait_cutobject;
  1439. place:=cut_normal;
  1440. end;
  1441. constructor tai_cutobject.Create_begin;
  1442. begin
  1443. inherited Create;
  1444. typ:=ait_cutobject;
  1445. place:=cut_begin;
  1446. end;
  1447. constructor tai_cutobject.Create_end;
  1448. begin
  1449. inherited Create;
  1450. typ:=ait_cutobject;
  1451. place:=cut_end;
  1452. end;
  1453. constructor tai_cutobject.ppuload(t:taitype;ppufile:tcompilerppufile);
  1454. begin
  1455. inherited ppuload(t,ppufile);
  1456. place:=TCutPlace(ppufile.getbyte);
  1457. end;
  1458. procedure tai_cutobject.ppuwrite(ppufile:tcompilerppufile);
  1459. begin
  1460. inherited ppuwrite(ppufile);
  1461. ppufile.putbyte(byte(place));
  1462. end;
  1463. {****************************************************************************
  1464. Tai_Marker
  1465. ****************************************************************************}
  1466. constructor Tai_Marker.Create(_Kind: TMarker);
  1467. begin
  1468. Inherited Create;
  1469. typ := ait_marker;
  1470. Kind := _Kind;
  1471. end;
  1472. constructor Tai_Marker.ppuload(t:taitype;ppufile:tcompilerppufile);
  1473. begin
  1474. inherited ppuload(t,ppufile);
  1475. kind:=TMarker(ppufile.getbyte);
  1476. end;
  1477. procedure Tai_Marker.ppuwrite(ppufile:tcompilerppufile);
  1478. begin
  1479. inherited ppuwrite(ppufile);
  1480. ppufile.putbyte(byte(kind));
  1481. end;
  1482. {*****************************************************************************
  1483. tai_tempalloc
  1484. *****************************************************************************}
  1485. constructor tai_tempalloc.alloc(pos,size:longint);
  1486. begin
  1487. inherited Create;
  1488. typ:=ait_tempalloc;
  1489. allocation:=true;
  1490. temppos:=pos;
  1491. tempsize:=size;
  1492. {$ifdef EXTDEBUG}
  1493. problem:=nil;
  1494. {$endif EXTDEBUG}
  1495. end;
  1496. destructor tai_tempalloc.destroy;
  1497. begin
  1498. {$ifdef EXTDEBUG}
  1499. stringdispose(problem);
  1500. {$endif EXTDEBUG}
  1501. inherited destroy;
  1502. end;
  1503. constructor tai_tempalloc.dealloc(pos,size:longint);
  1504. begin
  1505. inherited Create;
  1506. typ:=ait_tempalloc;
  1507. allocation:=false;
  1508. temppos:=pos;
  1509. tempsize:=size;
  1510. {$ifdef EXTDEBUG}
  1511. problem:=nil;
  1512. {$endif EXTDEBUG}
  1513. end;
  1514. {$ifdef EXTDEBUG}
  1515. constructor tai_tempalloc.allocinfo(pos,size:longint;const st:string);
  1516. begin
  1517. inherited Create;
  1518. typ:=ait_tempalloc;
  1519. allocation:=false;
  1520. temppos:=pos;
  1521. tempsize:=size;
  1522. problem:=stringdup(st);
  1523. end;
  1524. {$endif EXTDEBUG}
  1525. constructor tai_tempalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1526. begin
  1527. inherited ppuload(t,ppufile);
  1528. temppos:=ppufile.getlongint;
  1529. tempsize:=ppufile.getlongint;
  1530. allocation:=boolean(ppufile.getbyte);
  1531. {$ifdef EXTDEBUG}
  1532. problem:=nil;
  1533. {$endif EXTDEBUG}
  1534. end;
  1535. procedure tai_tempalloc.ppuwrite(ppufile:tcompilerppufile);
  1536. begin
  1537. inherited ppuwrite(ppufile);
  1538. ppufile.putlongint(temppos);
  1539. ppufile.putlongint(tempsize);
  1540. ppufile.putbyte(byte(allocation));
  1541. end;
  1542. {*****************************************************************************
  1543. tai_regalloc
  1544. *****************************************************************************}
  1545. constructor tai_regalloc.alloc(r : tregister;ainstr:tai);
  1546. begin
  1547. inherited create;
  1548. typ:=ait_regalloc;
  1549. ratype:=ra_alloc;
  1550. reg:=r;
  1551. { ainstr must be an instruction }
  1552. if assigned(ainstr) and
  1553. (ainstr.typ<>ait_instruction) then
  1554. internalerror(200411011);
  1555. instr:=ainstr;
  1556. end;
  1557. constructor tai_regalloc.dealloc(r : tregister;ainstr:tai);
  1558. begin
  1559. inherited create;
  1560. typ:=ait_regalloc;
  1561. ratype:=ra_dealloc;
  1562. reg:=r;
  1563. { ainstr must be an instruction }
  1564. if assigned(ainstr) and
  1565. (ainstr.typ<>ait_instruction) then
  1566. internalerror(200411012);
  1567. instr:=ainstr;
  1568. end;
  1569. constructor tai_regalloc.sync(r : tregister);
  1570. begin
  1571. inherited create;
  1572. typ:=ait_regalloc;
  1573. ratype:=ra_sync;
  1574. reg:=r;
  1575. end;
  1576. constructor tai_regalloc.resize(r : tregister);
  1577. begin
  1578. inherited create;
  1579. typ:=ait_regalloc;
  1580. ratype:=ra_resize;
  1581. reg:=r;
  1582. end;
  1583. constructor tai_regalloc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1584. begin
  1585. inherited ppuload(t,ppufile);
  1586. ppufile.getdata(reg,sizeof(Tregister));
  1587. ratype:=tregalloctype(ppufile.getbyte);
  1588. end;
  1589. procedure tai_regalloc.ppuwrite(ppufile:tcompilerppufile);
  1590. begin
  1591. inherited ppuwrite(ppufile);
  1592. ppufile.putdata(reg,sizeof(Tregister));
  1593. ppufile.putbyte(byte(ratype));
  1594. end;
  1595. {****************************************************************************
  1596. tai_file
  1597. ****************************************************************************}
  1598. constructor tai_file.Create(_str : string);
  1599. begin
  1600. inherited Create;
  1601. typ:=ait_file;
  1602. str:=strpnew(_str);
  1603. end;
  1604. destructor tai_file.destroy;
  1605. begin
  1606. strdispose(str);
  1607. inherited Destroy;
  1608. end;
  1609. constructor tai_file.ppuload(t:taitype;ppufile:tcompilerppufile);
  1610. var
  1611. len : longint;
  1612. begin
  1613. inherited ppuload(t,ppufile);
  1614. len:=ppufile.getlongint;
  1615. getmem(str,len+1);
  1616. ppufile.getdata(str^,len);
  1617. str[len]:=#0;
  1618. idx:=ppufile.getlongint;
  1619. end;
  1620. procedure tai_file.ppuwrite(ppufile:tcompilerppufile);
  1621. var
  1622. len : longint;
  1623. begin
  1624. inherited ppuwrite(ppufile);
  1625. len:=strlen(str);
  1626. ppufile.putlongint(len);
  1627. ppufile.putdata(str^,len);
  1628. ppufile.putlongint(idx);
  1629. end;
  1630. function tai_file.getcopy : tlinkedlistitem;
  1631. var
  1632. p : tlinkedlistitem;
  1633. begin
  1634. p:=inherited getcopy;
  1635. getmem(tai_comment(p).str,strlen(str)+1);
  1636. move(str^,tai_comment(p).str^,strlen(str)+1);
  1637. getcopy:=p;
  1638. end;
  1639. {****************************************************************************
  1640. tai_loc
  1641. ****************************************************************************}
  1642. constructor tai_loc.Create(_fileidx : tai_file;_line,_column : longint);
  1643. begin
  1644. inherited Create;
  1645. typ:=ait_loc;
  1646. fileentry:=_fileidx;
  1647. line:=_line;
  1648. column:=_column;
  1649. end;
  1650. constructor tai_loc.ppuload(t:taitype;ppufile:tcompilerppufile);
  1651. begin
  1652. inherited ppuload(t,ppufile);
  1653. {!!!! fileidx:=ppufile.getlongint; }
  1654. line:=ppufile.getlongint;
  1655. column:=ppufile.getlongint;
  1656. end;
  1657. procedure tai_loc.ppuwrite(ppufile:tcompilerppufile);
  1658. begin
  1659. inherited ppuwrite(ppufile);
  1660. {!!!!! ppufile.putlongint(fileidx); }
  1661. ppufile.putlongint(line);
  1662. ppufile.putlongint(column);
  1663. end;
  1664. {*****************************************************************************
  1665. TaiInstruction
  1666. *****************************************************************************}
  1667. constructor tai_cpu_abstract.Create(op : tasmop);
  1668. begin
  1669. inherited create;
  1670. typ:=ait_instruction;
  1671. is_jmp:=false;
  1672. opcode:=op;
  1673. ops:=0;
  1674. fillchar(condition,sizeof(condition),0);
  1675. fillchar(oper,sizeof(oper),0);
  1676. end;
  1677. destructor tai_cpu_abstract.Destroy;
  1678. var
  1679. i : integer;
  1680. begin
  1681. for i:=0 to opercnt-1 do
  1682. begin
  1683. clearop(i);
  1684. dispose(oper[i]);
  1685. end;
  1686. inherited destroy;
  1687. end;
  1688. { ---------------------------------------------------------------------
  1689. Loading of operands.
  1690. ---------------------------------------------------------------------}
  1691. procedure tai_cpu_abstract.allocate_oper(opers:longint);
  1692. begin
  1693. while (opers>opercnt) do
  1694. begin
  1695. new(oper[opercnt]);
  1696. fillchar(oper[opercnt]^,sizeof(toper),0);
  1697. inc(opercnt);
  1698. end;
  1699. end;
  1700. procedure tai_cpu_abstract.loadconst(opidx:longint;l:aint);
  1701. begin
  1702. allocate_oper(opidx+1);
  1703. with oper[opidx]^ do
  1704. begin
  1705. if typ<>top_const then
  1706. clearop(opidx);
  1707. val:=l;
  1708. typ:=top_const;
  1709. end;
  1710. end;
  1711. procedure tai_cpu_abstract.loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  1712. var
  1713. r : treference;
  1714. begin
  1715. reference_reset_symbol(r,s,sofs);
  1716. r.refaddr:=addr_full;
  1717. loadref(opidx,r);
  1718. end;
  1719. procedure tai_cpu_abstract.loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  1720. begin
  1721. if not assigned(s) then
  1722. internalerror(200204251);
  1723. allocate_oper(opidx+1);
  1724. with oper[opidx]^ do
  1725. begin
  1726. if typ<>top_local then
  1727. begin
  1728. clearop(opidx);
  1729. new(localoper);
  1730. end;
  1731. with oper[opidx]^.localoper^ do
  1732. begin
  1733. localsym:=s;
  1734. localsymofs:=sofs;
  1735. localindexreg:=indexreg;
  1736. localscale:=scale;
  1737. localgetoffset:=getoffset;
  1738. localforceref:=forceref;
  1739. end;
  1740. typ:=top_local;
  1741. end;
  1742. end;
  1743. procedure tai_cpu_abstract.loadref(opidx:longint;const r:treference);
  1744. begin
  1745. allocate_oper(opidx+1);
  1746. with oper[opidx]^ do
  1747. begin
  1748. if typ<>top_ref then
  1749. begin
  1750. clearop(opidx);
  1751. new(ref);
  1752. end;
  1753. ref^:=r;
  1754. {$ifdef x86}
  1755. { We allow this exception for x86, since overloading this would be
  1756. too much of a a speed penalty}
  1757. if (ref^.segment<>NR_NO) and (ref^.segment<>NR_DS) then
  1758. segprefix:=ref^.segment;
  1759. {$endif}
  1760. {$ifdef extdebug}
  1761. if (cs_create_pic in aktmoduleswitches) and
  1762. assigned(r.symbol) and
  1763. (r.refaddr=addr_no) then
  1764. internalerror(200502052);
  1765. {$endif}
  1766. typ:=top_ref;
  1767. if assigned(add_reg_instruction_hook) then
  1768. begin
  1769. add_reg_instruction_hook(self,ref^.base);
  1770. add_reg_instruction_hook(self,ref^.index);
  1771. end;
  1772. { mark symbol as used }
  1773. if assigned(ref^.symbol) then
  1774. ref^.symbol.increfs;
  1775. end;
  1776. end;
  1777. procedure tai_cpu_abstract.loadreg(opidx:longint;r:tregister);
  1778. begin
  1779. allocate_oper(opidx+1);
  1780. with oper[opidx]^ do
  1781. begin
  1782. if typ<>top_reg then
  1783. clearop(opidx);
  1784. reg:=r;
  1785. typ:=top_reg;
  1786. end;
  1787. if assigned(add_reg_instruction_hook) then
  1788. add_reg_instruction_hook(self,r);
  1789. {$ifdef ARM}
  1790. { R15 is the PC on the ARM thus moves to R15 are jumps.
  1791. Due to speed considerations we don't use a virtual overridden method here.
  1792. Because the pc/r15 isn't handled by the reg. allocator this should never cause
  1793. problems with iregs getting r15.
  1794. }
  1795. is_jmp:=(opcode=A_MOV) and (opidx=0) and (r=NR_R15);
  1796. {$endif ARM}
  1797. end;
  1798. procedure tai_cpu_abstract.loadoper(opidx:longint;o:toper);
  1799. begin
  1800. allocate_oper(opidx+1);
  1801. clearop(opidx);
  1802. oper[opidx]^:=o;
  1803. { copy also the reference }
  1804. with oper[opidx]^ do
  1805. begin
  1806. case typ of
  1807. top_reg:
  1808. begin
  1809. if assigned(add_reg_instruction_hook) then
  1810. add_reg_instruction_hook(self,reg);
  1811. end;
  1812. top_ref:
  1813. begin
  1814. new(ref);
  1815. ref^:=o.ref^;
  1816. if assigned(add_reg_instruction_hook) then
  1817. begin
  1818. add_reg_instruction_hook(self,ref^.base);
  1819. add_reg_instruction_hook(self,ref^.index);
  1820. end;
  1821. end;
  1822. {$ifdef ARM}
  1823. top_shifterop:
  1824. begin
  1825. new(shifterop);
  1826. shifterop^:=o.shifterop^;
  1827. if assigned(add_reg_instruction_hook) then
  1828. add_reg_instruction_hook(self,shifterop^.rs);
  1829. end;
  1830. {$endif ARM}
  1831. end;
  1832. end;
  1833. end;
  1834. procedure tai_cpu_abstract.clearop(opidx:longint);
  1835. begin
  1836. with oper[opidx]^ do
  1837. begin
  1838. case typ of
  1839. top_ref:
  1840. dispose(ref);
  1841. top_local:
  1842. dispose(localoper);
  1843. {$ifdef ARM}
  1844. top_shifterop:
  1845. dispose(shifterop);
  1846. top_regset:
  1847. dispose(regset);
  1848. {$endif ARM}
  1849. end;
  1850. typ:=top_none;
  1851. end;
  1852. end;
  1853. { ---------------------------------------------------------------------
  1854. Miscellaneous methods.
  1855. ---------------------------------------------------------------------}
  1856. procedure tai_cpu_abstract.SetCondition(const c:TAsmCond);
  1857. begin
  1858. condition:=c;
  1859. end;
  1860. Function tai_cpu_abstract.getcopy:TLinkedListItem;
  1861. var
  1862. i : longint;
  1863. p : tai_cpu_abstract;
  1864. begin
  1865. p:=tai_cpu_abstract(inherited getcopy);
  1866. { make a copy of the references }
  1867. p.opercnt:=0;
  1868. p.allocate_oper(ops);
  1869. for i:=0 to ops-1 do
  1870. begin
  1871. p.oper[i]^:=oper[i]^;
  1872. case oper[i]^.typ of
  1873. top_local :
  1874. begin
  1875. new(p.oper[i]^.localoper);
  1876. p.oper[i]^.localoper^:=oper[i]^.localoper^;
  1877. end;
  1878. top_ref :
  1879. begin
  1880. new(p.oper[i]^.ref);
  1881. p.oper[i]^.ref^:=oper[i]^.ref^;
  1882. end;
  1883. {$ifdef ARM}
  1884. top_shifterop:
  1885. begin
  1886. new(p.oper[i]^.shifterop);
  1887. p.oper[i]^.shifterop^:=oper[i]^.shifterop^;
  1888. end;
  1889. {$endif ARM}
  1890. end;
  1891. end;
  1892. getcopy:=p;
  1893. end;
  1894. function tai_cpu_abstract.is_same_reg_move(regtype: Tregistertype):boolean;
  1895. begin
  1896. { When the generic RA is used this needs to be overriden, we don't use
  1897. virtual;abstract; to prevent a lot of warnings of unimplemented abstract methods
  1898. when tai_cpu is created (PFV) }
  1899. internalerror(200404091);
  1900. result:=false;
  1901. end;
  1902. function tai_cpu_abstract.spilling_get_operation_type(opnr: longint): topertype;
  1903. begin
  1904. internalerror(200404091);
  1905. result:=operand_readwrite;
  1906. end;
  1907. function tai_cpu_abstract.spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;
  1908. begin
  1909. result := operand_read;
  1910. end;
  1911. constructor tai_cpu_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  1912. var
  1913. i : integer;
  1914. begin
  1915. inherited ppuload(t,ppufile);
  1916. { hopefully, we don't get problems with big/litte endian here when cross compiling :/ }
  1917. ppufile.getdata(condition,sizeof(tasmcond));
  1918. allocate_oper(ppufile.getbyte);
  1919. for i:=0 to ops-1 do
  1920. ppuloadoper(ppufile,oper[i]^);
  1921. opcode:=tasmop(ppufile.getword);
  1922. {$ifdef x86}
  1923. ppufile.getdata(segprefix,sizeof(Tregister));
  1924. {$endif x86}
  1925. is_jmp:=boolean(ppufile.getbyte);
  1926. end;
  1927. procedure tai_cpu_abstract.ppuwrite(ppufile:tcompilerppufile);
  1928. var
  1929. i : integer;
  1930. begin
  1931. inherited ppuwrite(ppufile);
  1932. ppufile.putdata(condition,sizeof(tasmcond));
  1933. ppufile.putbyte(ops);
  1934. for i:=0 to ops-1 do
  1935. ppuwriteoper(ppufile,oper[i]^);
  1936. ppufile.putword(word(opcode));
  1937. {$ifdef x86}
  1938. ppufile.putdata(segprefix,sizeof(Tregister));
  1939. {$endif x86}
  1940. ppufile.putbyte(byte(is_jmp));
  1941. end;
  1942. procedure tai_cpu_abstract.buildderefimpl;
  1943. var
  1944. i : integer;
  1945. begin
  1946. for i:=0 to ops-1 do
  1947. ppubuildderefimploper(oper[i]^);
  1948. end;
  1949. procedure tai_cpu_abstract.derefimpl;
  1950. var
  1951. i : integer;
  1952. begin
  1953. for i:=0 to ops-1 do
  1954. ppuderefoper(oper[i]^);
  1955. end;
  1956. procedure tai_cpu_abstract.resetpass1;
  1957. begin
  1958. end;
  1959. procedure tai_cpu_abstract.resetpass2;
  1960. begin
  1961. end;
  1962. {****************************************************************************
  1963. tai_align_abstract
  1964. ****************************************************************************}
  1965. constructor tai_align_abstract.Create(b: byte);
  1966. begin
  1967. inherited Create;
  1968. typ:=ait_align;
  1969. if b in [1,2,4,8,16,32] then
  1970. aligntype := b
  1971. else
  1972. aligntype := 1;
  1973. fillsize:=0;
  1974. fillop:=0;
  1975. use_op:=false;
  1976. end;
  1977. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  1978. begin
  1979. inherited Create;
  1980. typ:=ait_align;
  1981. if b in [1,2,4,8,16,32] then
  1982. aligntype := b
  1983. else
  1984. aligntype := 1;
  1985. fillsize:=0;
  1986. fillop:=_op;
  1987. use_op:=true;
  1988. end;
  1989. constructor tai_align_abstract.Create_zeros(b: byte);
  1990. begin
  1991. inherited Create;
  1992. typ:=ait_align;
  1993. if b in [1,2,4,8,16,32] then
  1994. aligntype := b
  1995. else
  1996. aligntype := 1;
  1997. use_op:=true;
  1998. fillsize:=0;
  1999. fillop:=0;
  2000. end;
  2001. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer):pchar;
  2002. begin
  2003. if fillsize>sizeof(buf) then
  2004. internalerror(200404293);
  2005. fillchar(buf,high(buf),fillop);
  2006. calculatefillbuf:=pchar(@buf);
  2007. end;
  2008. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2009. begin
  2010. inherited ppuload(t,ppufile);
  2011. aligntype:=ppufile.getbyte;
  2012. fillsize:=0;
  2013. fillop:=ppufile.getbyte;
  2014. use_op:=boolean(ppufile.getbyte);
  2015. end;
  2016. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2017. begin
  2018. inherited ppuwrite(ppufile);
  2019. ppufile.putbyte(aligntype);
  2020. ppufile.putbyte(fillop);
  2021. ppufile.putbyte(byte(use_op));
  2022. end;
  2023. {*****************************************************************************
  2024. TAAsmOutput
  2025. *****************************************************************************}
  2026. constructor taasmoutput.create;
  2027. begin
  2028. inherited create;
  2029. { make sure the optimizer won't remove the first tai of this list}
  2030. insert(tai_marker.create(marker_blockstart));
  2031. end;
  2032. function taasmoutput.empty : boolean;
  2033. begin
  2034. { there is always a marker_blockstart available,
  2035. see taasmoutput.create }
  2036. result:=(count<=1);
  2037. end;
  2038. function taasmoutput.getlasttaifilepos : pfileposinfo;
  2039. var
  2040. hp : tlinkedlistitem;
  2041. begin
  2042. getlasttaifilepos := nil;
  2043. if assigned(last) then
  2044. begin
  2045. { find the last file information record }
  2046. if not (tai(last).typ in SkipLineInfo) then
  2047. getlasttaifilepos:=@tailineinfo(last).fileinfo
  2048. else
  2049. { go through list backwards to find the first entry
  2050. with line information
  2051. }
  2052. begin
  2053. hp:=tai(last);
  2054. while assigned(hp) and (tai(hp).typ in SkipLineInfo) do
  2055. hp:=hp.Previous;
  2056. { found entry }
  2057. if assigned(hp) then
  2058. getlasttaifilepos:=@tailineinfo(hp).fileinfo
  2059. end;
  2060. end;
  2061. end;
  2062. procedure Taasmoutput.InsertAfter(Item,Loc : TLinkedListItem);
  2063. begin
  2064. { This is not possible because it is not sure that the
  2065. tai at Loc has taifileinfo as parent }
  2066. {if assigned(Loc) then
  2067. tailineinfo(Item).fileinfo:=tailineinfo(Loc).fileinfo;}
  2068. inherited InsertAfter(Item,Loc);
  2069. end;
  2070. begin
  2071. cai_cpu:=tai_cpu_abstract;
  2072. cai_align:=tai_align_abstract;
  2073. end.