aasmcnst.pas 74 KB

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