aasmcnst.pas 58 KB

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