aasmcnst.pas 72 KB

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