aasmtai.pas 67 KB

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