aasmcnst.pas 89 KB

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