aasmcnst.pas 56 KB

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