aasmtai.pas 67 KB

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