aasmcnst.pas 72 KB

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