aasmcnst.pas 79 KB

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