aasmtai.pas 67 KB

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