aasmcnst.pas 71 KB

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