aasmtai.pas 70 KB

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