aasmtai.pas 71 KB

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