aasmtai.pas 68 KB

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