aasmtai.pas 68 KB

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