aasmtai.pas 70 KB

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