aasmcnst.pas 80 KB

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