aasmcnst.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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; tck_simple_procvar2proc is to indicate that we mean the
  28. procdef corresponding to the procvar rather than the tmethod-like
  29. struct in case of a complex procvar }
  30. ttypedconstkind = (tck_simple, tck_simple_procvar2proc, tck_array, tck_record);
  31. { the type of the element and its def }
  32. tai_abstracttypedconst = class abstract (tai)
  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;
  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. procedure finish;
  77. destructor destroy; override;
  78. end;
  79. { Warning: never directly create a ttai_typedconstbuilder instance,
  80. instead create a cai_typedconstbuilder (this class can be overridden) }
  81. ttai_lowleveltypedconstbuilder = class abstract
  82. protected
  83. { temporary list in which all data is collected }
  84. fasmlist: tasmlist;
  85. { while queueing elements of a compound expression, this is the current
  86. offset in the top-level array/record }
  87. fqueue_offset: asizeint;
  88. { ensure that finalize_asmlist is called only once }
  89. fasmlist_finalized: boolean;
  90. { returns whether def must be handled as an aggregate on the current
  91. platform }
  92. function aggregate_kind(def: tdef): ttypedconstkind; virtual;
  93. { finalize the asmlist: add the necessary symbols etc }
  94. procedure finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; lab: boolean); virtual;
  95. public
  96. constructor create; virtual;
  97. destructor destroy; override;
  98. { add a simple constant data element (p) to the typed constant.
  99. def is the type of the added value }
  100. procedure emit_tai(p: tai; def: tdef); virtual;
  101. { same as above, for a special case: when the def is a procvardef and we
  102. want to use it explicitly as a procdef (i.e., not as a record with a
  103. code and data pointer in case of a complex procvardef) }
  104. procedure emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef); virtual;
  105. { begin a potential aggregate type. Must be called for any type
  106. that consists of multiple tai constant data entries, or that
  107. represents an aggregate at the Pascal level (a record, a non-dynamic
  108. array, ... }
  109. procedure maybe_begin_aggregate(def: tdef); virtual;
  110. { end a potential aggregate type. Must be paired with every
  111. maybe_begin_aggregate }
  112. procedure maybe_end_aggregate(def: tdef); virtual;
  113. { The next group of routines are for constructing complex expressions.
  114. While parsing a typed constant these operators are encountered from
  115. outer to inner, so that is also the order in which they should be
  116. added to the queue. Only one queue can be active at a time. }
  117. { Init the queue. Gives an internalerror if a queue was already active }
  118. procedure queue_init(todef: tdef); virtual;
  119. { queue an array/string indexing operation (performs all range checking,
  120. so it doesn't have to be duplicated in all descendents). }
  121. procedure queue_vecn(def: tdef; const index: tconstexprint); virtual;
  122. { queue a subscripting operation }
  123. procedure queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym); virtual;
  124. { queue a type conversion operation }
  125. procedure queue_typeconvn(fromdef, todef: tdef); virtual;
  126. { queue an address taking operation }
  127. procedure queue_addrn(fromdef, todef: tdef); virtual;
  128. { finalise the queue (so a new one can be created) and flush the
  129. previously queued operations, applying them in reverse order on a...}
  130. { ... procdef }
  131. procedure queue_emit_proc(pd: tprocdef); virtual;
  132. { ... staticvarsym }
  133. procedure queue_emit_staticvar(vs: tstaticvarsym); virtual;
  134. { ... labelsym }
  135. procedure queue_emit_label(l: tlabelsym); virtual;
  136. { ... constsym }
  137. procedure queue_emit_const(cs: tconstsym); virtual;
  138. { ... asmsym/asmlabel }
  139. procedure queue_emit_asmsym(sym: tasmsymbol; def: tdef); virtual;
  140. { finalize the internal asmlist (if necessary) and return it.
  141. This asmlist will be freed when the builder is destroyed, so add its
  142. contents to another list first. This property should only be accessed
  143. once all data has been added. }
  144. function get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint; lab: boolean): tasmlist;
  145. { returns the offset of the string data relative to ansi/unicode/widestring
  146. constant labels. On most platforms, this is 0 (with the header at a
  147. negative offset), but on some platforms such negative offsets are not
  148. supported this is 0 }
  149. class function get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint; virtual;
  150. end;
  151. ttai_lowleveltypedconstbuilderclass = class of ttai_lowleveltypedconstbuilder;
  152. var
  153. ctai_typedconstbuilder: ttai_lowleveltypedconstbuilderclass;
  154. implementation
  155. uses
  156. verbose,globals,systems,
  157. defutil;
  158. {****************************************************************************
  159. tai_abstracttypedconst
  160. ****************************************************************************}
  161. constructor tai_abstracttypedconst.create(_adetyp: ttypedconstkind; _def: tdef);
  162. begin
  163. inherited create;
  164. typ:=ait_typedconst;
  165. fadetyp:=_adetyp;
  166. fdef:=_def;
  167. end;
  168. {****************************************************************************
  169. tai_simpletypedconst
  170. ****************************************************************************}
  171. constructor tai_simpletypedconst.create(_adetyp: ttypedconstkind; _def: tdef; _val: tai);
  172. begin
  173. inherited create(_adetyp,_def);
  174. fval:=_val;
  175. end;
  176. {****************************************************************************
  177. tai_aggregatetypedconst.tadeenumerator
  178. ****************************************************************************}
  179. constructor tai_aggregatetypedconst.tadeenumerator.create(data: tai_aggregatetypedconst);
  180. begin
  181. fvalues:=data.fvalues;
  182. fvaluespos:=-1;
  183. end;
  184. function tai_aggregatetypedconst.tadeenumerator.getcurrent: tai_abstracttypedconst;
  185. begin
  186. result:=tai_abstracttypedconst(fvalues[fvaluespos]);
  187. end;
  188. function tai_aggregatetypedconst.tadeenumerator.movenext: boolean;
  189. begin
  190. if fvaluespos<pred(fvalues.count) then
  191. begin
  192. inc(fvaluespos);
  193. result:=true
  194. end
  195. else
  196. result:=false;
  197. end;
  198. procedure tai_aggregatetypedconst.tadeenumerator.reset;
  199. begin
  200. fvaluespos:=0
  201. end;
  202. {****************************************************************************
  203. tai_aggregatetypedconst
  204. ****************************************************************************}
  205. procedure tai_aggregatetypedconst.convert_to_string;
  206. var
  207. ai: tai_abstracttypedconst;
  208. newstr: tai_string;
  209. begin
  210. newstr:=tai_string.Create('');
  211. for ai in self do
  212. begin
  213. if ai.adetyp<>tck_simple then
  214. internalerror(2014070103);
  215. add_to_string(newstr,tai_simpletypedconst(ai).val);
  216. ai.free;
  217. end;
  218. fvalues.count:=0;
  219. { the "nil" def will be replaced with an array def of the appropriate
  220. size once we're finished adding data, so we don't create intermediate
  221. arraydefs all the time }
  222. fvalues.add(tai_simpletypedconst.create(tck_simple,nil,newstr));
  223. end;
  224. procedure tai_aggregatetypedconst.add_to_string(strtai: tai_string; othertai: tai);
  225. begin
  226. case othertai.typ of
  227. ait_string:
  228. begin
  229. strtai.str:=reallocmem(strtai.str,strtai.len+tai_string(othertai).len+1);
  230. { also copy null terminator }
  231. move(tai_string(othertai).str[0],strtai.str[strtai.len],tai_string(othertai).len+1);
  232. { the null terminator is not part of the string data }
  233. strtai.len:=strtai.len+tai_string(othertai).len;
  234. end;
  235. ait_const:
  236. begin
  237. if tai_const(othertai).size<>1 then
  238. internalerror(2014070101);
  239. strtai.str:=reallocmem(strtai.str,strtai.len+1);
  240. strtai.str[strtai.len]:=ansichar(tai_const(othertai).value);
  241. strtai.str[strtai.len+1]:=#0;
  242. inc(strtai.len);
  243. end;
  244. else
  245. internalerror(2014070102);
  246. end;
  247. end;
  248. constructor tai_aggregatetypedconst.create(_adetyp: ttypedconstkind; _fdef: tdef);
  249. begin
  250. inherited;
  251. fisstring:=false;
  252. fvalues:=tfplist.create;
  253. end;
  254. function tai_aggregatetypedconst.getenumerator: tadeenumerator;
  255. begin
  256. result:=tadeenumerator.create(self);
  257. end;
  258. procedure tai_aggregatetypedconst.addvalue(val: tai_abstracttypedconst);
  259. begin
  260. { merge string constants and ordinal constants added in an array of
  261. char, to unify the length and the string data }
  262. if fisstring or
  263. ((val.adetyp=tck_simple) and
  264. (tai_simpletypedconst(val).val.typ=ait_string)) then
  265. begin
  266. if not fisstring and
  267. (fvalues.count>0) then
  268. convert_to_string;
  269. fisstring:=true;
  270. case fvalues.count of
  271. 0: fvalues.add(val);
  272. 1:
  273. begin
  274. add_to_string(tai_string(tai_simpletypedconst(fvalues[0]).val),tai_simpletypedconst(val).val);
  275. val.free
  276. end
  277. else
  278. internalerror(2014070104);
  279. end;
  280. end
  281. else
  282. fvalues.add(val);
  283. end;
  284. procedure tai_aggregatetypedconst.finish;
  285. begin
  286. if fisstring then
  287. begin
  288. { set the def: an array of char with the same length as the string
  289. data }
  290. if fvalues.count<>1 then
  291. internalerror(2014070105);
  292. tai_simpletypedconst(fvalues[0]).fdef:=
  293. getarraydef(cansichartype,
  294. tai_string(tai_simpletypedconst(fvalues[0]).val).len);
  295. end;
  296. end;
  297. destructor tai_aggregatetypedconst.destroy;
  298. begin
  299. fvalues.free;
  300. inherited destroy;
  301. end;
  302. {*****************************************************************************
  303. ttai_lowleveltypedconstbuilder
  304. *****************************************************************************}
  305. function ttai_lowleveltypedconstbuilder.aggregate_kind(def: tdef): ttypedconstkind;
  306. begin
  307. if (def.typ in [recorddef,filedef,variantdef]) or
  308. is_object(def) or
  309. ((def.typ=procvardef) and
  310. not tprocvardef(def).is_addressonly) then
  311. result:=tck_record
  312. else if ((def.typ=arraydef) and
  313. not is_dynamic_array(def)) or
  314. ((def.typ=setdef) and
  315. not is_smallset(def)) or
  316. is_shortstring(def) then
  317. result:=tck_array
  318. else
  319. result:=tck_simple;
  320. end;
  321. procedure ttai_lowleveltypedconstbuilder.finalize_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: shortint; lab: boolean);
  322. var
  323. prelist: tasmlist;
  324. begin
  325. prelist:=tasmlist.create_without_marker;
  326. maybe_new_object_file(prelist);
  327. { only now add items based on the symbolname, because it may be
  328. modified by the "section" specifier in case of a typed constant }
  329. if section<>sec_none then
  330. new_section(prelist,section,secname,const_align(alignment));
  331. if not lab then
  332. if sym.bind=AB_GLOBAL then
  333. prelist.concat(tai_symbol.Create_Global(sym,0))
  334. else
  335. prelist.concat(tai_symbol.Create(sym,0))
  336. else
  337. prelist.concat(tai_label.Create(tasmlabel(sym)));
  338. { insert the symbol information before the data }
  339. fasmlist.insertlist(prelist);
  340. { end of the symbol }
  341. fasmlist.concat(tai_symbol_end.Createname(sym.name));
  342. { free the temporary list }
  343. prelist.free;
  344. end;
  345. function ttai_lowleveltypedconstbuilder.get_final_asmlist(sym: tasmsymbol; def: tdef; section: TAsmSectiontype; const secname: TSymStr; alignment: longint; lab: boolean): tasmlist;
  346. begin
  347. if not fasmlist_finalized then
  348. begin
  349. finalize_asmlist(sym,def,section,secname,alignment,lab);
  350. fasmlist_finalized:=true;
  351. end;
  352. result:=fasmlist;
  353. end;
  354. class function ttai_lowleveltypedconstbuilder.get_string_symofs(typ: tstringtype; winlikewidestring: boolean): pint;
  355. const
  356. ansistring_header_size =
  357. { encoding }
  358. 2 +
  359. { elesize }
  360. 2 +
  361. {$ifdef cpu64bitaddr}
  362. { alignment }
  363. 4 +
  364. {$endif cpu64bitaddr}
  365. { reference count }
  366. sizeof(pint) +
  367. { length }
  368. sizeof(pint);
  369. unicodestring_header_size = ansistring_header_size;
  370. begin
  371. { darwin's linker does not support negative offsets }
  372. if not(target_info.system in systems_darwin) then
  373. result:=0
  374. else case typ of
  375. st_ansistring:
  376. result:=ansistring_header_size;
  377. st_unicodestring:
  378. result:=unicodestring_header_size;
  379. st_widestring:
  380. if winlikewidestring then
  381. result:=0
  382. else
  383. result:=unicodestring_header_size;
  384. else
  385. result:=0;
  386. end;
  387. end;
  388. constructor ttai_lowleveltypedconstbuilder.create;
  389. begin
  390. inherited create;
  391. fasmlist:=tasmlist.create_without_marker;
  392. { queue is empty }
  393. fqueue_offset:=low(fqueue_offset);
  394. end;
  395. destructor ttai_lowleveltypedconstbuilder.destroy;
  396. begin
  397. { the queue should have been flushed if it was used }
  398. if fqueue_offset<>low(fqueue_offset) then
  399. internalerror(2014062901);
  400. fasmlist.free;
  401. inherited destroy;
  402. end;
  403. procedure ttai_lowleveltypedconstbuilder.emit_tai(p: tai; def: tdef);
  404. begin
  405. { by default, we ignore the def info since we don't care about it at the
  406. the assembler level }
  407. fasmlist.concat(p);
  408. end;
  409. procedure ttai_lowleveltypedconstbuilder.emit_tai_procvar2procdef(p: tai; pvdef: tprocvardef);
  410. begin
  411. { nothing special by default, since we don't care about the type }
  412. emit_tai(p,pvdef);
  413. end;
  414. procedure ttai_lowleveltypedconstbuilder.maybe_begin_aggregate(def: tdef);
  415. begin
  416. { do nothing }
  417. end;
  418. procedure ttai_lowleveltypedconstbuilder.maybe_end_aggregate(def: tdef);
  419. begin
  420. { do nothing }
  421. end;
  422. procedure ttai_lowleveltypedconstbuilder.queue_init(todef: tdef);
  423. begin
  424. { nested call to init? }
  425. if fqueue_offset<>low(fqueue_offset) then
  426. internalerror(2014062101);
  427. fqueue_offset:=0;
  428. end;
  429. procedure ttai_lowleveltypedconstbuilder.queue_vecn(def: tdef; const index: tconstexprint);
  430. var
  431. elelen,
  432. vecbase: asizeint;
  433. v: tconstexprint;
  434. begin
  435. elelen:=1;
  436. vecbase:=0;
  437. case def.typ of
  438. stringdef :
  439. ;
  440. arraydef :
  441. begin
  442. if not is_packed_array(def) then
  443. begin
  444. elelen:=tarraydef(def).elesize;
  445. vecbase:=tarraydef(def).lowrange;
  446. end
  447. else
  448. Message(parser_e_packed_dynamic_open_array);
  449. end;
  450. else
  451. Message(parser_e_illegal_expression);
  452. end;
  453. { Prevent overflow }
  454. v:=index-vecbase;
  455. if (v<int64(low(fqueue_offset))) or (v>int64(high(fqueue_offset))) then
  456. message3(type_e_range_check_error_bounds,tostr(v),tostr(low(fqueue_offset)),tostr(high(fqueue_offset)));
  457. if high(fqueue_offset)-fqueue_offset div elelen>v then
  458. inc(fqueue_offset,elelen*v.svalue)
  459. else
  460. message3(type_e_range_check_error_bounds,tostr(index),tostr(vecbase),tostr(high(fqueue_offset)-fqueue_offset div elelen+vecbase))
  461. end;
  462. procedure ttai_lowleveltypedconstbuilder.queue_subscriptn(def: tabstractrecorddef; vs: tfieldvarsym);
  463. begin
  464. inc(fqueue_offset,vs.fieldoffset);
  465. end;
  466. procedure ttai_lowleveltypedconstbuilder.queue_typeconvn(fromdef, todef: tdef);
  467. begin
  468. { do nothing }
  469. end;
  470. procedure ttai_lowleveltypedconstbuilder.queue_addrn(fromdef, todef: tdef);
  471. begin
  472. { do nothing }
  473. end;
  474. procedure ttai_lowleveltypedconstbuilder.queue_emit_proc(pd: tprocdef);
  475. begin
  476. emit_tai(Tai_const.Createname(pd.mangledname,fqueue_offset),pd);
  477. fqueue_offset:=low(fqueue_offset);
  478. end;
  479. procedure ttai_lowleveltypedconstbuilder.queue_emit_staticvar(vs: tstaticvarsym);
  480. begin
  481. { getpointerdef because we are emitting a pointer to the staticvarsym
  482. data, not the data itself }
  483. emit_tai(Tai_const.Createname(vs.mangledname,fqueue_offset),getpointerdef(vs.vardef));
  484. fqueue_offset:=low(fqueue_offset);
  485. end;
  486. procedure ttai_lowleveltypedconstbuilder.queue_emit_label(l: tlabelsym);
  487. begin
  488. emit_tai(Tai_const.Createname(l.mangledname,fqueue_offset),voidcodepointertype);
  489. fqueue_offset:=low(fqueue_offset);
  490. end;
  491. procedure ttai_lowleveltypedconstbuilder.queue_emit_const(cs: tconstsym);
  492. begin
  493. if cs.consttyp<>constresourcestring then
  494. internalerror(2014062102);
  495. if fqueue_offset<>0 then
  496. internalerror(2014062103);
  497. { warning: update if/when the type of resource strings changes }
  498. emit_tai(Tai_const.Createname(make_mangledname('RESSTR',cs.owner,cs.name),AT_DATA,sizeof(pint)),cansistringtype);
  499. fqueue_offset:=low(fqueue_offset);
  500. end;
  501. procedure ttai_lowleveltypedconstbuilder.queue_emit_asmsym(sym: tasmsymbol; def: tdef);
  502. begin
  503. { getpointerdef, because "sym" represents the address of whatever the
  504. data is }
  505. def:=getpointerdef(def);
  506. emit_tai(Tai_const.Create_sym_offset(sym,fqueue_offset),def);
  507. fqueue_offset:=low(fqueue_offset);
  508. end;
  509. begin
  510. ctai_typedconstbuilder:=ttai_lowleveltypedconstbuilder;
  511. end.