aasmcnst.pas 71 KB

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