aasmtai.pas 67 KB

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