aasmcnst.pas 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524
  1. {
  2. Copyright (c) 2014 by Jonas Maebe, member of the Free Pascal development
  3. team
  4. This unit implements typed constant data elements at the assembler level
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit aasmcnst;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,globtype,constexp,
  23. aasmbase,aasmdata,aasmtai,
  24. symconst,symtype,symdef,symsym;
  25. type
  26. { typed const: integer/floating point/string/pointer/... const along with
  27. tdef info }
  28. ttypedconstkind = (tck_simple, tck_array, tck_record);
  29. { the type of the element and its def }
  30. tai_abstracttypedconst = class abstract (tai)
  31. private
  32. procedure setdef(def: tdef);
  33. protected
  34. fadetyp: ttypedconstkind;
  35. { the def of this element }
  36. fdef: tdef;
  37. public
  38. constructor create(_adetyp: ttypedconstkind; _def: tdef);
  39. property adetyp: ttypedconstkind read fadetyp;
  40. property def: tdef read fdef write setdef;
  41. end;
  42. { a simple data element; the value is stored as a tai }
  43. tai_simpletypedconst = class(tai_abstracttypedconst)
  44. private
  45. procedure setval(AValue: tai);
  46. protected
  47. fval: tai;
  48. public
  49. constructor create(_adetyp: ttypedconstkind; _def: tdef; _val: tai);
  50. property val: tai read fval write setval;
  51. end;
  52. { an aggregate data element (record or array). Values are stored as an
  53. array of tsimpledataelement. }
  54. tai_aggregatetypedconst = class(tai_abstracttypedconst)
  55. public type
  56. { iterator to walk over all individual items in the aggregate }
  57. tadeenumerator = class(tobject)
  58. private
  59. fvalues: tfplist;
  60. fvaluespos: longint;
  61. function getcurrent: tai_abstracttypedconst;
  62. public
  63. constructor create(data: tai_aggregatetypedconst);
  64. function movenext: boolean;
  65. procedure reset;
  66. property current: tai_abstracttypedconst read getcurrent;
  67. end;
  68. protected
  69. fvalues: tfplist;
  70. fisstring: boolean;
  71. { converts the existing data to a single tai_string }
  72. procedure convert_to_string;
  73. procedure add_to_string(strtai: tai_string; othertai: tai);
  74. public
  75. constructor create(_adetyp: ttypedconstkind; _fdef: tdef);
  76. function getenumerator: tadeenumerator;
  77. procedure addvalue(val: tai_abstracttypedconst);
  78. function valuecount: longint;
  79. procedure insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  80. procedure finish;
  81. destructor destroy; override;
  82. end;
  83. tasmlabofs = record
  84. lab: tasmlabel;
  85. ofs: asizeint;
  86. end;
  87. { flags for the finalisation of the typed const builder asmlist }
  88. ttcasmlistoption = (
  89. { the tasmsymbol is a tasmlabel }
  90. tcalo_is_lab,
  91. { start a new section (e.g., because we don't know the current section
  92. type) }
  93. tcalo_new_section,
  94. { this symbol is the start of a block of data that should be
  95. dead-stripable/smartlinkable; may imply starting a new section, but
  96. not necessarily (depends on what the platform requirements are) }
  97. tcalo_make_dead_strippable
  98. );
  99. ttcasmlistoptions = set of ttcasmlistoption;
  100. { information about aggregates we are parsing }
  101. taggregateinformation = class
  102. private
  103. function getcuroffset: asizeint;
  104. function getfieldoffset(l: longint): asizeint;
  105. protected
  106. { type of the aggregate }
  107. fdef: tdef;
  108. { type of the aggregate }
  109. ftyp: ttypedconstkind;
  110. { symtable entry of the previously emitted field in case of a
  111. record/object (nil if none emitted yet), used to insert alignment bytes
  112. if necessary for variant records and objects }
  113. fcurfield,
  114. { field corresponding to the data that will be emitted next in case of a
  115. record/object (nil if not set), used to handle variant records and
  116. objects }
  117. fnextfield: tfieldvarsym;
  118. { similar as the fcurfield/fnextfield above, but instead of fieldvarsyms
  119. these are indices in the symlist of a recorddef that correspond to
  120. fieldvarsyms. These are used only for non-variant records, simply
  121. traversing the fields in order. We could use the above method here as
  122. well, but to find the next field we'd always have to use
  123. symlist.indexof(fcurfield), which would be quite slow. These have -1 as
  124. value if they're not set }
  125. fcurindex,
  126. fnextindex: longint;
  127. { anonymous record that is being built as we add constant data }
  128. fanonrecord: boolean;
  129. property curindex: longint read fcurindex write fcurindex;
  130. property nextindex: longint read fnextindex write fnextindex;
  131. public
  132. constructor create(_def: tdef; _typ: ttypedconstkind); virtual;
  133. { calculated padding bytes for alignment if needed, and add the def of the
  134. next field in case we are constructing an anonymous record }
  135. function prepare_next_field(nextfielddef: tdef): asizeint;
  136. property def: tdef read fdef;
  137. property typ: ttypedconstkind read ftyp;
  138. property curfield: tfieldvarsym read fcurfield write fcurfield;
  139. property nextfield: tfieldvarsym read fnextfield write fnextfield;
  140. property fieldoffset[l: longint]: asizeint read getfieldoffset;
  141. property curoffset: asizeint read getcuroffset;
  142. property anonrecord: boolean read fanonrecord write fanonrecord;
  143. end;
  144. taggregateinformationclass = class of taggregateinformation;
  145. { Warning: never directly create a ttai_typedconstbuilder instance,
  146. instead create a cai_typedconstbuilder (this class can be overridden) }
  147. ttai_typedconstbuilder = class abstract
  148. { class type to use when creating new aggregate information instances }
  149. protected class var
  150. caggregateinformation: taggregateinformationclass;
  151. private
  152. function getcurragginfo: taggregateinformation;
  153. procedure set_next_field(AValue: tfieldvarsym);
  154. protected
  155. { temporary list in which all data is collected }
  156. fasmlist: tasmlist;
  157. { options for the final asmlist }
  158. foptions: ttcasmlistoptions;
  159. { while queueing elements of a compound expression, this is the current
  160. offset in the top-level array/record }
  161. fqueue_offset: asizeint;
  162. { array of caggregateinformation instances }
  163. faggregateinformation: tfpobjectlist;
  164. { Support for generating data that is only referenced from the typed
  165. constant data that we are currently generated. Such data can all be put
  166. in the same dead-strippable unit, as it's either all included or none of
  167. it is included. This data can be spread over multiple kinds of sections
  168. though (e.g. rodata and rodata_no_rel), so per section keep track whether
  169. we already started a dead-strippable unit and if so, what the section
  170. name was (so that on platforms that perform the dead stripping based on
  171. sections, we put all data for one typed constant into a single section
  172. with the same name) }
  173. protected type
  174. tinternal_data_section_info = record
  175. secname: TSymStr;
  176. sectype: TAsmSectiontype;
  177. end;
  178. protected var
  179. { all internally generated data must be stored in the same list, as it must
  180. be consecutive (if it's spread over multiple lists, we don't know in
  181. which order they'll be concatenated) -> keep track of this list }
  182. finternal_data_asmlist: tasmlist;
  183. { kind of the last section we started in the finternal_data_asmlist, to
  184. avoid creating unnecessary section statements }
  185. finternal_data_current_section: TAsmSectiontype;
  186. { info about in which kinds of sections we have already emitted internal
  187. data, and what their names were }
  188. finternal_data_section_info: array of tinternal_data_section_info;
  189. { ensure that finalize_asmlist is called only once }
  190. fasmlist_finalized: boolean;
  191. { returns whether def must be handled as an aggregate on the current
  192. platform }
  193. function aggregate_kind(def: tdef): ttypedconstkind; virtual;
  194. { finalize the asmlist: add the necessary symbols etc }
  195. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); virtual;
  196. { called by the public emit_tai() routines to actually add the typed
  197. constant data; the public ones also take care of adding extra padding
  198. bytes etc (by calling this one) }
  199. procedure do_emit_tai(p: tai; def: tdef); virtual;
  200. { calls prepare_next_field() and adds the padding bytes in the current
  201. location }
  202. procedure pad_next_field(nextfielddef: tdef);
  203. { returns the index in finternal_data_section_info of the info for the
  204. section of type typ. Returns -1 if there is no such info yet }
  205. function get_internal_data_section_index(typ: TAsmSectiontype): longint;
  206. { get a start label for an internal data section (at the start of a
  207. potentially dead-strippable part) }
  208. function get_internal_data_section_start_label: tasmlabel; virtual;
  209. { get a label in the middle of an internal data section (no dead
  210. stripping) }
  211. function get_internal_data_section_internal_label: tasmlabel; virtual;
  212. { easy access to the top level aggregate information instance }
  213. property curagginfo: taggregateinformation read getcurragginfo;
  214. public
  215. constructor create(const options: ttcasmlistoptions); virtual;
  216. destructor destroy; override;
  217. public
  218. { returns a builder for generating data that is only referrenced by the
  219. typed constant date we are currently generating (e.g. string data for a
  220. pchar constant). Also returns the label that will be placed at the start
  221. of that data. list is the tasmlist to which the data will be added.
  222. secname can be empty to use a default }
  223. procedure start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  224. { finish a previously started internal data builder, including
  225. concatenating all generated data to the provided list and freeing the
  226. builder }
  227. procedure finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  228. { add a simple constant data element (p) to the typed constant.
  229. def is the type of the added value }
  230. procedure emit_tai(p: tai; def: tdef); virtual;
  231. { same as above, for a special case: when the def is a procvardef and we
  232. want to use it explicitly as a procdef (i.e., not as a record with a
  233. code and data pointer in case of a complex procvardef) }
  234. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); virtual;
  235. protected
  236. function emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel):tasmlabofs;
  237. procedure begin_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  238. procedure end_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  239. { when building an anonymous record, we cannot immediately insert the
  240. alignment before it in case it's nested, since we only know the required
  241. alignment once all fields have been inserted -> mark the location before
  242. the anonymous record, and insert the alignment once it's finished }
  243. procedure mark_anon_aggregate_alignment; virtual; abstract;
  244. procedure insert_marked_aggregate_alignment(def: tdef); virtual; abstract;
  245. public
  246. class function get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  247. { the datalist parameter specifies where the data for the string constant
  248. will be emitted (via an internal data builder) }
  249. function emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  250. function emit_unicodestring_const(datalist: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  251. { emits a tasmlabofs as returned by emit_*string_const }
  252. procedure emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);virtual;
  253. { emit a shortstring constant, and return its def }
  254. function emit_shortstring_const(const str: shortstring): tdef;
  255. { emit a guid constant }
  256. procedure emit_guid_const(const guid: tguid);
  257. { emit a procdef constant }
  258. procedure emit_procdef_const(pd: tprocdef);
  259. { emit an ordinal constant }
  260. procedure emit_ord_const(value: int64; def: tdef);
  261. { begin a potential aggregate type. Must be called for any type
  262. that consists of multiple tai constant data entries, or that
  263. represents an aggregate at the Pascal level (a record, a non-dynamic
  264. array, ... }
  265. procedure maybe_begin_aggregate(def: tdef);
  266. { end a potential aggregate type. Must be paired with every
  267. maybe_begin_aggregate }
  268. procedure maybe_end_aggregate(def: tdef);
  269. { similar as above, but in case
  270. a) it's definitely a record
  271. b) the def of the record should be automatically constructed based on
  272. the types of the emitted fields
  273. }
  274. function begin_anonymous_record(const optionalname: string; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef; virtual;
  275. function end_anonymous_record: trecorddef; virtual;
  276. { The next group of routines are for constructing complex expressions.
  277. While parsing a typed constant these operators are encountered from
  278. outer to inner, so that is also the order in which they should be
  279. added to the queue. Only one queue can be active at a time. }
  280. { Init the queue. Gives an internalerror if a queue was already active }
  281. procedure queue_init(todef: tdef); virtual;
  282. { queue an array/string indexing operation (performs all range checking,
  283. so it doesn't have to be duplicated in all descendents). }
  284. procedure queue_vecn(def: tdef; const index: tconstexprint); virtual;
  285. { queue a subscripting operation }
  286. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); virtual;
  287. { queue a type conversion operation }
  288. procedure queue_typeconvn(fromdef, todef: tdef); virtual;
  289. { queue an address taking operation }
  290. procedure queue_addrn(fromdef, todef: tdef); virtual;
  291. { finalise the queue (so a new one can be created) and flush the
  292. previously queued operations, applying them in reverse order on a...}
  293. { ... procdef }
  294. procedure queue_emit_proc(pd: tprocdef); virtual;
  295. { ... staticvarsym }
  296. procedure queue_emit_staticvar(vs: tstaticvarsym); virtual;
  297. { ... labelsym }
  298. procedure queue_emit_label(l: tlabelsym); virtual;
  299. { ... constsym }
  300. procedure queue_emit_const(cs: tconstsym); virtual;
  301. { ... asmsym/asmlabel }
  302. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); virtual;
  303. { ... an ordinal constant }
  304. procedure queue_emit_ordconst(value: int64; def: tdef); virtual;
  305. { finalize the internal asmlist (if necessary) and return it.
  306. This asmlist will be freed when the builder is destroyed, so add its
  307. contents to another list first. This property should only be accessed
  308. once all data has been added. }
  309. function get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  310. { returns the offset of the string data relative to ansi/unicode/widestring
  311. constant labels. On most platforms, this is 0 (with the header at a
  312. negative offset), but on some platforms such negative offsets are not
  313. supported this is equal to the header size }
  314. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; virtual;
  315. { set the fieldvarsym whose data we will emit next; needed
  316. in case of variant records, so we know which part of the variant gets
  317. initialised. Also in case of objects, because the fieldvarsyms are spread
  318. over the symtables of the entire inheritance tree }
  319. property next_field: tfieldvarsym write set_next_field;
  320. protected
  321. { this one always return the actual offset, called by the above (and
  322. overridden versions) }
  323. class function get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  324. end;
  325. ttai_typedconstbuilderclass = class of ttai_typedconstbuilder;
  326. tlowlevelaggregateinformation = class(taggregateinformation)
  327. protected
  328. fanonrecmarker: tai;
  329. public
  330. property anonrecmarker: tai read fanonrecmarker write fanonrecmarker;
  331. end;
  332. ttai_lowleveltypedconstbuilder = class(ttai_typedconstbuilder)
  333. protected
  334. procedure mark_anon_aggregate_alignment; override;
  335. procedure insert_marked_aggregate_alignment(def: tdef); override;
  336. public
  337. { set the default value for caggregateinformation (= tlowlevelaggregateinformation) }
  338. class constructor classcreate;
  339. end;
  340. var
  341. ctai_typedconstbuilder: ttai_typedconstbuilderclass;
  342. implementation
  343. uses
  344. verbose,globals,systems,widestr,
  345. symbase,symtable,defutil;
  346. {****************************************************************************
  347. taggregateinformation
  348. ****************************************************************************}
  349. function taggregateinformation.getcuroffset: asizeint;
  350. var
  351. field: tfieldvarsym;
  352. begin
  353. if assigned(curfield) then
  354. result:=curfield.fieldoffset+curfield.vardef.size
  355. else if curindex<>-1 then
  356. begin
  357. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[curindex]);
  358. result:=field.fieldoffset+field.vardef.size
  359. end
  360. else
  361. result:=0
  362. end;
  363. function taggregateinformation.getfieldoffset(l: longint): asizeint;
  364. var
  365. field: tfieldvarsym;
  366. begin
  367. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[l]);
  368. result:=field.fieldoffset;
  369. end;
  370. constructor taggregateinformation.create(_def: tdef; _typ: ttypedconstkind);
  371. begin
  372. fdef:=_def;
  373. ftyp:=_typ;
  374. fcurindex:=-1;
  375. fnextindex:=-1;
  376. end;
  377. function taggregateinformation.prepare_next_field(nextfielddef: tdef): asizeint;
  378. var
  379. currentoffset,nextoffset: asizeint;
  380. i: longint;
  381. begin
  382. { get the next field and its offset, and make that next field the current
  383. one }
  384. if assigned(nextfield) then
  385. begin
  386. nextoffset:=nextfield.fieldoffset;
  387. currentoffset:=curoffset;
  388. curfield:=nextfield;
  389. end
  390. else
  391. begin
  392. { must set nextfield for unions and objects, as we cannot
  393. automatically detect the "next" field in that case }
  394. if ((def.typ=recorddef) and
  395. trecorddef(def).isunion) or
  396. is_object(def) then
  397. internalerror(2014091202);
  398. { if we are constructing this record as data gets emitted, add a field
  399. for this data }
  400. if anonrecord then
  401. trecorddef(def).add_field_by_def(nextfielddef);
  402. { find next field }
  403. i:=curindex;
  404. repeat
  405. inc(i);
  406. until (tsym(tabstractrecorddef(def).symtable.symlist[i]).typ=fieldvarsym) and
  407. not(sp_static in tsym(tabstractrecorddef(def).symtable.symlist[i]).symoptions);
  408. nextoffset:=fieldoffset[i];
  409. currentoffset:=curoffset;
  410. curindex:=i;
  411. end;
  412. { need padding? }
  413. result:=nextoffset-currentoffset;
  414. end;
  415. {****************************************************************************
  416. tai_abstracttypedconst
  417. ****************************************************************************}
  418. procedure tai_abstracttypedconst.setdef(def: tdef);
  419. begin
  420. { should not be changed, rewrite the calling code if this happens }
  421. if assigned(fdef) then
  422. Internalerror(2014080203);
  423. fdef:=def;
  424. end;
  425. constructor tai_abstracttypedconst.create(_adetyp: ttypedconstkind; _def: tdef);
  426. begin
  427. inherited create;
  428. typ:=ait_typedconst;
  429. fadetyp:=_adetyp;
  430. fdef:=_def;
  431. end;
  432. {****************************************************************************
  433. tai_simpletypedconst
  434. ****************************************************************************}
  435. procedure tai_simpletypedconst.setval(AValue: tai);
  436. begin
  437. fval:=AValue;
  438. end;
  439. constructor tai_simpletypedconst.create(_adetyp: ttypedconstkind; _def: tdef; _val: tai);
  440. begin
  441. inherited create(_adetyp,_def);
  442. fval:=_val;
  443. end;
  444. {****************************************************************************
  445. tai_aggregatetypedconst.tadeenumerator
  446. ****************************************************************************}
  447. constructor tai_aggregatetypedconst.tadeenumerator.create(data: tai_aggregatetypedconst);
  448. begin
  449. fvalues:=data.fvalues;
  450. fvaluespos:=-1;
  451. end;
  452. function tai_aggregatetypedconst.tadeenumerator.getcurrent: tai_abstracttypedconst;
  453. begin
  454. result:=tai_abstracttypedconst(fvalues[fvaluespos]);
  455. end;
  456. function tai_aggregatetypedconst.tadeenumerator.movenext: boolean;
  457. begin
  458. if fvaluespos<pred(fvalues.count) then
  459. begin
  460. inc(fvaluespos);
  461. result:=true
  462. end
  463. else
  464. result:=false;
  465. end;
  466. procedure tai_aggregatetypedconst.tadeenumerator.reset;
  467. begin
  468. fvaluespos:=0
  469. end;
  470. {****************************************************************************
  471. tai_aggregatetypedconst
  472. ****************************************************************************}
  473. procedure tai_aggregatetypedconst.convert_to_string;
  474. var
  475. ai: tai_abstracttypedconst;
  476. newstr: tai_string;
  477. begin
  478. newstr:=tai_string.Create('');
  479. for ai in self do
  480. begin
  481. if ai.adetyp<>tck_simple then
  482. internalerror(2014070103);
  483. add_to_string(newstr,tai_simpletypedconst(ai).val);
  484. ai.free;
  485. end;
  486. fvalues.count:=0;
  487. { the "nil" def will be replaced with an array def of the appropriate
  488. size once we're finished adding data, so we don't create intermediate
  489. arraydefs all the time }
  490. fvalues.add(tai_simpletypedconst.create(tck_simple,nil,newstr));
  491. end;
  492. procedure tai_aggregatetypedconst.add_to_string(strtai: tai_string; othertai: tai);
  493. begin
  494. case othertai.typ of
  495. ait_string:
  496. begin
  497. strtai.str:=reallocmem(strtai.str,strtai.len+tai_string(othertai).len+1);
  498. { also copy null terminator }
  499. move(tai_string(othertai).str[0],strtai.str[strtai.len],tai_string(othertai).len+1);
  500. { the null terminator is not part of the string data }
  501. strtai.len:=strtai.len+tai_string(othertai).len;
  502. end;
  503. ait_const:
  504. begin
  505. if tai_const(othertai).size<>1 then
  506. internalerror(2014070101);
  507. strtai.str:=reallocmem(strtai.str,strtai.len+1);
  508. strtai.str[strtai.len]:=ansichar(tai_const(othertai).value);
  509. strtai.str[strtai.len+1]:=#0;
  510. inc(strtai.len);
  511. end;
  512. else
  513. internalerror(2014070102);
  514. end;
  515. end;
  516. constructor tai_aggregatetypedconst.create(_adetyp: ttypedconstkind; _fdef: tdef);
  517. begin
  518. inherited;
  519. fisstring:=false;
  520. fvalues:=tfplist.create;
  521. end;
  522. function tai_aggregatetypedconst.getenumerator: tadeenumerator;
  523. begin
  524. result:=tadeenumerator.create(self);
  525. end;
  526. procedure tai_aggregatetypedconst.addvalue(val: tai_abstracttypedconst);
  527. begin
  528. { merge string constants and ordinal constants added in an array of
  529. char, to unify the length and the string data }
  530. if fisstring or
  531. ((val.adetyp=tck_simple) and
  532. (tai_simpletypedconst(val).val.typ=ait_string)) then
  533. begin
  534. if not fisstring and
  535. (fvalues.count>0) then
  536. convert_to_string;
  537. fisstring:=true;
  538. case fvalues.count of
  539. 0: fvalues.add(val);
  540. 1:
  541. begin
  542. add_to_string(tai_string(tai_simpletypedconst(fvalues[0]).val),tai_simpletypedconst(val).val);
  543. val.free
  544. end
  545. else
  546. internalerror(2014070104);
  547. end;
  548. end
  549. else
  550. fvalues.add(val);
  551. end;
  552. function tai_aggregatetypedconst.valuecount: longint;
  553. begin
  554. result:=fvalues.count;
  555. end;
  556. procedure tai_aggregatetypedconst.insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  557. begin
  558. fvalues.insert(pos,val);
  559. end;
  560. procedure tai_aggregatetypedconst.finish;
  561. begin
  562. if fisstring then
  563. begin
  564. { set the def: an array of char with the same length as the string
  565. data }
  566. if fvalues.count<>1 then
  567. internalerror(2014070105);
  568. tai_simpletypedconst(fvalues[0]).fdef:=
  569. getarraydef(cansichartype,
  570. tai_string(tai_simpletypedconst(fvalues[0]).val).len);
  571. end;
  572. end;
  573. destructor tai_aggregatetypedconst.destroy;
  574. begin
  575. fvalues.free;
  576. inherited destroy;
  577. end;
  578. {*****************************************************************************
  579. ttai_typedconstbuilder
  580. *****************************************************************************}
  581. function ttai_typedconstbuilder.getcurragginfo: taggregateinformation;
  582. begin
  583. if assigned(faggregateinformation) and
  584. (faggregateinformation.count>0) then
  585. result:=taggregateinformation(faggregateinformation[faggregateinformation.count-1])
  586. else
  587. result:=nil;
  588. end;
  589. procedure ttai_typedconstbuilder.set_next_field(AValue: tfieldvarsym);
  590. var
  591. info: taggregateinformation;
  592. begin
  593. info:=curagginfo;
  594. if not assigned(info) then
  595. internalerror(2014091206);
  596. info.nextfield:=AValue;
  597. end;
  598. procedure ttai_typedconstbuilder.pad_next_field(nextfielddef: tdef);
  599. var
  600. fillbytes: asizeint;
  601. begin
  602. fillbytes:=curagginfo.prepare_next_field(nextfielddef);
  603. while fillbytes>0 do
  604. begin
  605. do_emit_tai(tai_const.create_8bit(0),u8inttype);
  606. dec(fillbytes);
  607. end;
  608. end;
  609. function ttai_typedconstbuilder.get_internal_data_section_index(typ: TAsmSectiontype): longint;
  610. begin
  611. { avoid wrong warning by -Oodfa }
  612. result:=-1;
  613. for result:=low(finternal_data_section_info) to high(finternal_data_section_info) do
  614. if finternal_data_section_info[result].sectype=typ then
  615. exit;
  616. result:=-1;
  617. end;
  618. function ttai_typedconstbuilder.get_internal_data_section_start_label: tasmlabel;
  619. begin
  620. { on Darwin, dead code/data stripping happens based on non-temporary
  621. labels (any label that doesn't start with "L" -- it doesn't have
  622. to be global) }
  623. if target_info.system in systems_darwin then
  624. current_asmdata.getstaticdatalabel(result)
  625. else if create_smartlink_library then
  626. current_asmdata.getglobaldatalabel(result)
  627. else
  628. current_asmdata.getlocaldatalabel(result);
  629. end;
  630. function ttai_typedconstbuilder.get_internal_data_section_internal_label: tasmlabel;
  631. begin
  632. if create_smartlink_library then
  633. { all labels need to be global in case they're in another object }
  634. current_asmdata.getglobaldatalabel(result)
  635. else
  636. { no special requirement for the label -> just get a local one }
  637. current_asmdata.getlocaldatalabel(result);
  638. end;
  639. function ttai_typedconstbuilder.aggregate_kind(def: tdef): ttypedconstkind;
  640. begin
  641. if (def.typ in [recorddef,filedef,variantdef]) or
  642. is_object(def) or
  643. ((def.typ=procvardef) and
  644. not tprocvardef(def).is_addressonly) then
  645. result:=tck_record
  646. else if ((def.typ=arraydef) and
  647. not is_dynamic_array(def)) or
  648. ((def.typ=setdef) and
  649. not is_smallset(def)) or
  650. is_shortstring(def) then
  651. result:=tck_array
  652. else
  653. result:=tck_simple;
  654. end;
  655. procedure ttai_typedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  656. var
  657. prelist: tasmlist;
  658. begin
  659. prelist:=tasmlist.create;
  660. { only now add items based on the symbolname, because it may be
  661. modified by the "section" specifier in case of a typed constant }
  662. if tcalo_make_dead_strippable in options then
  663. begin
  664. maybe_new_object_file(prelist);
  665. { we always need a new section here, since if we started a new
  666. object file then we have to say what the section is, and otherwise
  667. we need a new section because that's how the dead stripping works
  668. (except on Darwin, but that will be addressed in a future commit) }
  669. new_section(prelist,section,secname,const_align(alignment));
  670. end
  671. else if tcalo_new_section in options then
  672. new_section(prelist,section,secname,const_align(alignment))
  673. else
  674. prelist.concat(cai_align.Create(const_align(alignment)));
  675. if not(tcalo_is_lab in options) then
  676. if sym.bind=AB_GLOBAL then
  677. prelist.concat(tai_symbol.Create_Global(sym,0))
  678. else
  679. prelist.concat(tai_symbol.Create(sym,0))
  680. else
  681. prelist.concat(tai_label.Create(tasmlabel(sym)));
  682. { insert the symbol information before the data }
  683. fasmlist.insertlist(prelist);
  684. { end of the symbol }
  685. fasmlist.concat(tai_symbol_end.Createname(sym.name));
  686. { free the temporary list }
  687. prelist.free;
  688. end;
  689. procedure ttai_typedconstbuilder.do_emit_tai(p: tai; def: tdef);
  690. begin
  691. { by default we don't care about the type }
  692. fasmlist.concat(p);
  693. end;
  694. function ttai_typedconstbuilder.get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  695. begin
  696. if not fasmlist_finalized then
  697. begin
  698. finalize_asmlist(sym,def,section,secname,alignment,foptions);
  699. fasmlist_finalized:=true;
  700. end;
  701. result:=fasmlist;
  702. end;
  703. class function ttai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  704. begin
  705. { darwin's linker does not support negative offsets }
  706. if not(target_info.system in systems_darwin) then
  707. result:=0
  708. else
  709. result:=get_string_header_size(typ,winlikewidestring);
  710. end;
  711. class function ttai_typedconstbuilder.get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  712. const
  713. ansistring_header_size =
  714. { encoding }
  715. 2 +
  716. { elesize }
  717. 2 +
  718. {$ifdef cpu64bitaddr}
  719. { alignment }
  720. 4 +
  721. {$endif cpu64bitaddr}
  722. { reference count }
  723. sizeof(pint) +
  724. { length }
  725. sizeof(pint);
  726. unicodestring_header_size = ansistring_header_size;
  727. begin
  728. case typ of
  729. st_ansistring:
  730. result:=ansistring_header_size;
  731. st_unicodestring:
  732. result:=unicodestring_header_size;
  733. st_widestring:
  734. if winlikewidestring then
  735. result:=0
  736. else
  737. result:=unicodestring_header_size;
  738. else
  739. result:=0;
  740. end;
  741. end;
  742. constructor ttai_typedconstbuilder.create(const options: ttcasmlistoptions);
  743. begin
  744. inherited create;
  745. fasmlist:=tasmlist.create;
  746. foptions:=options;
  747. { queue is empty }
  748. fqueue_offset:=low(fqueue_offset);
  749. finternal_data_current_section:=sec_none;
  750. end;
  751. destructor ttai_typedconstbuilder.destroy;
  752. begin
  753. { the queue should have been flushed if it was used }
  754. if fqueue_offset<>low(fqueue_offset) then
  755. internalerror(2014062901);
  756. faggregateinformation.free;
  757. fasmlist.free;
  758. inherited destroy;
  759. end;
  760. procedure ttai_typedconstbuilder.start_internal_data_builder(list: tasmlist; sectype: TAsmSectiontype; const secname: TSymStr; out tcb: ttai_typedconstbuilder; out l: tasmlabel);
  761. var
  762. options: ttcasmlistoptions;
  763. foundsec: longint;
  764. begin
  765. options:=[tcalo_is_lab];
  766. { Add a section header if the previous one was different. We'll use the
  767. same section name in case multiple items are added to the same kind of
  768. section (rodata, rodata_no_rel, ...), so that everything will still
  769. end up in the same section even if there are multiple section headers }
  770. if finternal_data_current_section<>sectype then
  771. include(options,tcalo_new_section);
  772. finternal_data_current_section:=sectype;
  773. l:=nil;
  774. { did we already create a section of this type for the internal data of
  775. this builder? }
  776. foundsec:=get_internal_data_section_index(sectype);
  777. if foundsec=-1 then
  778. begin
  779. { we only need to start a dead-strippable section of data at the
  780. start of the first subsection of this kind for this block.
  781. exception: if dead stripping happens based on objects/libraries,
  782. then we only have to create a new object file for the first
  783. internal data section of any kind (all the rest will simply be put
  784. in the same object file) }
  785. if create_smartlink then
  786. begin
  787. if not create_smartlink_library or
  788. (length(finternal_data_section_info)=0) then
  789. include(options,tcalo_make_dead_strippable);
  790. { on Darwin, dead code/data stripping happens based on non-
  791. temporary labels (any label that doesn't start with "L" -- it
  792. doesn't have to be global) -> add a non-temporary lobel at the
  793. start of every kind of subsection created in this builder }
  794. if target_info.system in systems_darwin then
  795. l:=get_internal_data_section_start_label;
  796. end;
  797. foundsec:=length(finternal_data_section_info);
  798. setlength(finternal_data_section_info,foundsec+1);
  799. finternal_data_section_info[foundsec].sectype:=sectype;
  800. end;
  801. if not assigned(finternal_data_asmlist) and
  802. (cs_create_smart in current_settings.moduleswitches) then
  803. begin
  804. l:=get_internal_data_section_start_label;
  805. { the internal data list should only be assigned by this routine,
  806. the first time that an internal data block is started }
  807. if not assigned(list) or
  808. assigned(finternal_data_asmlist) then
  809. internalerror(2015032101);
  810. finternal_data_asmlist:=list;
  811. end
  812. { all internal data for this tcb must go to the same list (otherwise all
  813. data we want to add to the dead-strippable block is not guaranteed to
  814. be sequential and e.g. in the same object file in case of library-based
  815. dead stripping) }
  816. else if (assigned(finternal_data_asmlist) and
  817. (list<>finternal_data_asmlist)) or
  818. not assigned(list) then
  819. internalerror(2015032101);
  820. finternal_data_asmlist:=list;
  821. if not assigned(l) then
  822. l:=get_internal_data_section_internal_label;
  823. { first section of this kind -> set name }
  824. if finternal_data_section_info[foundsec].secname='' then
  825. if secname='' then
  826. finternal_data_section_info[foundsec].secname:=l.Name
  827. else
  828. finternal_data_section_info[foundsec].secname:=secname
  829. { if the name is specified multiple times, it must match }
  830. else if (secname<>'') and
  831. (finternal_data_section_info[foundsec].secname<>secname) then
  832. internalerror(2015032401);
  833. tcb:=ttai_typedconstbuilderclass(classtype).create(options);
  834. end;
  835. procedure ttai_typedconstbuilder.finish_internal_data_builder(var tcb: ttai_typedconstbuilder; l: tasmlabel; def: tdef; alignment: longint);
  836. begin
  837. finternal_data_asmlist.concatList(tcb.get_final_asmlist(l,def,
  838. finternal_data_current_section,
  839. finternal_data_section_info[get_internal_data_section_index(finternal_data_current_section)].secname,
  840. alignment));
  841. tcb.free;
  842. tcb:=nil;
  843. end;
  844. procedure ttai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  845. var
  846. kind: ttypedconstkind;
  847. info: taggregateinformation;
  848. begin
  849. { these elements can be aggregates themselves, e.g. a shortstring can
  850. be emitted as a series of bytes and char arrays }
  851. kind:=aggregate_kind(def);
  852. info:=curagginfo;
  853. if (kind<>tck_simple) and
  854. (not assigned(info) or
  855. (info.typ<>kind)) then
  856. internalerror(2014091001);
  857. { if we're emitting a record, handle the padding bytes, and in case of
  858. an anonymous record also add the next field }
  859. if assigned(info) then
  860. begin
  861. if ((info.def.typ=recorddef) or
  862. is_object(info.def)) and
  863. { may add support for these later }
  864. not is_packed_record_or_object(info.def) then
  865. pad_next_field(def);
  866. end;
  867. { emit the data }
  868. do_emit_tai(p,def);
  869. end;
  870. procedure ttai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  871. begin
  872. { nothing special by default, since we don't care about the type }
  873. emit_tai(p,pvdef);
  874. end;
  875. function ttai_typedconstbuilder.emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; var startlab: tasmlabel): tasmlabofs;
  876. var
  877. string_symofs: asizeint;
  878. charptrdef: tdef;
  879. elesize: word;
  880. begin
  881. result.lab:=startlab;
  882. result.ofs:=0;
  883. { pack the data, so that we don't add unnecessary null bytes after the
  884. constant string }
  885. begin_anonymous_record('$'+get_dynstring_rec_name(stringtype,false,len),1,1,1);
  886. string_symofs:=get_string_symofs(stringtype,false);
  887. { encoding }
  888. emit_tai(tai_const.create_16bit(encoding),u16inttype);
  889. inc(result.ofs,2);
  890. { element size }
  891. case stringtype of
  892. st_ansistring:
  893. begin
  894. elesize:=1;
  895. charptrdef:=charpointertype;
  896. end;
  897. st_unicodestring:
  898. begin
  899. elesize:=2;
  900. charptrdef:=widecharpointertype;
  901. end
  902. else
  903. internalerror(2014080401);
  904. end;
  905. emit_tai(tai_const.create_16bit(elesize),u16inttype);
  906. inc(result.ofs,2);
  907. {$ifdef cpu64bitaddr}
  908. { dummy for alignment }
  909. emit_tai(tai_const.create_32bit(0),u32inttype);
  910. inc(result.ofs,4);
  911. {$endif cpu64bitaddr}
  912. emit_tai(tai_const.create_pint(-1),ptrsinttype);
  913. inc(result.ofs,sizeof(pint));
  914. emit_tai(tai_const.create_pint(len),ptrsinttype);
  915. inc(result.ofs,sizeof(pint));
  916. if string_symofs=0 then
  917. begin
  918. { results in slightly more efficient code }
  919. emit_tai(tai_label.create(result.lab),charptrdef);
  920. result.ofs:=0;
  921. { create new label of the same kind (including whether or not the
  922. name starts with target_asm.labelprefix in case it's AB_LOCAL,
  923. so we keep the difference depending on whether the original was
  924. allocated via getstatic/getlocal/getglobal datalabel) }
  925. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  926. end;
  927. { sanity check }
  928. if result.ofs<>string_symofs then
  929. internalerror(2012051701);
  930. end;
  931. procedure ttai_typedconstbuilder.begin_aggregate_internal(def: tdef; anonymous: boolean);
  932. var
  933. info: taggregateinformation;
  934. tck: ttypedconstkind;
  935. begin
  936. tck:=aggregate_kind(def);
  937. if tck=tck_simple then
  938. exit;
  939. if not assigned(faggregateinformation) then
  940. faggregateinformation:=tfpobjectlist.create
  941. { if we're starting an anonymous record, we can't align it yet because
  942. the alignment depends on the fields that will be added -> we'll do
  943. it at the end }
  944. else if not anonymous then
  945. begin
  946. { add padding if necessary, and update the current field/offset }
  947. info:=curagginfo;
  948. if is_record(curagginfo.def) or
  949. is_object(curagginfo.def) then
  950. pad_next_field(def);
  951. end
  952. { if this is the outer record, no padding is required; the alignment
  953. has to be specified explicitly in that case via get_final_asmlist() }
  954. else if assigned(curagginfo) and
  955. (curagginfo.def.typ=recorddef) then
  956. { mark where we'll have to insert the padding bytes at the end }
  957. mark_anon_aggregate_alignment;
  958. info:=caggregateinformation.create(def,aggregate_kind(def));
  959. faggregateinformation.add(info);
  960. end;
  961. procedure ttai_typedconstbuilder.end_aggregate_internal(def: tdef; anonymous: boolean);
  962. var
  963. info: taggregateinformation;
  964. fillbytes: asizeint;
  965. tck: ttypedconstkind;
  966. begin
  967. tck:=aggregate_kind(def);
  968. if tck=tck_simple then
  969. exit;
  970. info:=curagginfo;
  971. if not assigned(info) then
  972. internalerror(2014091002);
  973. if def<>info.def then
  974. internalerror(2014091205);
  975. { add tail padding if necessary }
  976. if (is_record(def) or
  977. is_object(def)) and
  978. not is_packed_record_or_object(def) then
  979. begin
  980. fillbytes:=def.size-info.curoffset;
  981. while fillbytes>0 do
  982. begin
  983. do_emit_tai(Tai_const.Create_8bit(0),u8inttype);
  984. dec(fillbytes)
  985. end;
  986. end;
  987. { pop and free the information }
  988. faggregateinformation.count:=faggregateinformation.count-1;
  989. info.free;
  990. end;
  991. class function ttai_typedconstbuilder.get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  992. begin
  993. case typ of
  994. st_ansistring:
  995. result:='ansistrrec';
  996. st_unicodestring,
  997. st_widestring:
  998. if (typ=st_unicodestring) or
  999. not winlike then
  1000. result:='unicodestrrec'
  1001. else
  1002. result:='widestrrec';
  1003. else
  1004. internalerror(2014080402);
  1005. end;
  1006. result:=result+tostr(len);
  1007. end;
  1008. function ttai_typedconstbuilder.emit_ansistring_const(datalist: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding): tasmlabofs;
  1009. var
  1010. s: PChar;
  1011. startlab: tasmlabel;
  1012. ansistrrecdef: trecorddef;
  1013. datadef: tdef;
  1014. datatcb: ttai_typedconstbuilder;
  1015. begin
  1016. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1017. result:=datatcb.emit_string_const_common(st_ansistring,len,encoding,startlab);
  1018. getmem(s,len+1);
  1019. move(data^,s^,len);
  1020. s[len]:=#0;
  1021. { terminating zero included }
  1022. datadef:=getarraydef(cansichartype,len+1);
  1023. datatcb.maybe_begin_aggregate(datadef);
  1024. datatcb.emit_tai(tai_string.create_pchar(s,len+1),datadef);
  1025. datatcb.maybe_end_aggregate(datadef);
  1026. ansistrrecdef:=datatcb.end_anonymous_record;
  1027. finish_internal_data_builder(datatcb,startlab,ansistrrecdef,const_align(sizeof(pointer)));
  1028. end;
  1029. function ttai_typedconstbuilder.emit_unicodestring_const(datalist: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  1030. var
  1031. i, strlength: longint;
  1032. string_symofs: asizeint;
  1033. startlab: tasmlabel;
  1034. datadef: tdef;
  1035. uniwidestrrecdef: trecorddef;
  1036. datatcb: ttai_typedconstbuilder;
  1037. begin
  1038. start_internal_data_builder(datalist,sec_rodata_norel,'',datatcb,startlab);
  1039. strlength:=getlengthwidestring(pcompilerwidestring(data));
  1040. if winlike then
  1041. begin
  1042. result.lab:=startlab;
  1043. datatcb.begin_anonymous_record('$'+get_dynstring_rec_name(st_widestring,true,strlength),
  1044. 4,
  1045. targetinfos[target_info.system]^.alignment.recordalignmin,
  1046. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  1047. datatcb.emit_tai(Tai_const.Create_32bit(strlength*cwidechartype.size),s32inttype);
  1048. { can we optimise by placing the string constant label at the
  1049. required offset? }
  1050. string_symofs:=get_string_symofs(st_widestring,true);
  1051. if string_symofs=0 then
  1052. begin
  1053. { yes }
  1054. datatcb.emit_tai(Tai_label.Create(result.lab),widecharpointertype);
  1055. { allocate a separate label for the start of the data (see
  1056. emit_string_const_common() for explanation) }
  1057. startlab:=tasmlabel.create(current_asmdata.AsmSymbolDict,startlab.name+'$strlab',startlab.bind,startlab.typ);
  1058. end
  1059. else
  1060. internalerror(2015031502);
  1061. result.ofs:=string_symofs;
  1062. end
  1063. else
  1064. begin
  1065. result:=datatcb.emit_string_const_common(st_unicodestring,strlength,encoding,startlab);
  1066. end;
  1067. if cwidechartype.size = 2 then
  1068. begin
  1069. datadef:=getarraydef(cwidechartype,strlength+1);
  1070. datatcb.maybe_begin_aggregate(datadef);
  1071. for i:=0 to strlength-1 do
  1072. datatcb.emit_tai(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]),cwidechartype);
  1073. { ending #0 }
  1074. datatcb.emit_tai(Tai_const.Create_16bit(0),cwidechartype);
  1075. datatcb.maybe_end_aggregate(datadef);
  1076. uniwidestrrecdef:=datatcb.end_anonymous_record;
  1077. end
  1078. else
  1079. { code generation for other sizes must be written }
  1080. internalerror(200904271);
  1081. finish_internal_data_builder(datatcb,startlab,datadef,const_align(sizeof(pint)));
  1082. end;
  1083. procedure ttai_typedconstbuilder.emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  1084. begin
  1085. emit_tai(Tai_const.Create_sym_offset(ll.lab,ll.ofs),charptrdef);
  1086. end;
  1087. function ttai_typedconstbuilder.emit_shortstring_const(const str: shortstring): tdef;
  1088. begin
  1089. { we use an arraydef instead of a shortstringdef, because we don't have
  1090. functionality in place yet to reuse shortstringdefs of the same length
  1091. and neither the lowlevel nor the llvm typedconst builder cares about
  1092. this difference }
  1093. result:=getarraydef(cansichartype,length(str)+1);
  1094. maybe_begin_aggregate(result);
  1095. emit_tai(Tai_const.Create_8bit(length(str)),u8inttype);
  1096. if str<>'' then
  1097. emit_tai(Tai_string.Create(str),getarraydef(cansichartype,length(str)));
  1098. maybe_end_aggregate(result);
  1099. end;
  1100. procedure ttai_typedconstbuilder.emit_guid_const(const guid: tguid);
  1101. var
  1102. i: longint;
  1103. begin
  1104. maybe_begin_aggregate(rec_tguid);
  1105. { variant record -> must specify which fields get initialised }
  1106. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[0]);
  1107. emit_tai(Tai_const.Create_32bit(longint(guid.D1)),u32inttype);
  1108. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[1]);
  1109. emit_tai(Tai_const.Create_16bit(guid.D2),u16inttype);
  1110. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[2]);
  1111. emit_tai(Tai_const.Create_16bit(guid.D3),u16inttype);
  1112. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[3]);
  1113. { the array }
  1114. maybe_begin_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  1115. for i:=Low(guid.D4) to High(guid.D4) do
  1116. emit_tai(Tai_const.Create_8bit(guid.D4[i]),u8inttype);
  1117. maybe_end_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  1118. maybe_end_aggregate(rec_tguid);
  1119. end;
  1120. procedure ttai_typedconstbuilder.emit_procdef_const(pd: tprocdef);
  1121. begin
  1122. emit_tai(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0),pd.getcopyas(procvardef,pc_address_only));
  1123. end;
  1124. procedure ttai_typedconstbuilder.emit_ord_const(value: int64; def: tdef);
  1125. begin
  1126. case def.size of
  1127. 1:
  1128. emit_tai(Tai_const.Create_8bit(byte(value)),def);
  1129. 2:
  1130. emit_tai(Tai_const.Create_16bit(word(value)),def);
  1131. 4:
  1132. emit_tai(Tai_const.Create_32bit(longint(value)),def);
  1133. 8:
  1134. emit_tai(Tai_const.Create_64bit(value),def);
  1135. else
  1136. internalerror(2014100501);
  1137. end;
  1138. end;
  1139. procedure ttai_typedconstbuilder.maybe_begin_aggregate(def: tdef);
  1140. begin
  1141. begin_aggregate_internal(def,false);
  1142. end;
  1143. procedure ttai_typedconstbuilder.maybe_end_aggregate(def: tdef);
  1144. begin
  1145. end_aggregate_internal(def,false);
  1146. end;
  1147. function ttai_typedconstbuilder.begin_anonymous_record(const optionalname: string; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  1148. var
  1149. anonrecorddef: trecorddef;
  1150. srsym: tsym;
  1151. srsymtable: tsymtable;
  1152. found: boolean;
  1153. begin
  1154. { if the name is specified, we create a typesym with that name in order
  1155. to ensure we can find it again later with that name -> reuse here as
  1156. well if possible (and that also avoids duplicate type name issues) }
  1157. if optionalname<>'' then
  1158. begin
  1159. if optionalname[1]='$' then
  1160. found:=searchsym_type(copy(optionalname,2,length(optionalname)),srsym,srsymtable)
  1161. else
  1162. found:=searchsym_type(optionalname,srsym,srsymtable);
  1163. if found then
  1164. begin
  1165. if ttypesym(srsym).typedef.typ<>recorddef then
  1166. internalerror(2014091207);
  1167. result:=trecorddef(ttypesym(srsym).typedef);
  1168. maybe_begin_aggregate(result);
  1169. exit;
  1170. end;
  1171. end;
  1172. { create skeleton def }
  1173. anonrecorddef:=crecorddef.create_global_internal(optionalname,packrecords,recordalignmin,maxcrecordalign);
  1174. { generic aggregate housekeeping }
  1175. begin_aggregate_internal(anonrecorddef,true);
  1176. { mark as anonymous record }
  1177. curagginfo.anonrecord:=true;
  1178. { in case a descendent wants to do something with the anonrecorddef too }
  1179. result:=anonrecorddef;
  1180. end;
  1181. function ttai_typedconstbuilder.end_anonymous_record: trecorddef;
  1182. var
  1183. info: taggregateinformation;
  1184. anonrecord: boolean;
  1185. begin
  1186. info:=curagginfo;
  1187. if not assigned(info) or
  1188. (info.def.typ<>recorddef) then
  1189. internalerror(2014080201);
  1190. result:=trecorddef(info.def);
  1191. { make a copy, as we need it after info has been freed by
  1192. maybe_end_aggregate(result) }
  1193. anonrecord:=info.anonrecord;
  1194. { finalise the record skeleton (all fields have been added already by
  1195. emit_tai()) -- anonrecord may not be set in case we reused an earlier
  1196. constructed def }
  1197. if anonrecord then
  1198. trecordsymtable(result.symtable).addalignmentpadding;
  1199. end_aggregate_internal(result,true);
  1200. if anonrecord and
  1201. assigned(curagginfo) and
  1202. (curagginfo.def.typ=recorddef) then
  1203. insert_marked_aggregate_alignment(result);
  1204. end;
  1205. procedure ttai_typedconstbuilder.queue_init(todef: tdef);
  1206. begin
  1207. { nested call to init? }
  1208. if fqueue_offset<>low(fqueue_offset) then
  1209. internalerror(2014062101);
  1210. fqueue_offset:=0;
  1211. end;
  1212. procedure ttai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  1213. var
  1214. elelen,
  1215. vecbase: asizeint;
  1216. v: tconstexprint;
  1217. begin
  1218. elelen:=1;
  1219. vecbase:=0;
  1220. case def.typ of
  1221. stringdef :
  1222. ;
  1223. arraydef :
  1224. begin
  1225. if not is_packed_array(def) then
  1226. begin
  1227. elelen:=tarraydef(def).elesize;
  1228. vecbase:=tarraydef(def).lowrange;
  1229. end
  1230. else
  1231. Message(parser_e_packed_dynamic_open_array);
  1232. end;
  1233. else
  1234. Message(parser_e_illegal_expression);
  1235. end;
  1236. { Prevent overflow }
  1237. v:=index-vecbase;
  1238. if (v<int64(low(fqueue_offset))) or (v>int64(high(fqueue_offset))) then
  1239. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(fqueue_offset)),tostr(high(fqueue_offset)));
  1240. if high(fqueue_offset)-fqueue_offset div elelen>v then
  1241. inc(fqueue_offset,elelen*v.svalue)
  1242. else
  1243. message3(type_e_range_check_error_bounds,tostr(index),tostr(vecbase),tostr(high(fqueue_offset)-fqueue_offset div elelen+vecbase))
  1244. end;
  1245. procedure ttai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  1246. begin
  1247. inc(fqueue_offset,vs.fieldoffset);
  1248. end;
  1249. procedure ttai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  1250. begin
  1251. { do nothing }
  1252. end;
  1253. procedure ttai_typedconstbuilder.queue_addrn(fromdef, todef: tdef);
  1254. begin
  1255. { do nothing }
  1256. end;
  1257. procedure ttai_typedconstbuilder.queue_emit_proc(pd: tprocdef);
  1258. begin
  1259. if fqueue_offset<>0 then
  1260. internalerror(2014092101);
  1261. emit_procdef_const(pd);
  1262. fqueue_offset:=low(fqueue_offset);
  1263. end;
  1264. procedure ttai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  1265. begin
  1266. { getpointerdef because we are emitting a pointer to the staticvarsym
  1267. data, not the data itself }
  1268. emit_tai(Tai_const.Createname(vs.mangledname,fqueue_offset),getpointerdef(vs.vardef));
  1269. fqueue_offset:=low(fqueue_offset);
  1270. end;
  1271. procedure ttai_typedconstbuilder.queue_emit_label(l: tlabelsym);
  1272. begin
  1273. emit_tai(Tai_const.Createname(l.mangledname,fqueue_offset),voidcodepointertype);
  1274. fqueue_offset:=low(fqueue_offset);
  1275. end;
  1276. procedure ttai_typedconstbuilder.queue_emit_const(cs: tconstsym);
  1277. begin
  1278. if cs.consttyp<>constresourcestring then
  1279. internalerror(2014062102);
  1280. if fqueue_offset<>0 then
  1281. internalerror(2014062103);
  1282. { warning: update if/when the type of resource strings changes }
  1283. emit_tai(Tai_const.Createname(make_mangledname('RESSTR',cs.owner,cs.name),AT_DATA,sizeof(pint)),cansistringtype);
  1284. fqueue_offset:=low(fqueue_offset);
  1285. end;
  1286. procedure ttai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  1287. begin
  1288. { getpointerdef, because "sym" represents the address of whatever the
  1289. data is }
  1290. def:=getpointerdef(def);
  1291. emit_tai(Tai_const.Create_sym_offset(sym,fqueue_offset),def);
  1292. fqueue_offset:=low(fqueue_offset);
  1293. end;
  1294. procedure ttai_typedconstbuilder.queue_emit_ordconst(value: int64; def: tdef);
  1295. begin
  1296. emit_ord_const(value,def);
  1297. fqueue_offset:=low(fqueue_offset);
  1298. end;
  1299. {****************************************************************************
  1300. tai_abstracttypedconst
  1301. ****************************************************************************}
  1302. class constructor ttai_lowleveltypedconstbuilder.classcreate;
  1303. begin
  1304. caggregateinformation:=tlowlevelaggregateinformation;
  1305. end;
  1306. procedure ttai_lowleveltypedconstbuilder.mark_anon_aggregate_alignment;
  1307. var
  1308. marker: tai_marker;
  1309. begin
  1310. marker:=tai_marker.Create(mark_position);
  1311. fasmlist.concat(marker);
  1312. tlowlevelaggregateinformation(curagginfo).anonrecmarker:=marker;
  1313. end;
  1314. procedure ttai_lowleveltypedconstbuilder.insert_marked_aggregate_alignment(def: tdef);
  1315. var
  1316. info: tlowlevelaggregateinformation;
  1317. fillbytes: asizeint;
  1318. begin
  1319. info:=tlowlevelaggregateinformation(curagginfo);
  1320. if not assigned(info.anonrecmarker) then
  1321. internalerror(2014091401);
  1322. fillbytes:=info.prepare_next_field(def);
  1323. while fillbytes>0 do
  1324. begin
  1325. fasmlist.insertafter(tai_const.create_8bit(0),info.anonrecmarker);
  1326. dec(fillbytes);
  1327. end;
  1328. fasmlist.remove(info.anonrecmarker);
  1329. info.anonrecmarker.free;
  1330. info.anonrecmarker:=nil;
  1331. end;
  1332. begin
  1333. ctai_typedconstbuilder:=ttai_lowleveltypedconstbuilder;
  1334. end.