aasmcnst.pas 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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,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. protected
  45. fval: tai;
  46. public
  47. constructor create(_adetyp: ttypedconstkind; _def: tdef; _val: tai);
  48. property val: tai read fval;
  49. end;
  50. { an aggregate data element (record or array). Values are stored as an
  51. array of tsimpledataelement. }
  52. tai_aggregatetypedconst = class(tai_abstracttypedconst)
  53. public type
  54. { iterator to walk over all individual items in the aggregate }
  55. tadeenumerator = class(tobject)
  56. private
  57. fvalues: tfplist;
  58. fvaluespos: longint;
  59. function getcurrent: tai_abstracttypedconst;
  60. public
  61. constructor create(data: tai_aggregatetypedconst);
  62. function movenext: boolean;
  63. procedure reset;
  64. property current: tai_abstracttypedconst read getcurrent;
  65. end;
  66. protected
  67. fvalues: tfplist;
  68. fisstring: boolean;
  69. { converts the existing data to a single tai_string }
  70. procedure convert_to_string;
  71. procedure add_to_string(strtai: tai_string; othertai: tai);
  72. public
  73. constructor create(_adetyp: ttypedconstkind; _fdef: tdef);
  74. function getenumerator: tadeenumerator;
  75. procedure addvalue(val: tai_abstracttypedconst);
  76. function valuecount: longint;
  77. procedure insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  78. procedure finish;
  79. destructor destroy; override;
  80. end;
  81. tasmlabofs = record
  82. lab: tasmlabel;
  83. ofs: asizeint;
  84. end;
  85. { flags for the finalisation of the typed const builder asmlist }
  86. ttcasmlistoption = (
  87. { the tasmsymbol is a tasmlabel }
  88. tcalo_is_lab,
  89. { start a new section (e.g., because we don't know the current section
  90. type) }
  91. tcalo_new_section,
  92. { this symbol is the start of a block of data that should be
  93. dead-stripable/smartlinkable; may imply starting a new section, but
  94. not necessarily (depends on what the platform requirements are) }
  95. tcalo_make_dead_strippable
  96. );
  97. ttcasmlistoptions = set of ttcasmlistoption;
  98. { information about aggregates we are parsing }
  99. taggregateinformation = class
  100. private
  101. function getcuroffset: asizeint;
  102. function getfieldoffset(l: longint): asizeint;
  103. protected
  104. { type of the aggregate }
  105. fdef: tdef;
  106. { type of the aggregate }
  107. ftyp: ttypedconstkind;
  108. { symtable entry of the previously emitted field in case of a
  109. record/object (nil if none emitted yet), used to insert alignment bytes
  110. if necessary for variant records and objects }
  111. fcurfield,
  112. { field corresponding to the data that will be emitted next in case of a
  113. record/object (nil if not set), used to handle variant records and
  114. objects }
  115. fnextfield: tfieldvarsym;
  116. { similar as the fcurfield/fnextfield above, but instead of fieldvarsyms
  117. these are indices in the symlist of a recorddef that correspond to
  118. fieldvarsyms. These are used only for non-variant records, simply
  119. traversing the fields in order. We could use the above method here as
  120. well, but to find the next field we'd always have to use
  121. symlist.indexof(fcurfield), which would be quite slow. These have -1 as
  122. value if they're not set }
  123. fcurindex,
  124. fnextindex: longint;
  125. { anonymous record that is being built as we add constant data }
  126. fanonrecord: boolean;
  127. property curindex: longint read fcurindex write fcurindex;
  128. property nextindex: longint read fnextindex write fnextindex;
  129. public
  130. constructor create(_def: tdef; _typ: ttypedconstkind); virtual;
  131. { calculated padding bytes for alignment if needed, and add the def of the
  132. next field in case we are constructing an anonymous record }
  133. function prepare_next_field(nextfielddef: tdef): asizeint;
  134. property def: tdef read fdef;
  135. property typ: ttypedconstkind read ftyp;
  136. property curfield: tfieldvarsym read fcurfield write fcurfield;
  137. property nextfield: tfieldvarsym read fnextfield write fnextfield;
  138. property fieldoffset[l: longint]: asizeint read getfieldoffset;
  139. property curoffset: asizeint read getcuroffset;
  140. property anonrecord: boolean read fanonrecord write fanonrecord;
  141. end;
  142. taggregateinformationclass = class of taggregateinformation;
  143. { Warning: never directly create a ttai_typedconstbuilder instance,
  144. instead create a cai_typedconstbuilder (this class can be overridden) }
  145. ttai_typedconstbuilder = class abstract
  146. { class type to use when creating new aggregate information instances }
  147. protected class var
  148. caggregateinformation: taggregateinformationclass;
  149. private
  150. function getcurragginfo: taggregateinformation;
  151. procedure set_next_field(AValue: tfieldvarsym);
  152. protected
  153. { temporary list in which all data is collected }
  154. fasmlist: tasmlist;
  155. { options for the final asmlist }
  156. foptions: ttcasmlistoptions;
  157. { while queueing elements of a compound expression, this is the current
  158. offset in the top-level array/record }
  159. fqueue_offset: asizeint;
  160. { array of caggregateinformation instances }
  161. faggregateinformation: tfpobjectlist;
  162. { ensure that finalize_asmlist is called only once }
  163. fasmlist_finalized: boolean;
  164. { returns whether def must be handled as an aggregate on the current
  165. platform }
  166. function aggregate_kind(def: tdef): ttypedconstkind; virtual;
  167. { finalize the asmlist: add the necessary symbols etc }
  168. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions); virtual;
  169. { called by the public emit_tai() routines to actually add the typed
  170. constant data; the public ones also take care of adding extra padding
  171. bytes etc (by calling this one) }
  172. procedure do_emit_tai(p: tai; def: tdef); virtual;
  173. { calls prepare_next_field() and adds the padding bytes in the current
  174. location }
  175. procedure pad_next_field(nextfielddef: tdef);
  176. { easy access to the top level aggregate information instance }
  177. property curagginfo: taggregateinformation read getcurragginfo;
  178. public
  179. constructor create(const options: ttcasmlistoptions); virtual;
  180. destructor destroy; override;
  181. { add a simple constant data element (p) to the typed constant.
  182. def is the type of the added value }
  183. procedure emit_tai(p: tai; def: tdef); virtual;
  184. { same as above, for a special case: when the def is a procvardef and we
  185. want to use it explicitly as a procdef (i.e., not as a record with a
  186. code and data pointer in case of a complex procvardef) }
  187. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); virtual;
  188. protected
  189. function emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; out startlab: tasmlabel):tasmlabofs;
  190. procedure begin_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  191. procedure end_aggregate_internal(def: tdef; anonymous: boolean); virtual;
  192. { when building an anonymous record, we cannot immediately insert the
  193. alignment before it in case it's nested, since we only know the required
  194. alignment once all fields have been inserted -> mark the location before
  195. the anonymous record, and insert the alignment once it's finished }
  196. procedure mark_anon_aggregate_alignment; virtual; abstract;
  197. procedure insert_marked_aggregate_alignment(def: tdef); virtual; abstract;
  198. public
  199. class function get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  200. { class functions and an extra list parameter, because emitting the data
  201. for the strings has to happen via a separate typed const builder (which
  202. will be created/destroyed internally by these methods) }
  203. class function emit_ansistring_const(list: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding; newsection: boolean): tasmlabofs;
  204. class function emit_unicodestring_const(list: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  205. { emits a tasmlabofs as returned by emit_*string_const }
  206. procedure emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);virtual;
  207. { emit a shortstring constant, and return its def }
  208. function emit_shortstring_const(const str: shortstring): tdef;
  209. { emit a guid constant }
  210. procedure emit_guid_const(const guid: tguid);
  211. { emit a procdef constant }
  212. procedure emit_procdef_const(pd: tprocdef);
  213. { emit an ordinal constant }
  214. procedure emit_ord_const(value: int64; def: tdef);
  215. { begin a potential aggregate type. Must be called for any type
  216. that consists of multiple tai constant data entries, or that
  217. represents an aggregate at the Pascal level (a record, a non-dynamic
  218. array, ... }
  219. procedure maybe_begin_aggregate(def: tdef);
  220. { end a potential aggregate type. Must be paired with every
  221. maybe_begin_aggregate }
  222. procedure maybe_end_aggregate(def: tdef);
  223. { similar as above, but in case
  224. a) it's definitely a record
  225. b) the def of the record should be automatically constructed based on
  226. the types of the emitted fields
  227. }
  228. function begin_anonymous_record(const optionalname: string; packrecords: shortint): trecorddef; virtual;
  229. function end_anonymous_record: trecorddef; virtual;
  230. { The next group of routines are for constructing complex expressions.
  231. While parsing a typed constant these operators are encountered from
  232. outer to inner, so that is also the order in which they should be
  233. added to the queue. Only one queue can be active at a time. }
  234. { Init the queue. Gives an internalerror if a queue was already active }
  235. procedure queue_init(todef: tdef); virtual;
  236. { queue an array/string indexing operation (performs all range checking,
  237. so it doesn't have to be duplicated in all descendents). }
  238. procedure queue_vecn(def: tdef; const index: tconstexprint); virtual;
  239. { queue a subscripting operation }
  240. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); virtual;
  241. { queue a type conversion operation }
  242. procedure queue_typeconvn(fromdef, todef: tdef); virtual;
  243. { queue an address taking operation }
  244. procedure queue_addrn(fromdef, todef: tdef); virtual;
  245. { finalise the queue (so a new one can be created) and flush the
  246. previously queued operations, applying them in reverse order on a...}
  247. { ... procdef }
  248. procedure queue_emit_proc(pd: tprocdef); virtual;
  249. { ... staticvarsym }
  250. procedure queue_emit_staticvar(vs: tstaticvarsym); virtual;
  251. { ... labelsym }
  252. procedure queue_emit_label(l: tlabelsym); virtual;
  253. { ... constsym }
  254. procedure queue_emit_const(cs: tconstsym); virtual;
  255. { ... asmsym/asmlabel }
  256. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); virtual;
  257. { ... an ordinal constant }
  258. procedure queue_emit_ordconst(value: int64; def: tdef); virtual;
  259. { finalize the internal asmlist (if necessary) and return it.
  260. This asmlist will be freed when the builder is destroyed, so add its
  261. contents to another list first. This property should only be accessed
  262. once all data has been added. }
  263. function get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  264. { returns the offset of the string data relative to ansi/unicode/widestring
  265. constant labels. On most platforms, this is 0 (with the header at a
  266. negative offset), but on some platforms such negative offsets are not
  267. supported this is equal to the header size }
  268. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; virtual;
  269. { set the fieldvarsym whose data we will emit next; needed
  270. in case of variant records, so we know which part of the variant gets
  271. initialised. Also in case of objects, because the fieldvarsyms are spread
  272. over the symtables of the entire inheritance tree }
  273. property next_field: tfieldvarsym write set_next_field;
  274. protected
  275. { this one always return the actual offset, called by the above (and
  276. overridden versions) }
  277. class function get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  278. end;
  279. ttai_typedconstbuilderclass = class of ttai_typedconstbuilder;
  280. tlowlevelaggregateinformation = class(taggregateinformation)
  281. protected
  282. fanonrecmarker: tai;
  283. public
  284. property anonrecmarker: tai read fanonrecmarker write fanonrecmarker;
  285. end;
  286. ttai_lowleveltypedconstbuilder = class(ttai_typedconstbuilder)
  287. protected
  288. procedure mark_anon_aggregate_alignment; override;
  289. procedure insert_marked_aggregate_alignment(def: tdef); override;
  290. public
  291. { set the default value for caggregateinformation (= tlowlevelaggregateinformation) }
  292. class constructor classcreate;
  293. end;
  294. var
  295. ctai_typedconstbuilder: ttai_typedconstbuilderclass;
  296. implementation
  297. uses
  298. verbose,globals,systems,widestr,
  299. symbase,symtable,defutil;
  300. {****************************************************************************
  301. taggregateinformation
  302. ****************************************************************************}
  303. function taggregateinformation.getcuroffset: asizeint;
  304. var
  305. field: tfieldvarsym;
  306. begin
  307. if assigned(curfield) then
  308. result:=curfield.fieldoffset+curfield.vardef.size
  309. else if curindex<>-1 then
  310. begin
  311. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[curindex]);
  312. result:=field.fieldoffset+field.vardef.size
  313. end
  314. else
  315. result:=0
  316. end;
  317. function taggregateinformation.getfieldoffset(l: longint): asizeint;
  318. var
  319. field: tfieldvarsym;
  320. begin
  321. field:=tfieldvarsym(tabstractrecorddef(def).symtable.symlist[l]);
  322. result:=field.fieldoffset;
  323. end;
  324. constructor taggregateinformation.create(_def: tdef; _typ: ttypedconstkind);
  325. begin
  326. fdef:=_def;
  327. ftyp:=_typ;
  328. fcurindex:=-1;
  329. fnextindex:=-1;
  330. end;
  331. function taggregateinformation.prepare_next_field(nextfielddef: tdef): asizeint;
  332. var
  333. currentoffset,nextoffset: asizeint;
  334. i: longint;
  335. begin
  336. { get the next field and its offset, and make that next field the current
  337. one }
  338. if assigned(nextfield) then
  339. begin
  340. nextoffset:=nextfield.fieldoffset;
  341. currentoffset:=curoffset;
  342. curfield:=nextfield;
  343. end
  344. else
  345. begin
  346. { must set nextfield for unions and objects, as we cannot
  347. automatically detect the "next" field in that case }
  348. if ((def.typ=recorddef) and
  349. trecorddef(def).isunion) or
  350. is_object(def) then
  351. internalerror(2014091202);
  352. { if we are constructing this record as data gets emitted, add a field
  353. for this data }
  354. if anonrecord then
  355. trecorddef(def).add_field_by_def(nextfielddef);
  356. { find next field }
  357. i:=curindex;
  358. repeat
  359. inc(i);
  360. until tsym(tabstractrecorddef(def).symtable.symlist[i]).typ=fieldvarsym;
  361. nextoffset:=fieldoffset[i];
  362. currentoffset:=curoffset;
  363. curindex:=i;
  364. end;
  365. { need padding? }
  366. result:=nextoffset-currentoffset;
  367. end;
  368. {****************************************************************************
  369. tai_abstracttypedconst
  370. ****************************************************************************}
  371. procedure tai_abstracttypedconst.setdef(def: tdef);
  372. begin
  373. { should not be changed, rewrite the calling code if this happens }
  374. if assigned(fdef) then
  375. Internalerror(2014080203);
  376. fdef:=def;
  377. end;
  378. constructor tai_abstracttypedconst.create(_adetyp: ttypedconstkind; _def: tdef);
  379. begin
  380. inherited create;
  381. typ:=ait_typedconst;
  382. fadetyp:=_adetyp;
  383. fdef:=_def;
  384. end;
  385. {****************************************************************************
  386. tai_simpletypedconst
  387. ****************************************************************************}
  388. constructor tai_simpletypedconst.create(_adetyp: ttypedconstkind; _def: tdef; _val: tai);
  389. begin
  390. inherited create(_adetyp,_def);
  391. fval:=_val;
  392. end;
  393. {****************************************************************************
  394. tai_aggregatetypedconst.tadeenumerator
  395. ****************************************************************************}
  396. constructor tai_aggregatetypedconst.tadeenumerator.create(data: tai_aggregatetypedconst);
  397. begin
  398. fvalues:=data.fvalues;
  399. fvaluespos:=-1;
  400. end;
  401. function tai_aggregatetypedconst.tadeenumerator.getcurrent: tai_abstracttypedconst;
  402. begin
  403. result:=tai_abstracttypedconst(fvalues[fvaluespos]);
  404. end;
  405. function tai_aggregatetypedconst.tadeenumerator.movenext: boolean;
  406. begin
  407. if fvaluespos<pred(fvalues.count) then
  408. begin
  409. inc(fvaluespos);
  410. result:=true
  411. end
  412. else
  413. result:=false;
  414. end;
  415. procedure tai_aggregatetypedconst.tadeenumerator.reset;
  416. begin
  417. fvaluespos:=0
  418. end;
  419. {****************************************************************************
  420. tai_aggregatetypedconst
  421. ****************************************************************************}
  422. procedure tai_aggregatetypedconst.convert_to_string;
  423. var
  424. ai: tai_abstracttypedconst;
  425. newstr: tai_string;
  426. begin
  427. newstr:=tai_string.Create('');
  428. for ai in self do
  429. begin
  430. if ai.adetyp<>tck_simple then
  431. internalerror(2014070103);
  432. add_to_string(newstr,tai_simpletypedconst(ai).val);
  433. ai.free;
  434. end;
  435. fvalues.count:=0;
  436. { the "nil" def will be replaced with an array def of the appropriate
  437. size once we're finished adding data, so we don't create intermediate
  438. arraydefs all the time }
  439. fvalues.add(tai_simpletypedconst.create(tck_simple,nil,newstr));
  440. end;
  441. procedure tai_aggregatetypedconst.add_to_string(strtai: tai_string; othertai: tai);
  442. begin
  443. case othertai.typ of
  444. ait_string:
  445. begin
  446. strtai.str:=reallocmem(strtai.str,strtai.len+tai_string(othertai).len+1);
  447. { also copy null terminator }
  448. move(tai_string(othertai).str[0],strtai.str[strtai.len],tai_string(othertai).len+1);
  449. { the null terminator is not part of the string data }
  450. strtai.len:=strtai.len+tai_string(othertai).len;
  451. end;
  452. ait_const:
  453. begin
  454. if tai_const(othertai).size<>1 then
  455. internalerror(2014070101);
  456. strtai.str:=reallocmem(strtai.str,strtai.len+1);
  457. strtai.str[strtai.len]:=ansichar(tai_const(othertai).value);
  458. strtai.str[strtai.len+1]:=#0;
  459. inc(strtai.len);
  460. end;
  461. else
  462. internalerror(2014070102);
  463. end;
  464. end;
  465. constructor tai_aggregatetypedconst.create(_adetyp: ttypedconstkind; _fdef: tdef);
  466. begin
  467. inherited;
  468. fisstring:=false;
  469. fvalues:=tfplist.create;
  470. end;
  471. function tai_aggregatetypedconst.getenumerator: tadeenumerator;
  472. begin
  473. result:=tadeenumerator.create(self);
  474. end;
  475. procedure tai_aggregatetypedconst.addvalue(val: tai_abstracttypedconst);
  476. begin
  477. { merge string constants and ordinal constants added in an array of
  478. char, to unify the length and the string data }
  479. if fisstring or
  480. ((val.adetyp=tck_simple) and
  481. (tai_simpletypedconst(val).val.typ=ait_string)) then
  482. begin
  483. if not fisstring and
  484. (fvalues.count>0) then
  485. convert_to_string;
  486. fisstring:=true;
  487. case fvalues.count of
  488. 0: fvalues.add(val);
  489. 1:
  490. begin
  491. add_to_string(tai_string(tai_simpletypedconst(fvalues[0]).val),tai_simpletypedconst(val).val);
  492. val.free
  493. end
  494. else
  495. internalerror(2014070104);
  496. end;
  497. end
  498. else
  499. fvalues.add(val);
  500. end;
  501. function tai_aggregatetypedconst.valuecount: longint;
  502. begin
  503. result:=fvalues.count;
  504. end;
  505. procedure tai_aggregatetypedconst.insertvaluebeforepos(val: tai_abstracttypedconst; pos: longint);
  506. begin
  507. fvalues.insert(pos,val);
  508. end;
  509. procedure tai_aggregatetypedconst.finish;
  510. begin
  511. if fisstring then
  512. begin
  513. { set the def: an array of char with the same length as the string
  514. data }
  515. if fvalues.count<>1 then
  516. internalerror(2014070105);
  517. tai_simpletypedconst(fvalues[0]).fdef:=
  518. getarraydef(cansichartype,
  519. tai_string(tai_simpletypedconst(fvalues[0]).val).len);
  520. end;
  521. end;
  522. destructor tai_aggregatetypedconst.destroy;
  523. begin
  524. fvalues.free;
  525. inherited destroy;
  526. end;
  527. {*****************************************************************************
  528. ttai_typedconstbuilder
  529. *****************************************************************************}
  530. function ttai_typedconstbuilder.getcurragginfo: taggregateinformation;
  531. begin
  532. if assigned(faggregateinformation) and
  533. (faggregateinformation.count>0) then
  534. result:=taggregateinformation(faggregateinformation[faggregateinformation.count-1])
  535. else
  536. result:=nil;
  537. end;
  538. procedure ttai_typedconstbuilder.set_next_field(AValue: tfieldvarsym);
  539. var
  540. info: taggregateinformation;
  541. begin
  542. info:=curagginfo;
  543. if not assigned(info) then
  544. internalerror(2014091206);
  545. info.nextfield:=AValue;
  546. end;
  547. procedure ttai_typedconstbuilder.pad_next_field(nextfielddef: tdef);
  548. var
  549. fillbytes: asizeint;
  550. begin
  551. fillbytes:=curagginfo.prepare_next_field(nextfielddef);
  552. while fillbytes>0 do
  553. begin
  554. do_emit_tai(tai_const.create_8bit(0),u8inttype);
  555. dec(fillbytes);
  556. end;
  557. end;
  558. function ttai_typedconstbuilder.aggregate_kind(def: tdef): ttypedconstkind;
  559. begin
  560. if (def.typ in [recorddef,filedef,variantdef]) or
  561. is_object(def) or
  562. ((def.typ=procvardef) and
  563. not tprocvardef(def).is_addressonly) then
  564. result:=tck_record
  565. else if ((def.typ=arraydef) and
  566. not is_dynamic_array(def)) or
  567. ((def.typ=setdef) and
  568. not is_smallset(def)) or
  569. is_shortstring(def) then
  570. result:=tck_array
  571. else
  572. result:=tck_simple;
  573. end;
  574. procedure ttai_typedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; const options: ttcasmlistoptions);
  575. var
  576. prelist: tasmlist;
  577. begin
  578. prelist:=tasmlist.create;
  579. { only now add items based on the symbolname, because it may be
  580. modified by the "section" specifier in case of a typed constant }
  581. if tcalo_make_dead_strippable in options then
  582. begin
  583. maybe_new_object_file(prelist);
  584. { we always need a new section here, since if we started a new
  585. object file then we have to say what the section is, and otherwise
  586. we need a new section because that's how the dead stripping works
  587. (except on Darwin, but that will be addressed in a future commit) }
  588. new_section(prelist,section,secname,const_align(alignment));
  589. end
  590. else if tcalo_new_section in options then
  591. new_section(prelist,section,secname,const_align(alignment))
  592. else
  593. prelist.concat(cai_align.Create(const_align(alignment)));
  594. if not(tcalo_is_lab in options) then
  595. if sym.bind=AB_GLOBAL then
  596. prelist.concat(tai_symbol.Create_Global(sym,0))
  597. else
  598. prelist.concat(tai_symbol.Create(sym,0))
  599. else
  600. prelist.concat(tai_label.Create(tasmlabel(sym)));
  601. { insert the symbol information before the data }
  602. fasmlist.insertlist(prelist);
  603. { end of the symbol }
  604. fasmlist.concat(tai_symbol_end.Createname(sym.name));
  605. { free the temporary list }
  606. prelist.free;
  607. end;
  608. procedure ttai_typedconstbuilder.do_emit_tai(p: tai; def: tdef);
  609. begin
  610. { by default we don't care about the type }
  611. fasmlist.concat(p);
  612. end;
  613. function ttai_typedconstbuilder.get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint): tasmlist;
  614. begin
  615. if not fasmlist_finalized then
  616. begin
  617. finalize_asmlist(sym,def,section,secname,alignment,foptions);
  618. fasmlist_finalized:=true;
  619. end;
  620. result:=fasmlist;
  621. end;
  622. class function ttai_typedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  623. begin
  624. { darwin's linker does not support negative offsets }
  625. if not(target_info.system in systems_darwin) then
  626. result:=0
  627. else
  628. result:=get_string_header_size(typ,winlikewidestring);
  629. end;
  630. class function ttai_typedconstbuilder.get_string_header_size(typ: tstringtype; winlikewidestring: boolean): pint;
  631. const
  632. ansistring_header_size =
  633. { encoding }
  634. 2 +
  635. { elesize }
  636. 2 +
  637. {$ifdef cpu64bitaddr}
  638. { alignment }
  639. 4 +
  640. {$endif cpu64bitaddr}
  641. { reference count }
  642. sizeof(pint) +
  643. { length }
  644. sizeof(pint);
  645. unicodestring_header_size = ansistring_header_size;
  646. begin
  647. case typ of
  648. st_ansistring:
  649. result:=ansistring_header_size;
  650. st_unicodestring:
  651. result:=unicodestring_header_size;
  652. st_widestring:
  653. if winlikewidestring then
  654. result:=0
  655. else
  656. result:=unicodestring_header_size;
  657. else
  658. result:=0;
  659. end;
  660. end;
  661. constructor ttai_typedconstbuilder.create(const options: ttcasmlistoptions);
  662. begin
  663. inherited create;
  664. fasmlist:=tasmlist.create;
  665. foptions:=options;
  666. { queue is empty }
  667. fqueue_offset:=low(fqueue_offset);
  668. end;
  669. destructor ttai_typedconstbuilder.destroy;
  670. begin
  671. { the queue should have been flushed if it was used }
  672. if fqueue_offset<>low(fqueue_offset) then
  673. internalerror(2014062901);
  674. faggregateinformation.free;
  675. fasmlist.free;
  676. inherited destroy;
  677. end;
  678. procedure ttai_typedconstbuilder.emit_tai(p: tai; def: tdef);
  679. var
  680. kind: ttypedconstkind;
  681. info: taggregateinformation;
  682. begin
  683. { these elements can be aggregates themselves, e.g. a shortstring can
  684. be emitted as a series of bytes and char arrays }
  685. kind:=aggregate_kind(def);
  686. info:=curagginfo;
  687. if (kind<>tck_simple) and
  688. (not assigned(info) or
  689. (info.typ<>kind)) then
  690. internalerror(2014091001);
  691. { if we're emitting a record, handle the padding bytes, and in case of
  692. an anonymous record also add the next field }
  693. if assigned(info) then
  694. begin
  695. if ((info.def.typ=recorddef) or
  696. is_object(info.def)) and
  697. { may add support for these later }
  698. not is_packed_record_or_object(info.def) then
  699. pad_next_field(def);
  700. end;
  701. { emit the data }
  702. do_emit_tai(p,def);
  703. end;
  704. procedure ttai_typedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  705. begin
  706. { nothing special by default, since we don't care about the type }
  707. emit_tai(p,pvdef);
  708. end;
  709. function ttai_typedconstbuilder.emit_string_const_common(stringtype: tstringtype; len: asizeint; encoding: tstringencoding; out startlab: tasmlabel): tasmlabofs;
  710. var
  711. string_symofs: asizeint;
  712. charptrdef: tdef;
  713. elesize: word;
  714. begin
  715. current_asmdata.getglobaldatalabel(result.lab);
  716. startlab:=result.lab;
  717. result.ofs:=0;
  718. { pack the data, so that we don't add unnecessary null bytes after the
  719. constant string }
  720. begin_anonymous_record('$'+get_dynstring_rec_name(stringtype,false,len),1);
  721. string_symofs:=get_string_symofs(stringtype,false);
  722. { encoding }
  723. emit_tai(tai_const.create_16bit(encoding),u16inttype);
  724. inc(result.ofs,2);
  725. { element size }
  726. case stringtype of
  727. st_ansistring:
  728. begin
  729. elesize:=1;
  730. charptrdef:=charpointertype;
  731. end;
  732. st_unicodestring:
  733. begin
  734. elesize:=2;
  735. charptrdef:=widecharpointertype;
  736. end
  737. else
  738. internalerror(2014080401);
  739. end;
  740. emit_tai(tai_const.create_16bit(elesize),u16inttype);
  741. inc(result.ofs,2);
  742. {$ifdef cpu64bitaddr}
  743. { dummy for alignment }
  744. emit_tai(tai_const.create_32bit(0),u32inttype);
  745. inc(result.ofs,4);
  746. {$endif cpu64bitaddr}
  747. emit_tai(tai_const.create_pint(-1),ptrsinttype);
  748. inc(result.ofs,sizeof(pint));
  749. emit_tai(tai_const.create_pint(len),ptrsinttype);
  750. inc(result.ofs,sizeof(pint));
  751. if string_symofs=0 then
  752. begin
  753. { results in slightly more efficient code }
  754. emit_tai(tai_label.create(result.lab),charptrdef);
  755. result.ofs:=0;
  756. current_asmdata.getglobaldatalabel(startlab);
  757. end;
  758. { sanity check }
  759. if result.ofs<>string_symofs then
  760. internalerror(2012051701);
  761. end;
  762. procedure ttai_typedconstbuilder.begin_aggregate_internal(def: tdef; anonymous: boolean);
  763. var
  764. info: taggregateinformation;
  765. tck: ttypedconstkind;
  766. begin
  767. tck:=aggregate_kind(def);
  768. if tck=tck_simple then
  769. exit;
  770. if not assigned(faggregateinformation) then
  771. faggregateinformation:=tfpobjectlist.create
  772. { if we're starting an anonymous record, we can't align it yet because
  773. the alignment depends on the fields that will be added -> we'll do
  774. it at the end }
  775. else if not anonymous then
  776. begin
  777. { add padding if necessary, and update the current field/offset }
  778. info:=curagginfo;
  779. if is_record(curagginfo.def) or
  780. is_object(curagginfo.def) then
  781. pad_next_field(def);
  782. end
  783. { if this is the outer record, no padding is required; the alignment
  784. has to be specified explicitly in that case via get_final_asmlist() }
  785. else if assigned(curagginfo) and
  786. (curagginfo.def.typ=recorddef) then
  787. { mark where we'll have to insert the padding bytes at the end }
  788. mark_anon_aggregate_alignment;
  789. info:=caggregateinformation.create(def,aggregate_kind(def));
  790. faggregateinformation.add(info);
  791. end;
  792. procedure ttai_typedconstbuilder.end_aggregate_internal(def: tdef; anonymous: boolean);
  793. var
  794. info: taggregateinformation;
  795. fillbytes: asizeint;
  796. tck: ttypedconstkind;
  797. begin
  798. tck:=aggregate_kind(def);
  799. if tck=tck_simple then
  800. exit;
  801. info:=curagginfo;
  802. if not assigned(info) then
  803. internalerror(2014091002);
  804. if def<>info.def then
  805. internalerror(2014091205);
  806. { add tail padding if necessary }
  807. if (is_record(def) or
  808. is_object(def)) and
  809. not is_packed_record_or_object(def) then
  810. begin
  811. fillbytes:=def.size-info.curoffset;
  812. while fillbytes>0 do
  813. begin
  814. do_emit_tai(Tai_const.Create_8bit(0),u8inttype);
  815. dec(fillbytes)
  816. end;
  817. end;
  818. { pop and free the information }
  819. faggregateinformation.count:=faggregateinformation.count-1;
  820. info.free;
  821. end;
  822. class function ttai_typedconstbuilder.get_dynstring_rec_name(typ: tstringtype; winlike: boolean; len: asizeint): string;
  823. begin
  824. case typ of
  825. st_ansistring:
  826. result:='ansistrrec';
  827. st_unicodestring,
  828. st_widestring:
  829. if (typ=st_unicodestring) or
  830. not winlike then
  831. result:='unicodestrrec'
  832. else
  833. result:='widestrrec';
  834. else
  835. internalerror(2014080402);
  836. end;
  837. result:=result+tostr(len);
  838. end;
  839. class function ttai_typedconstbuilder.emit_ansistring_const(list: TAsmList; data: pchar; len: asizeint; encoding: tstringencoding; newsection: boolean): tasmlabofs;
  840. var
  841. s: PChar;
  842. startlab: tasmlabel;
  843. ansistrrecdef: trecorddef;
  844. datadef: tdef;
  845. datatcb: ttai_typedconstbuilder;
  846. options: ttcasmlistoptions;
  847. begin
  848. options:=[tcalo_is_lab];
  849. if NewSection then
  850. include(options,tcalo_make_dead_strippable);
  851. datatcb:=self.create(options);
  852. result:=datatcb.emit_string_const_common(st_ansistring,len,encoding,startlab);
  853. getmem(s,len+1);
  854. move(data^,s^,len);
  855. s[len]:=#0;
  856. { terminating zero included }
  857. datadef:=getarraydef(cansichartype,len+1);
  858. datatcb.maybe_begin_aggregate(datadef);
  859. datatcb.emit_tai(tai_string.create_pchar(s,len+1),datadef);
  860. datatcb.maybe_end_aggregate(datadef);
  861. ansistrrecdef:=datatcb.end_anonymous_record;
  862. list.concatlist(datatcb.get_final_asmlist(startlab,ansistrrecdef,sec_rodata_norel,startlab.name,const_align(sizeof(pint))));
  863. datatcb.free;
  864. end;
  865. class function ttai_typedconstbuilder.emit_unicodestring_const(list: TAsmList; data: pointer; encoding: tstringencoding; winlike: boolean):tasmlabofs;
  866. var
  867. i, strlength: longint;
  868. string_symofs: asizeint;
  869. startlab: tasmlabel;
  870. datadef: tdef;
  871. uniwidestrrecdef: trecorddef;
  872. datatcb: ttai_typedconstbuilder;
  873. begin
  874. datatcb:=self.create([tcalo_is_lab,tcalo_make_dead_strippable]);
  875. strlength:=getlengthwidestring(pcompilerwidestring(data));
  876. if winlike then
  877. begin
  878. datatcb.begin_anonymous_record('$'+get_dynstring_rec_name(st_widestring,true,strlength),sizeof(pint));
  879. current_asmdata.getglobaldatalabel(result.lab);
  880. datatcb.emit_tai(Tai_const.Create_32bit(strlength*cwidechartype.size),s32inttype);
  881. { can we optimise by placing the string constant label at the
  882. required offset? }
  883. string_symofs:=get_string_symofs(st_widestring,true);
  884. if string_symofs=0 then
  885. begin
  886. { yes }
  887. datatcb.emit_tai(Tai_label.Create(result.lab),widecharpointertype);
  888. { allocate a separate label for the start of the data }
  889. current_asmdata.getglobaldatalabel(startlab);
  890. end
  891. else
  892. internalerror(2015031502);
  893. result.ofs:=string_symofs;
  894. end
  895. else
  896. begin
  897. result:=datatcb.emit_string_const_common(st_unicodestring,strlength,encoding,startlab);
  898. end;
  899. if cwidechartype.size = 2 then
  900. begin
  901. datadef:=getarraydef(cwidechartype,strlength+1);
  902. datatcb.maybe_begin_aggregate(datadef);
  903. for i:=0 to strlength-1 do
  904. datatcb.emit_tai(Tai_const.Create_16bit(pcompilerwidestring(data)^.data[i]),cwidechartype);
  905. { ending #0 }
  906. datatcb.emit_tai(Tai_const.Create_16bit(0),cwidechartype);
  907. datatcb.maybe_end_aggregate(datadef);
  908. uniwidestrrecdef:=datatcb.end_anonymous_record;
  909. end
  910. else
  911. { code generation for other sizes must be written }
  912. internalerror(200904271);
  913. list.concatlist(datatcb.get_final_asmlist(startlab,uniwidestrrecdef,sec_rodata_norel,startlab.name,const_align(sizeof(pint))));
  914. datatcb.free;
  915. end;
  916. procedure ttai_typedconstbuilder.emit_string_offset(const ll: tasmlabofs; const strlength: longint; const st: tstringtype; const winlikewidestring: boolean; const charptrdef: tdef);
  917. begin
  918. emit_tai(Tai_const.Create_sym_offset(ll.lab,ll.ofs),charptrdef);
  919. end;
  920. function ttai_typedconstbuilder.emit_shortstring_const(const str: shortstring): tdef;
  921. begin
  922. { we use an arraydef instead of a shortstringdef, because we don't have
  923. functionality in place yet to reuse shortstringdefs of the same length
  924. and neither the lowlevel nor the llvm typedconst builder cares about
  925. this difference }
  926. result:=getarraydef(cansichartype,length(str)+1);
  927. maybe_begin_aggregate(result);
  928. emit_tai(Tai_const.Create_8bit(length(str)),u8inttype);
  929. if str<>'' then
  930. emit_tai(Tai_string.Create(str),getarraydef(cansichartype,length(str)));
  931. maybe_end_aggregate(result);
  932. end;
  933. procedure ttai_typedconstbuilder.emit_guid_const(const guid: tguid);
  934. var
  935. i: longint;
  936. begin
  937. maybe_begin_aggregate(rec_tguid);
  938. { variant record -> must specify which fields get initialised }
  939. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[0]);
  940. emit_tai(Tai_const.Create_32bit(longint(guid.D1)),u32inttype);
  941. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[1]);
  942. emit_tai(Tai_const.Create_16bit(guid.D2),u16inttype);
  943. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[2]);
  944. emit_tai(Tai_const.Create_16bit(guid.D3),u16inttype);
  945. next_field:=tfieldvarsym(rec_tguid.symtable.symlist[3]);
  946. { the array }
  947. maybe_begin_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  948. for i:=Low(guid.D4) to High(guid.D4) do
  949. emit_tai(Tai_const.Create_8bit(guid.D4[i]),u8inttype);
  950. maybe_end_aggregate(tfieldvarsym(rec_tguid.symtable.symlist[3]).vardef);
  951. maybe_end_aggregate(rec_tguid);
  952. end;
  953. procedure ttai_typedconstbuilder.emit_procdef_const(pd: tprocdef);
  954. begin
  955. emit_tai(Tai_const.Createname(pd.mangledname,AT_FUNCTION,0),pd.getcopyas(procvardef,pc_address_only));
  956. end;
  957. procedure ttai_typedconstbuilder.emit_ord_const(value: int64; def: tdef);
  958. begin
  959. case def.size of
  960. 1:
  961. emit_tai(Tai_const.Create_8bit(byte(value)),def);
  962. 2:
  963. emit_tai(Tai_const.Create_16bit(word(value)),def);
  964. 4:
  965. emit_tai(Tai_const.Create_32bit(longint(value)),def);
  966. 8:
  967. emit_tai(Tai_const.Create_64bit(value),def);
  968. else
  969. internalerror(2014100501);
  970. end;
  971. end;
  972. procedure ttai_typedconstbuilder.maybe_begin_aggregate(def: tdef);
  973. begin
  974. begin_aggregate_internal(def,false);
  975. end;
  976. procedure ttai_typedconstbuilder.maybe_end_aggregate(def: tdef);
  977. begin
  978. end_aggregate_internal(def,false);
  979. end;
  980. function ttai_typedconstbuilder.begin_anonymous_record(const optionalname: string; packrecords: shortint): trecorddef;
  981. var
  982. anonrecorddef: trecorddef;
  983. srsym: tsym;
  984. srsymtable: tsymtable;
  985. found: boolean;
  986. begin
  987. { if the name is specified, we create a typesym with that name in order
  988. to ensure we can find it again later with that name -> reuse here as
  989. well if possible (and that also avoids duplicate type name issues) }
  990. if optionalname<>'' then
  991. begin
  992. if optionalname[1]='$' then
  993. found:=searchsym_type(copy(optionalname,2,length(optionalname)),srsym,srsymtable)
  994. else
  995. found:=searchsym_type(optionalname,srsym,srsymtable);
  996. if found then
  997. begin
  998. if ttypesym(srsym).typedef.typ<>recorddef then
  999. internalerror(2014091207);
  1000. result:=trecorddef(ttypesym(srsym).typedef);
  1001. maybe_begin_aggregate(result);
  1002. exit;
  1003. end;
  1004. end;
  1005. { create skeleton def }
  1006. anonrecorddef:=crecorddef.create_global_internal(optionalname,packrecords);
  1007. { generic aggregate housekeeping }
  1008. begin_aggregate_internal(anonrecorddef,true);
  1009. { mark as anonymous record }
  1010. curagginfo.anonrecord:=true;
  1011. { in case a descendent wants to do something with the anonrecorddef too }
  1012. result:=anonrecorddef;
  1013. end;
  1014. function ttai_typedconstbuilder.end_anonymous_record: trecorddef;
  1015. var
  1016. info: taggregateinformation;
  1017. anonrecord: boolean;
  1018. begin
  1019. info:=curagginfo;
  1020. if not assigned(info) or
  1021. (info.def.typ<>recorddef) then
  1022. internalerror(2014080201);
  1023. result:=trecorddef(info.def);
  1024. { make a copy, as we need it after info has been freed by
  1025. maybe_end_aggregate(result) }
  1026. anonrecord:=info.anonrecord;
  1027. { finalise the record skeleton (all fields have been added already by
  1028. emit_tai()) -- anonrecord may not be set in case we reused an earlier
  1029. constructed def }
  1030. if anonrecord then
  1031. trecordsymtable(result.symtable).addalignmentpadding;
  1032. end_aggregate_internal(result,true);
  1033. if anonrecord and
  1034. assigned(curagginfo) and
  1035. (curagginfo.def.typ=recorddef) then
  1036. insert_marked_aggregate_alignment(result);
  1037. end;
  1038. procedure ttai_typedconstbuilder.queue_init(todef: tdef);
  1039. begin
  1040. { nested call to init? }
  1041. if fqueue_offset<>low(fqueue_offset) then
  1042. internalerror(2014062101);
  1043. fqueue_offset:=0;
  1044. end;
  1045. procedure ttai_typedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  1046. var
  1047. elelen,
  1048. vecbase: asizeint;
  1049. v: tconstexprint;
  1050. begin
  1051. elelen:=1;
  1052. vecbase:=0;
  1053. case def.typ of
  1054. stringdef :
  1055. ;
  1056. arraydef :
  1057. begin
  1058. if not is_packed_array(def) then
  1059. begin
  1060. elelen:=tarraydef(def).elesize;
  1061. vecbase:=tarraydef(def).lowrange;
  1062. end
  1063. else
  1064. Message(parser_e_packed_dynamic_open_array);
  1065. end;
  1066. else
  1067. Message(parser_e_illegal_expression);
  1068. end;
  1069. { Prevent overflow }
  1070. v:=index-vecbase;
  1071. if (v<int64(low(fqueue_offset))) or (v>int64(high(fqueue_offset))) then
  1072. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(fqueue_offset)),tostr(high(fqueue_offset)));
  1073. if high(fqueue_offset)-fqueue_offset div elelen>v then
  1074. inc(fqueue_offset,elelen*v.svalue)
  1075. else
  1076. message3(type_e_range_check_error_bounds,tostr(index),tostr(vecbase),tostr(high(fqueue_offset)-fqueue_offset div elelen+vecbase))
  1077. end;
  1078. procedure ttai_typedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  1079. begin
  1080. inc(fqueue_offset,vs.fieldoffset);
  1081. end;
  1082. procedure ttai_typedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  1083. begin
  1084. { do nothing }
  1085. end;
  1086. procedure ttai_typedconstbuilder.queue_addrn(fromdef, todef: tdef);
  1087. begin
  1088. { do nothing }
  1089. end;
  1090. procedure ttai_typedconstbuilder.queue_emit_proc(pd: tprocdef);
  1091. begin
  1092. if fqueue_offset<>0 then
  1093. internalerror(2014092101);
  1094. emit_procdef_const(pd);
  1095. fqueue_offset:=low(fqueue_offset);
  1096. end;
  1097. procedure ttai_typedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  1098. begin
  1099. { getpointerdef because we are emitting a pointer to the staticvarsym
  1100. data, not the data itself }
  1101. emit_tai(Tai_const.Createname(vs.mangledname,fqueue_offset),getpointerdef(vs.vardef));
  1102. fqueue_offset:=low(fqueue_offset);
  1103. end;
  1104. procedure ttai_typedconstbuilder.queue_emit_label(l: tlabelsym);
  1105. begin
  1106. emit_tai(Tai_const.Createname(l.mangledname,fqueue_offset),voidcodepointertype);
  1107. fqueue_offset:=low(fqueue_offset);
  1108. end;
  1109. procedure ttai_typedconstbuilder.queue_emit_const(cs: tconstsym);
  1110. begin
  1111. if cs.consttyp<>constresourcestring then
  1112. internalerror(2014062102);
  1113. if fqueue_offset<>0 then
  1114. internalerror(2014062103);
  1115. { warning: update if/when the type of resource strings changes }
  1116. emit_tai(Tai_const.Createname(make_mangledname('RESSTR',cs.owner,cs.name),AT_DATA,sizeof(pint)),cansistringtype);
  1117. fqueue_offset:=low(fqueue_offset);
  1118. end;
  1119. procedure ttai_typedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  1120. begin
  1121. { getpointerdef, because "sym" represents the address of whatever the
  1122. data is }
  1123. def:=getpointerdef(def);
  1124. emit_tai(Tai_const.Create_sym_offset(sym,fqueue_offset),def);
  1125. fqueue_offset:=low(fqueue_offset);
  1126. end;
  1127. procedure ttai_typedconstbuilder.queue_emit_ordconst(value: int64; def: tdef);
  1128. begin
  1129. emit_ord_const(value,def);
  1130. fqueue_offset:=low(fqueue_offset);
  1131. end;
  1132. {****************************************************************************
  1133. tai_abstracttypedconst
  1134. ****************************************************************************}
  1135. class constructor ttai_lowleveltypedconstbuilder.classcreate;
  1136. begin
  1137. caggregateinformation:=tlowlevelaggregateinformation;
  1138. end;
  1139. procedure ttai_lowleveltypedconstbuilder.mark_anon_aggregate_alignment;
  1140. var
  1141. marker: tai_marker;
  1142. begin
  1143. marker:=tai_marker.Create(mark_position);
  1144. fasmlist.concat(marker);
  1145. tlowlevelaggregateinformation(curagginfo).anonrecmarker:=marker;
  1146. end;
  1147. procedure ttai_lowleveltypedconstbuilder.insert_marked_aggregate_alignment(def: tdef);
  1148. var
  1149. info: tlowlevelaggregateinformation;
  1150. fillbytes: asizeint;
  1151. begin
  1152. info:=tlowlevelaggregateinformation(curagginfo);
  1153. if not assigned(info.anonrecmarker) then
  1154. internalerror(2014091401);
  1155. fillbytes:=info.prepare_next_field(def);
  1156. while fillbytes>0 do
  1157. begin
  1158. fasmlist.insertafter(tai_const.create_8bit(0),info.anonrecmarker);
  1159. dec(fillbytes);
  1160. end;
  1161. fasmlist.remove(info.anonrecmarker);
  1162. info.anonrecmarker.free;
  1163. info.anonrecmarker:=nil;
  1164. end;
  1165. begin
  1166. ctai_typedconstbuilder:=ttai_lowleveltypedconstbuilder;
  1167. end.