aasmtai.pas 67 KB

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