aasmtai.pas 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489
  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. aitconst_secrel32_symbol,
  91. { darwin only }
  92. aitconst_indirect_symbol
  93. );
  94. const
  95. {$ifdef cpu64bit}
  96. aitconst_aint = aitconst_64bit;
  97. aitconst_ptr = aitconst_64bit;
  98. {$else cpu64bit}
  99. aitconst_aint = aitconst_32bit;
  100. aitconst_ptr = aitconst_32bit;
  101. {$endif cpu64bit}
  102. taitypestr : array[taitype] of string[24] = (
  103. '<none>',
  104. 'align',
  105. 'section',
  106. 'comment',
  107. 'string',
  108. 'instruction',
  109. 'datablock',
  110. 'symbol',
  111. 'symbol_end',
  112. 'symbol_directive',
  113. 'label',
  114. 'const',
  115. 'real_32bit',
  116. 'real_64bit',
  117. 'real_80bit',
  118. 'comp_64bit',
  119. 'real_128bit',
  120. 'stab',
  121. 'force_line',
  122. 'function_name',
  123. {$ifdef alpha}
  124. { the follow is for the DEC Alpha }
  125. 'frame',
  126. 'ent',
  127. {$endif alpha}
  128. {$ifdef ia64}
  129. 'bundle',
  130. 'stop',
  131. {$endif ia64}
  132. {$ifdef m68k}
  133. 'labeled_instr',
  134. {$endif m68k}
  135. 'cut',
  136. 'regalloc',
  137. 'tempalloc',
  138. 'marker',
  139. 'file',
  140. 'line'
  141. );
  142. type
  143. { Types of operand }
  144. toptype=(top_none,top_reg,top_ref,top_const,top_bool,top_local
  145. {$ifdef arm}
  146. { ARM only }
  147. ,top_regset
  148. ,top_shifterop
  149. {$endif arm}
  150. {$ifdef m68k}
  151. { m68k only }
  152. ,top_regset
  153. {$endif m68k}
  154. { i386 only});
  155. { kinds of operations that an instruction can perform on an operand }
  156. topertype = (operand_read,operand_write,operand_readwrite);
  157. tlocaloper = record
  158. localsym : pointer;
  159. localsymderef : tderef;
  160. localsymofs : longint;
  161. localindexreg : tregister;
  162. localscale : byte;
  163. localgetoffset,
  164. localforceref : boolean
  165. end;
  166. plocaloper = ^tlocaloper;
  167. { please keep the size of this record <=12 bytes and keep it properly aligned }
  168. toper = record
  169. ot : longint;
  170. case typ : toptype of
  171. top_none : ();
  172. top_reg : (reg:tregister);
  173. top_ref : (ref:preference);
  174. top_const : (val:aint);
  175. top_bool : (b:boolean);
  176. { local varsym that will be inserted in pass_generate_code }
  177. top_local : (localoper:plocaloper);
  178. {$ifdef arm}
  179. top_regset : (regset:^tcpuregisterset);
  180. top_shifterop : (shifterop : pshifterop);
  181. {$endif arm}
  182. {$ifdef m68k}
  183. top_regset : (regset:^tcpuregisterset);
  184. {$endif m68k}
  185. end;
  186. poper=^toper;
  187. const
  188. { ait_* types which don't result in executable code or which don't influence
  189. the way the program runs/behaves, but which may be encountered by the
  190. optimizer (= if it's sometimes added to the exprasm list). Update if you add
  191. a new ait type! }
  192. SkipInstr = [ait_comment, ait_symbol,ait_section
  193. ,ait_stab, ait_function_name, ait_force_line
  194. ,ait_regalloc, ait_tempalloc, ait_symbol_end, ait_directive
  195. ,ait_file,ait_loc];
  196. { ait_* types which do not have line information (and hence which are of type
  197. tai, otherwise, they are of type tailineinfo }
  198. SkipLineInfo =[ait_label,
  199. ait_regalloc,ait_tempalloc,
  200. ait_stab,ait_function_name,
  201. ait_cutobject,ait_marker,ait_align,ait_section,ait_comment,
  202. ait_const,
  203. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_real_128bit,
  204. ait_file,ait_loc,ait_symbol
  205. ];
  206. type
  207. { cut type, required for alphanumeric ordering of the assembler filenames }
  208. TCutPlace=(cut_normal,cut_begin,cut_end);
  209. TAsmMarker = (
  210. mark_NoPropInfoStart,mark_NoPropInfoEnd,
  211. mark_AsmBlockStart,mark_AsmBlockEnd,
  212. mark_InlineStart,mark_InlineEnd,mark_BlockStart,
  213. mark_Position
  214. );
  215. TRegAllocType = (ra_alloc,ra_dealloc,ra_sync,ra_resize);
  216. TStabType = (stab_stabs,stab_stabn,stab_stabd);
  217. TAsmDirective=(
  218. asd_non_lazy_symbol_pointer,asd_indirect_symbol,asd_lazy_symbol_pointer,
  219. asd_extern,asd_nasm_import, asd_toc_entry, asd_mod_init_func, asd_mod_term_func,
  220. asd_reference,asd_no_dead_strip
  221. );
  222. const
  223. regallocstr : array[tregalloctype] of string[10]=('allocated','released','sync','resized');
  224. tempallocstr : array[boolean] of string[10]=('released','allocated');
  225. stabtypestr : array[TStabType] of string[5]=('stabs','stabn','stabd');
  226. directivestr : array[TAsmDirective] of string[23]=(
  227. 'non_lazy_symbol_pointer','indirect_symbol','lazy_symbol_pointer',
  228. 'extern','nasm_import', 'tc', 'mod_init_func', 'mod_term_func', 'reference',
  229. 'no_dead_strip'
  230. );
  231. type
  232. { abstract assembler item }
  233. tai = class(TLinkedListItem)
  234. {$ifndef NOOPT}
  235. { pointer to record with optimizer info about this tai object }
  236. optinfo : pointer;
  237. {$endif NOOPT}
  238. typ : taitype;
  239. constructor Create;
  240. constructor ppuload(t:taitype;ppufile:tcompilerppufile);virtual;
  241. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  242. procedure buildderefimpl;virtual;
  243. procedure derefimpl;virtual;
  244. end;
  245. { abstract assembler item with line information }
  246. tailineinfo = class(tai)
  247. fileinfo : tfileposinfo;
  248. constructor Create;
  249. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  250. procedure ppuwrite(ppufile:tcompilerppufile);override;
  251. end;
  252. tai_simple = class(tai)
  253. constructor create(_typ : taitype);
  254. end;
  255. taiclass = class of tai;
  256. taiclassarray = array[taitype] of taiclass;
  257. { Generates an assembler string }
  258. tai_string = class(tailineinfo)
  259. str : pchar;
  260. { extra len so the string can contain an \0 }
  261. len : longint;
  262. constructor Create(const _str : string);
  263. constructor Create_pchar(_str : pchar;length : longint);
  264. destructor Destroy;override;
  265. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  266. procedure ppuwrite(ppufile:tcompilerppufile);override;
  267. function getcopy:tlinkedlistitem;override;
  268. end;
  269. { Generates a common label }
  270. tai_symbol = class(tai)
  271. is_global : boolean;
  272. sym : tasmsymbol;
  273. size : longint;
  274. constructor Create(_sym:tasmsymbol;siz:longint);
  275. constructor Create_Global(_sym:tasmsymbol;siz:longint);
  276. constructor Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  277. constructor Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  278. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  279. procedure ppuwrite(ppufile:tcompilerppufile);override;
  280. procedure derefimpl;override;
  281. end;
  282. tai_symbol_end = class(tailineinfo)
  283. sym : tasmsymbol;
  284. constructor Create(_sym:tasmsymbol);
  285. constructor Createname(const _name : string);
  286. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  287. procedure ppuwrite(ppufile:tcompilerppufile);override;
  288. procedure derefimpl;override;
  289. end;
  290. tai_directive = class(tailineinfo)
  291. name : pshortstring;
  292. directive : TAsmDirective;
  293. constructor Create(_directive:TAsmDirective;const _name:string);
  294. destructor Destroy;override;
  295. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  296. procedure ppuwrite(ppufile:tcompilerppufile);override;
  297. end;
  298. { Generates an assembler label }
  299. tai_label = class(tai)
  300. is_global : boolean;
  301. labsym : tasmlabel;
  302. constructor Create(_labsym : tasmlabel);
  303. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  304. procedure ppuwrite(ppufile:tcompilerppufile);override;
  305. procedure derefimpl;override;
  306. end;
  307. { Generates an assembler comment }
  308. tai_comment = class(tai)
  309. str : pchar;
  310. constructor Create(_str : pchar);
  311. destructor Destroy; override;
  312. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  313. procedure ppuwrite(ppufile:tcompilerppufile);override;
  314. function getcopy:tlinkedlistitem;override;
  315. end;
  316. { Generates a section / segment directive }
  317. tai_section = class(tai)
  318. sectype : TAsmSectiontype;
  319. secorder : TasmSectionorder;
  320. secalign : byte;
  321. name : pshortstring;
  322. sec : TObjSection; { used in binary writer }
  323. constructor Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  324. destructor Destroy;override;
  325. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  326. procedure ppuwrite(ppufile:tcompilerppufile);override;
  327. end;
  328. { Generates an uninitializised data block }
  329. tai_datablock = class(tailineinfo)
  330. is_global : boolean;
  331. sym : tasmsymbol;
  332. size : aint;
  333. constructor Create(const _name : string;_size : aint);
  334. constructor Create_global(const _name : string;_size : aint);
  335. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  336. procedure ppuwrite(ppufile:tcompilerppufile);override;
  337. procedure derefimpl;override;
  338. end;
  339. { Generates an integer const }
  340. tai_const = class(tai)
  341. sym,
  342. endsym : tasmsymbol;
  343. value : int64;
  344. consttype : taiconst_type;
  345. { we use for the 128bit int64/qword for now because I can't imagine a
  346. case where we need 128 bit now (FK) }
  347. constructor Create(_typ:taiconst_type;_value : int64);
  348. constructor Create_128bit(_value : int64);
  349. constructor Create_64bit(_value : int64);
  350. constructor Create_32bit(_value : longint);
  351. constructor Create_16bit(_value : word);
  352. constructor Create_8bit(_value : byte);
  353. constructor Create_sleb128bit(_value : int64);
  354. constructor Create_uleb128bit(_value : qword);
  355. constructor Create_aint(_value : aint);
  356. constructor Create_sym(_sym:tasmsymbol);
  357. constructor Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  358. constructor Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  359. constructor Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  360. constructor Create_rva_sym(_sym:tasmsymbol);
  361. constructor Create_indirect_sym(_sym:tasmsymbol);
  362. constructor Createname(const name:string;ofs:aint);
  363. constructor Createname_rva(const name:string);
  364. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  365. procedure ppuwrite(ppufile:tcompilerppufile);override;
  366. procedure derefimpl;override;
  367. function getcopy:tlinkedlistitem;override;
  368. function size:longint;
  369. end;
  370. { Generates a single float (32 bit real) }
  371. tai_real_32bit = class(tai)
  372. value : ts32real;
  373. constructor Create(_value : ts32real);
  374. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  375. procedure ppuwrite(ppufile:tcompilerppufile);override;
  376. end;
  377. tformatoptions = (fo_none,fo_hiloswapped);
  378. { Generates a double float (64 bit real) }
  379. tai_real_64bit = class(tai)
  380. value : ts64real;
  381. {$ifdef ARM}
  382. formatoptions : tformatoptions;
  383. constructor Create_hiloswapped(_value : ts64real);
  384. {$endif ARM}
  385. constructor Create(_value : ts64real);
  386. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  387. procedure ppuwrite(ppufile:tcompilerppufile);override;
  388. end;
  389. { Generates an extended float (80 bit real) }
  390. tai_real_80bit = class(tai)
  391. value : ts80real;
  392. constructor Create(_value : ts80real);
  393. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  394. procedure ppuwrite(ppufile:tcompilerppufile);override;
  395. end;
  396. { Generates an float128 (128 bit real) }
  397. tai_real_128bit = class(tai)
  398. value : ts128real;
  399. constructor Create(_value : ts128real);
  400. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  401. procedure ppuwrite(ppufile:tcompilerppufile);override;
  402. end;
  403. { Generates a comp int (integer over 64 bits)
  404. This is Intel 80x86 specific, and is not
  405. really supported on other processors.
  406. }
  407. tai_comp_64bit = class(tai)
  408. value : ts64comp;
  409. constructor Create(_value : ts64comp);
  410. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  411. procedure ppuwrite(ppufile:tcompilerppufile);override;
  412. end;
  413. tai_stab = class(tai)
  414. str : pchar;
  415. stabtype : TStabType;
  416. constructor Create(_stabtype:TStabType;_str : pchar);
  417. constructor Create_str(_stabtype:TStabType;const s:string);
  418. destructor Destroy;override;
  419. end;
  420. tai_force_line = class(tailineinfo)
  421. constructor Create;
  422. end;
  423. tai_function_name = class(tai)
  424. funcname : pshortstring;
  425. constructor create(const s:string);
  426. destructor destroy;override;
  427. end;
  428. { Insert a cut to split assembler into several smaller files }
  429. tai_cutobject = class(tai)
  430. place : tcutplace;
  431. constructor Create;
  432. constructor Create_begin;
  433. constructor Create_end;
  434. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  435. procedure ppuwrite(ppufile:tcompilerppufile);override;
  436. end;
  437. { Insert a marker for assembler and inline blocks }
  438. tai_marker = class(tai)
  439. Kind: TAsmMarker;
  440. Constructor Create(_Kind: TAsmMarker);
  441. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  442. procedure ppuwrite(ppufile:tcompilerppufile);override;
  443. end;
  444. tai_tempalloc = class(tai)
  445. allocation : boolean;
  446. {$ifdef EXTDEBUG}
  447. problem : pshortstring;
  448. {$endif EXTDEBUG}
  449. temppos,
  450. tempsize : longint;
  451. constructor alloc(pos,size:longint);
  452. constructor dealloc(pos,size:longint);
  453. {$ifdef EXTDEBUG}
  454. constructor allocinfo(pos,size:longint;const st:string);
  455. {$endif EXTDEBUG}
  456. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  457. destructor destroy;override;
  458. procedure ppuwrite(ppufile:tcompilerppufile);override;
  459. end;
  460. tai_regalloc = class(tai)
  461. reg : tregister;
  462. ratype : TRegAllocType;
  463. { reg(de)alloc belongs to this instruction, this
  464. is only used for automatic inserted (de)alloc for
  465. imaginary register and required for spilling code }
  466. instr : tai;
  467. constructor alloc(r : tregister;ainstr:tai);
  468. constructor dealloc(r : tregister;ainstr:tai);
  469. constructor sync(r : tregister);
  470. constructor resize(r : tregister);
  471. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  472. procedure ppuwrite(ppufile:tcompilerppufile);override;
  473. end;
  474. { Generates a dwarf file location }
  475. tai_file = class(tai)
  476. str : pchar;
  477. idx : longint;
  478. constructor Create(_str : string);
  479. destructor Destroy; override;
  480. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  481. procedure ppuwrite(ppufile:tcompilerppufile);override;
  482. function getcopy:tlinkedlistitem;override;
  483. end;
  484. { Generates a dwarf line location }
  485. tai_loc = class(tai)
  486. fileentry : tai_file;
  487. line,
  488. column : longint;
  489. constructor Create(_fileidx : tai_file;_line,_column : longint);
  490. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  491. procedure ppuwrite(ppufile:tcompilerppufile);override;
  492. end;
  493. tadd_reg_instruction_proc=procedure(instr:Tai;r:tregister) of object;
  494. { Class template for assembler instructions
  495. }
  496. tai_cpu_abstract = class(tailineinfo)
  497. protected
  498. procedure ppuloadoper(ppufile:tcompilerppufile;var o:toper);virtual;
  499. procedure ppuwriteoper(ppufile:tcompilerppufile;const o:toper);virtual;
  500. procedure ppubuildderefimploper(var o:toper);virtual;abstract;
  501. procedure ppuderefoper(var o:toper);virtual;abstract;
  502. public
  503. { Condition flags for instruction }
  504. condition : TAsmCond;
  505. { Number of operands to instruction }
  506. ops : byte;
  507. { Number of allocate oper structures }
  508. opercnt : byte;
  509. { Operands of instruction }
  510. oper : array[0..max_operands-1] of poper;
  511. { Actual opcode of instruction }
  512. opcode : tasmop;
  513. {$ifdef x86}
  514. segprefix : tregister;
  515. {$endif x86}
  516. { true if instruction is a jmp }
  517. is_jmp : boolean; { is this instruction a jump? (needed for optimizer) }
  518. Constructor Create(op : tasmop);virtual;
  519. Destructor Destroy;override;
  520. function getcopy:TLinkedListItem;override;
  521. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  522. procedure ppuwrite(ppufile:tcompilerppufile);override;
  523. procedure buildderefimpl;override;
  524. procedure derefimpl;override;
  525. procedure SetCondition(const c:TAsmCond);
  526. procedure allocate_oper(opers:longint);
  527. procedure loadconst(opidx:longint;l:aint);
  528. procedure loadsymbol(opidx:longint;s:tasmsymbol;sofs:longint);
  529. procedure loadlocal(opidx:longint;s:pointer;sofs:longint;indexreg:tregister;scale:byte;getoffset,forceref:boolean);
  530. procedure loadref(opidx:longint;const r:treference);
  531. procedure loadreg(opidx:longint;r:tregister);
  532. procedure loadoper(opidx:longint;o:toper);
  533. procedure clearop(opidx:longint);
  534. procedure freeop(opidx:longint);
  535. { register allocator }
  536. function is_same_reg_move(regtype: Tregistertype):boolean;virtual;
  537. function spilling_get_operation_type(opnr: longint): topertype;virtual;
  538. function spilling_get_operation_type_ref(opnr: longint; reg: tregister): topertype;virtual;
  539. function Pass1(objdata:TObjData):longint;virtual;
  540. procedure Pass2(objdata:TObjData);virtual;
  541. procedure resetpass1; virtual;
  542. procedure resetpass2; virtual;
  543. end;
  544. tai_cpu_class = class of tai_cpu_abstract;
  545. { Buffer type used for alignment }
  546. tfillbuffer = array[0..63] of char;
  547. { alignment for operator }
  548. tai_align_abstract = class(tai)
  549. aligntype : byte; { 1 = no align, 2 = word align, 4 = dword align }
  550. fillsize : byte; { real size to fill }
  551. fillop : byte; { value to fill with - optional }
  552. use_op : boolean;
  553. constructor Create(b:byte);virtual;
  554. constructor Create_op(b: byte; _op: byte);virtual;
  555. constructor Create_zeros(b:byte);
  556. constructor ppuload(t:taitype;ppufile:tcompilerppufile);override;
  557. procedure ppuwrite(ppufile:tcompilerppufile);override;
  558. function calculatefillbuf(var buf : tfillbuffer):pchar;virtual;
  559. end;
  560. tai_align_class = class of tai_align_abstract;
  561. var
  562. { array with all class types for tais }
  563. aiclass : taiclassarray;
  564. { target specific tais }
  565. cai_align : tai_align_class;
  566. cai_cpu : tai_cpu_class;
  567. { hook to notify uses of registers }
  568. add_reg_instruction_hook : tadd_reg_instruction_proc;
  569. procedure maybe_new_object_file(list:TAsmList);
  570. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  571. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  572. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  573. procedure section_symbol_end(list:TAsmList;const Aname:string);
  574. function ppuloadai(ppufile:tcompilerppufile):tai;
  575. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  576. implementation
  577. uses
  578. SysUtils,
  579. verbose;
  580. const
  581. pputaimarker = 254;
  582. {****************************************************************************
  583. Helpers
  584. ****************************************************************************}
  585. procedure maybe_new_object_file(list:TAsmList);
  586. begin
  587. if (cs_create_smart in current_settings.moduleswitches) and
  588. (not use_smartlink_section) then
  589. list.concat(tai_cutobject.create);
  590. end;
  591. procedure new_section(list:TAsmList;Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  592. begin
  593. list.concat(tai_section.create(Asectype,Aname,Aalign,Asecorder));
  594. list.concat(cai_align.create(Aalign));
  595. end;
  596. procedure section_symbol_start(list:TAsmList;const Aname:string;Asymtyp:Tasmsymtype;
  597. Aglobal:boolean;Asectype:TAsmSectiontype;Aalign:byte);
  598. begin
  599. maybe_new_object_file(list);
  600. list.concat(tai_section.create(Asectype,Aname,Aalign));
  601. list.concat(cai_align.create(Aalign));
  602. if Aglobal or
  603. maybe_smartlink_symbol then
  604. list.concat(tai_symbol.createname_global(Aname,Asymtyp,0))
  605. else
  606. list.concat(tai_symbol.createname(Aname,Asymtyp,0));
  607. end;
  608. procedure section_symbol_end(list:TAsmList;const Aname:string);
  609. begin
  610. list.concat(tai_symbol_end.createname(Aname));
  611. end;
  612. function ppuloadai(ppufile:tcompilerppufile):tai;
  613. var
  614. b : byte;
  615. t : taitype;
  616. begin
  617. { marker }
  618. b:=ppufile.getbyte;
  619. if b<>pputaimarker then
  620. internalerror(200208181);
  621. { load nodetype }
  622. t:=taitype(ppufile.getbyte);
  623. if t<>ait_none then
  624. begin
  625. if t>high(taitype) then
  626. internalerror(200208182);
  627. if not assigned(aiclass[t]) then
  628. internalerror(200208183);
  629. {writeln('taiload: ',taitypestr[t]);}
  630. { generate tai of the correct class }
  631. ppuloadai:=aiclass[t].ppuload(t,ppufile);
  632. end
  633. else
  634. ppuloadai:=nil;
  635. end;
  636. procedure ppuwriteai(ppufile:tcompilerppufile;n:tai);
  637. begin
  638. { marker, read by ppuloadnode }
  639. ppufile.putbyte(pputaimarker);
  640. if assigned(n) then
  641. begin
  642. { type, read by ppuloadnode }
  643. ppufile.putbyte(byte(n.typ));
  644. {writeln('taiwrite: ',taitypestr[n.typ]);}
  645. n.ppuwrite(ppufile);
  646. end
  647. else
  648. ppufile.putbyte(byte(ait_none));
  649. end;
  650. {****************************************************************************
  651. TAI
  652. ****************************************************************************}
  653. constructor tai.Create;
  654. begin
  655. {$ifndef NOOPT}
  656. optinfo:=nil;
  657. {$endif NOOPT}
  658. end;
  659. constructor tai.ppuload(t:taitype;ppufile:tcompilerppufile);
  660. begin
  661. typ:=t;
  662. {$ifndef NOOPT}
  663. optinfo:=nil;
  664. {$endif}
  665. end;
  666. procedure tai.ppuwrite(ppufile:tcompilerppufile);
  667. begin
  668. end;
  669. procedure tai.buildderefimpl;
  670. begin
  671. end;
  672. procedure tai.derefimpl;
  673. begin
  674. end;
  675. {****************************************************************************
  676. TAILINEINFO
  677. ****************************************************************************}
  678. constructor tailineinfo.create;
  679. begin
  680. inherited create;
  681. fileinfo:=current_filepos;
  682. end;
  683. constructor tailineinfo.ppuload(t:taitype;ppufile:tcompilerppufile);
  684. begin
  685. inherited ppuload(t,ppufile);
  686. ppufile.getposinfo(fileinfo);
  687. end;
  688. procedure tailineinfo.ppuwrite(ppufile:tcompilerppufile);
  689. begin
  690. inherited ppuwrite(ppufile);
  691. ppufile.putposinfo(fileinfo);
  692. end;
  693. {****************************************************************************
  694. TAI_SIMPLE
  695. ****************************************************************************}
  696. constructor tai_simple.create(_typ : taitype);
  697. begin
  698. inherited create;
  699. typ:=_typ;
  700. end;
  701. {****************************************************************************
  702. TAI_SECTION
  703. ****************************************************************************}
  704. constructor tai_section.Create(Asectype:TAsmSectiontype;Aname:string;Aalign:byte;Asecorder:TasmSectionorder=secorder_default);
  705. begin
  706. inherited Create;
  707. typ:=ait_section;
  708. sectype:=asectype;
  709. secalign:=Aalign;
  710. secorder:=Asecorder;
  711. name:=stringdup(Aname);
  712. sec:=nil;
  713. end;
  714. constructor tai_section.ppuload(t:taitype;ppufile:tcompilerppufile);
  715. begin
  716. inherited ppuload(t,ppufile);
  717. sectype:=TAsmSectiontype(ppufile.getbyte);
  718. secalign:=ppufile.getbyte;
  719. name:=stringdup(ppufile.getstring);
  720. sec:=nil;
  721. end;
  722. destructor tai_section.Destroy;
  723. begin
  724. stringdispose(name);
  725. end;
  726. procedure tai_section.ppuwrite(ppufile:tcompilerppufile);
  727. begin
  728. inherited ppuwrite(ppufile);
  729. ppufile.putbyte(byte(sectype));
  730. ppufile.putbyte(secalign);
  731. ppufile.putstring(name^);
  732. end;
  733. {****************************************************************************
  734. TAI_DATABLOCK
  735. ****************************************************************************}
  736. constructor tai_datablock.Create(const _name : string;_size : aint);
  737. begin
  738. inherited Create;
  739. typ:=ait_datablock;
  740. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,AT_DATA);
  741. { keep things aligned }
  742. if _size<=0 then
  743. _size:=sizeof(aint);
  744. size:=_size;
  745. is_global:=false;
  746. end;
  747. constructor tai_datablock.Create_global(const _name : string;_size : aint);
  748. begin
  749. inherited Create;
  750. typ:=ait_datablock;
  751. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,AT_DATA);
  752. { keep things aligned }
  753. if _size<=0 then
  754. _size:=sizeof(aint);
  755. size:=_size;
  756. is_global:=true;
  757. end;
  758. constructor tai_datablock.ppuload(t:taitype;ppufile:tcompilerppufile);
  759. begin
  760. inherited Create;
  761. sym:=ppufile.getasmsymbol;
  762. size:=ppufile.getaint;
  763. is_global:=boolean(ppufile.getbyte);
  764. end;
  765. procedure tai_datablock.ppuwrite(ppufile:tcompilerppufile);
  766. begin
  767. inherited ppuwrite(ppufile);
  768. ppufile.putasmsymbol(sym);
  769. ppufile.putaint(size);
  770. ppufile.putbyte(byte(is_global));
  771. end;
  772. procedure tai_datablock.derefimpl;
  773. begin
  774. end;
  775. {****************************************************************************
  776. TAI_SYMBOL
  777. ****************************************************************************}
  778. constructor tai_symbol.Create(_sym:tasmsymbol;siz:longint);
  779. begin
  780. inherited Create;
  781. typ:=ait_symbol;
  782. sym:=_sym;
  783. size:=siz;
  784. sym.bind:=AB_LOCAL;
  785. is_global:=false;
  786. end;
  787. constructor tai_symbol.Create_global(_sym:tasmsymbol;siz:longint);
  788. begin
  789. inherited Create;
  790. typ:=ait_symbol;
  791. sym:=_sym;
  792. size:=siz;
  793. sym.bind:=AB_GLOBAL;
  794. is_global:=true;
  795. end;
  796. constructor tai_symbol.Createname(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  797. begin
  798. inherited Create;
  799. typ:=ait_symbol;
  800. sym:=current_asmdata.DefineAsmSymbol(_name,AB_LOCAL,_symtyp);
  801. size:=siz;
  802. is_global:=false;
  803. end;
  804. constructor tai_symbol.Createname_global(const _name : string;_symtyp:Tasmsymtype;siz:longint);
  805. begin
  806. inherited Create;
  807. typ:=ait_symbol;
  808. sym:=current_asmdata.DefineAsmSymbol(_name,AB_GLOBAL,_symtyp);
  809. size:=siz;
  810. is_global:=true;
  811. end;
  812. constructor tai_symbol.ppuload(t:taitype;ppufile:tcompilerppufile);
  813. begin
  814. inherited ppuload(t,ppufile);
  815. sym:=ppufile.getasmsymbol;
  816. size:=ppufile.getlongint;
  817. is_global:=boolean(ppufile.getbyte);
  818. end;
  819. procedure tai_symbol.ppuwrite(ppufile:tcompilerppufile);
  820. begin
  821. inherited ppuwrite(ppufile);
  822. ppufile.putasmsymbol(sym);
  823. ppufile.putlongint(size);
  824. ppufile.putbyte(byte(is_global));
  825. end;
  826. procedure tai_symbol.derefimpl;
  827. begin
  828. end;
  829. {****************************************************************************
  830. TAI_SYMBOL_END
  831. ****************************************************************************}
  832. constructor tai_symbol_end.Create(_sym:tasmsymbol);
  833. begin
  834. inherited Create;
  835. typ:=ait_symbol_end;
  836. sym:=_sym;
  837. end;
  838. constructor tai_symbol_end.Createname(const _name : string);
  839. begin
  840. inherited Create;
  841. typ:=ait_symbol_end;
  842. sym:=current_asmdata.RefAsmSymbol(_name);
  843. end;
  844. constructor tai_symbol_end.ppuload(t:taitype;ppufile:tcompilerppufile);
  845. begin
  846. inherited ppuload(t,ppufile);
  847. sym:=ppufile.getasmsymbol;
  848. end;
  849. procedure tai_symbol_end.ppuwrite(ppufile:tcompilerppufile);
  850. begin
  851. inherited ppuwrite(ppufile);
  852. ppufile.putasmsymbol(sym);
  853. end;
  854. procedure tai_symbol_end.derefimpl;
  855. begin
  856. end;
  857. {****************************************************************************
  858. TAI_SYMBOL_END
  859. ****************************************************************************}
  860. constructor tai_directive.Create(_directive:TAsmDirective;const _name:string);
  861. begin
  862. inherited Create;
  863. typ:=ait_directive;
  864. name:=stringdup(_name);
  865. directive:=_directive;
  866. end;
  867. destructor tai_directive.Destroy;
  868. begin
  869. stringdispose(name);
  870. end;
  871. constructor tai_directive.ppuload(t:taitype;ppufile:tcompilerppufile);
  872. begin
  873. inherited ppuload(t,ppufile);
  874. name:=stringdup(ppufile.getstring);
  875. directive:=TAsmDirective(ppufile.getbyte);
  876. end;
  877. procedure tai_directive.ppuwrite(ppufile:tcompilerppufile);
  878. begin
  879. inherited ppuwrite(ppufile);
  880. ppufile.putstring(name^);
  881. ppufile.putbyte(byte(directive));
  882. end;
  883. {****************************************************************************
  884. TAI_CONST
  885. ****************************************************************************}
  886. constructor tai_const.Create(_typ:taiconst_type;_value : int64);
  887. begin
  888. inherited Create;
  889. typ:=ait_const;
  890. consttype:=_typ;
  891. value:=_value;
  892. sym:=nil;
  893. endsym:=nil;
  894. end;
  895. constructor tai_const.Create_128bit(_value : int64);
  896. begin
  897. inherited Create;
  898. typ:=ait_const;
  899. consttype:=aitconst_128bit;
  900. value:=_value;
  901. sym:=nil;
  902. endsym:=nil;
  903. end;
  904. constructor tai_const.Create_64bit(_value : int64);
  905. begin
  906. inherited Create;
  907. typ:=ait_const;
  908. consttype:=aitconst_64bit;
  909. value:=_value;
  910. sym:=nil;
  911. endsym:=nil;
  912. end;
  913. constructor tai_const.Create_32bit(_value : longint);
  914. begin
  915. inherited Create;
  916. typ:=ait_const;
  917. consttype:=aitconst_32bit;
  918. value:=_value;
  919. sym:=nil;
  920. endsym:=nil;
  921. end;
  922. constructor tai_const.Create_16bit(_value : word);
  923. begin
  924. inherited Create;
  925. typ:=ait_const;
  926. consttype:=aitconst_16bit;
  927. value:=_value;
  928. sym:=nil;
  929. endsym:=nil;
  930. end;
  931. constructor tai_const.Create_8bit(_value : byte);
  932. begin
  933. inherited Create;
  934. typ:=ait_const;
  935. consttype:=aitconst_8bit;
  936. value:=_value;
  937. sym:=nil;
  938. endsym:=nil;
  939. end;
  940. constructor tai_const.Create_sleb128bit(_value : int64);
  941. begin
  942. inherited Create;
  943. typ:=ait_const;
  944. consttype:=aitconst_sleb128bit;
  945. value:=_value;
  946. sym:=nil;
  947. endsym:=nil;
  948. end;
  949. constructor tai_const.Create_uleb128bit(_value : qword);
  950. begin
  951. inherited Create;
  952. typ:=ait_const;
  953. consttype:=aitconst_uleb128bit;
  954. value:=int64(_value);
  955. sym:=nil;
  956. endsym:=nil;
  957. end;
  958. constructor tai_const.Create_aint(_value : aint);
  959. begin
  960. inherited Create;
  961. typ:=ait_const;
  962. consttype:=aitconst_aint;
  963. value:=_value;
  964. sym:=nil;
  965. endsym:=nil;
  966. end;
  967. constructor tai_const.Create_type_sym(_typ:taiconst_type;_sym:tasmsymbol);
  968. begin
  969. inherited Create;
  970. typ:=ait_const;
  971. consttype:=_typ;
  972. sym:=_sym;
  973. endsym:=nil;
  974. value:=0;
  975. { update sym info }
  976. if assigned(sym) then
  977. sym.increfs;
  978. end;
  979. constructor tai_const.Create_sym(_sym:tasmsymbol);
  980. begin
  981. inherited Create;
  982. typ:=ait_const;
  983. consttype:=aitconst_ptr;
  984. { sym is allowed to be nil, this is used to write nil pointers }
  985. sym:=_sym;
  986. endsym:=nil;
  987. value:=0;
  988. { update sym info }
  989. if assigned(sym) then
  990. sym.increfs;
  991. end;
  992. constructor tai_const.Create_sym_offset(_sym:tasmsymbol;ofs:aint);
  993. begin
  994. inherited Create;
  995. typ:=ait_const;
  996. consttype:=aitconst_ptr;
  997. if not assigned(_sym) then
  998. internalerror(200404121);
  999. sym:=_sym;
  1000. endsym:=nil;
  1001. value:=ofs;
  1002. { update sym info }
  1003. sym.increfs;
  1004. end;
  1005. constructor tai_const.Create_rel_sym(_typ:taiconst_type;_sym,_endsym:tasmsymbol);
  1006. begin
  1007. inherited Create;
  1008. typ:=ait_const;
  1009. consttype:=_typ;
  1010. sym:=_sym;
  1011. endsym:=_endsym;
  1012. value:=0;
  1013. { update sym info }
  1014. sym.increfs;
  1015. endsym.increfs;
  1016. end;
  1017. constructor tai_const.Create_rva_sym(_sym:tasmsymbol);
  1018. begin
  1019. inherited Create;
  1020. typ:=ait_const;
  1021. consttype:=aitconst_rva_symbol;
  1022. sym:=_sym;
  1023. endsym:=nil;
  1024. value:=0;
  1025. { update sym info }
  1026. sym.increfs;
  1027. end;
  1028. constructor tai_const.Create_indirect_sym(_sym:tasmsymbol);
  1029. begin
  1030. inherited Create;
  1031. typ:=ait_const;
  1032. consttype:=aitconst_indirect_symbol;
  1033. sym:=_sym;
  1034. endsym:=nil;
  1035. value:=0;
  1036. { update sym info }
  1037. sym.increfs;
  1038. end;
  1039. constructor tai_const.Createname(const name:string;ofs:aint);
  1040. begin
  1041. inherited Create;
  1042. typ:=ait_const;
  1043. consttype:=aitconst_ptr;
  1044. sym:=current_asmdata.RefAsmSymbol(name);
  1045. endsym:=nil;
  1046. value:=ofs;
  1047. { update sym info }
  1048. sym.increfs;
  1049. end;
  1050. constructor tai_const.Createname_rva(const name:string);
  1051. begin
  1052. inherited Create;
  1053. typ:=ait_const;
  1054. consttype:=aitconst_rva_symbol;
  1055. sym:=current_asmdata.RefAsmSymbol(name);
  1056. endsym:=nil;
  1057. value:=0;
  1058. { update sym info }
  1059. sym.increfs;
  1060. end;
  1061. constructor tai_const.ppuload(t:taitype;ppufile:tcompilerppufile);
  1062. begin
  1063. inherited ppuload(t,ppufile);
  1064. consttype:=taiconst_type(ppufile.getbyte);
  1065. sym:=ppufile.getasmsymbol;
  1066. endsym:=ppufile.getasmsymbol;
  1067. value:=ppufile.getint64;
  1068. end;
  1069. procedure tai_const.ppuwrite(ppufile:tcompilerppufile);
  1070. begin
  1071. inherited ppuwrite(ppufile);
  1072. ppufile.putbyte(byte(consttype));
  1073. ppufile.putasmsymbol(sym);
  1074. ppufile.putasmsymbol(endsym);
  1075. ppufile.putint64(value);
  1076. end;
  1077. procedure tai_const.derefimpl;
  1078. begin
  1079. end;
  1080. function tai_const.getcopy:tlinkedlistitem;
  1081. begin
  1082. getcopy:=inherited getcopy;
  1083. { we need to increase the reference number }
  1084. sym.increfs;
  1085. if assigned(endsym) then
  1086. endsym.increfs;
  1087. end;
  1088. function tai_const.size:longint;
  1089. begin
  1090. case consttype of
  1091. aitconst_8bit :
  1092. result:=1;
  1093. aitconst_16bit :
  1094. result:=2;
  1095. aitconst_32bit :
  1096. result:=4;
  1097. aitconst_64bit :
  1098. result:=8;
  1099. aitconst_indirect_symbol,
  1100. aitconst_secrel32_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. function tai_cpu_abstract.Pass1(objdata:TObjData):longint;
  1915. begin
  1916. result:=0;
  1917. end;
  1918. procedure tai_cpu_abstract.Pass2(objdata:TObjData);
  1919. begin
  1920. end;
  1921. procedure tai_cpu_abstract.ppuloadoper(ppufile:tcompilerppufile;var o:toper);
  1922. begin
  1923. o.typ:=toptype(ppufile.getbyte);
  1924. o.ot:=ppufile.getlongint;
  1925. case o.typ of
  1926. top_reg :
  1927. ppufile.getdata(o.reg,sizeof(Tregister));
  1928. top_ref :
  1929. begin
  1930. new(o.ref);
  1931. {$ifdef x86}
  1932. ppufile.getdata(o.ref^.segment,sizeof(Tregister));
  1933. {$endif x86}
  1934. ppufile.getdata(o.ref^.base,sizeof(Tregister));
  1935. ppufile.getdata(o.ref^.index,sizeof(Tregister));
  1936. ppufile.getdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));;
  1937. o.ref^.scalefactor:=ppufile.getbyte;
  1938. o.ref^.offset:=ppufile.getaint;
  1939. o.ref^.symbol:=ppufile.getasmsymbol;
  1940. o.ref^.relsymbol:=ppufile.getasmsymbol;
  1941. end;
  1942. top_const :
  1943. o.val:=ppufile.getaint;
  1944. top_local :
  1945. begin
  1946. new(o.localoper);
  1947. with o.localoper^ do
  1948. begin
  1949. ppufile.getderef(localsymderef);
  1950. localsymofs:=ppufile.getaint;
  1951. localindexreg:=tregister(ppufile.getlongint);
  1952. localscale:=ppufile.getbyte;
  1953. localgetoffset:=(ppufile.getbyte<>0);
  1954. end;
  1955. end;
  1956. else
  1957. internalerror(2007010210);
  1958. end;
  1959. end;
  1960. procedure tai_cpu_abstract.ppuwriteoper(ppufile:tcompilerppufile;const o:toper);
  1961. begin
  1962. ppufile.putbyte(byte(o.typ));
  1963. ppufile.putlongint(o.ot);
  1964. case o.typ of
  1965. top_reg :
  1966. ppufile.putdata(o.reg,sizeof(Tregister));
  1967. top_ref :
  1968. begin
  1969. {$ifdef x86}
  1970. ppufile.putdata(o.ref^.segment,sizeof(Tregister));
  1971. {$endif x86}
  1972. ppufile.putdata(o.ref^.base,sizeof(Tregister));
  1973. ppufile.putdata(o.ref^.index,sizeof(Tregister));
  1974. ppufile.putdata(o.ref^.refaddr,sizeof(o.ref^.refaddr));
  1975. ppufile.putbyte(o.ref^.scalefactor);
  1976. ppufile.putaint(o.ref^.offset);
  1977. ppufile.putasmsymbol(o.ref^.symbol);
  1978. ppufile.putasmsymbol(o.ref^.relsymbol);
  1979. end;
  1980. top_const :
  1981. ppufile.putaint(o.val);
  1982. top_local :
  1983. begin
  1984. with o.localoper^ do
  1985. begin
  1986. ppufile.putderef(localsymderef);
  1987. ppufile.putaint(localsymofs);
  1988. ppufile.putlongint(longint(localindexreg));
  1989. ppufile.putbyte(localscale);
  1990. ppufile.putbyte(byte(localgetoffset));
  1991. end;
  1992. end;
  1993. else
  1994. internalerror(2007010211);
  1995. end;
  1996. end;
  1997. {****************************************************************************
  1998. tai_align_abstract
  1999. ****************************************************************************}
  2000. constructor tai_align_abstract.Create(b: byte);
  2001. begin
  2002. inherited Create;
  2003. typ:=ait_align;
  2004. if b in [1,2,4,8,16,32] then
  2005. aligntype := b
  2006. else
  2007. aligntype := 1;
  2008. fillsize:=0;
  2009. fillop:=0;
  2010. use_op:=false;
  2011. end;
  2012. constructor tai_align_abstract.Create_op(b: byte; _op: byte);
  2013. begin
  2014. inherited Create;
  2015. typ:=ait_align;
  2016. if b in [1,2,4,8,16,32] then
  2017. aligntype := b
  2018. else
  2019. aligntype := 1;
  2020. fillsize:=0;
  2021. fillop:=_op;
  2022. use_op:=true;
  2023. end;
  2024. constructor tai_align_abstract.Create_zeros(b: byte);
  2025. begin
  2026. inherited Create;
  2027. typ:=ait_align;
  2028. if b in [1,2,4,8,16,32] then
  2029. aligntype := b
  2030. else
  2031. aligntype := 1;
  2032. use_op:=true;
  2033. fillsize:=0;
  2034. fillop:=0;
  2035. end;
  2036. function tai_align_abstract.calculatefillbuf(var buf : tfillbuffer):pchar;
  2037. begin
  2038. if fillsize>sizeof(buf) then
  2039. internalerror(200404293);
  2040. fillchar(buf,high(buf),fillop);
  2041. calculatefillbuf:=pchar(@buf);
  2042. end;
  2043. constructor tai_align_abstract.ppuload(t:taitype;ppufile:tcompilerppufile);
  2044. begin
  2045. inherited ppuload(t,ppufile);
  2046. aligntype:=ppufile.getbyte;
  2047. fillsize:=0;
  2048. fillop:=ppufile.getbyte;
  2049. use_op:=boolean(ppufile.getbyte);
  2050. end;
  2051. procedure tai_align_abstract.ppuwrite(ppufile:tcompilerppufile);
  2052. begin
  2053. inherited ppuwrite(ppufile);
  2054. ppufile.putbyte(aligntype);
  2055. ppufile.putbyte(fillop);
  2056. ppufile.putbyte(byte(use_op));
  2057. end;
  2058. begin
  2059. cai_cpu:=tai_cpu_abstract;
  2060. cai_align:=tai_align_abstract;
  2061. end.