aasmtai.pas 67 KB

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