aasmcnst.pas 59 KB

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