aasmcnst.pas 19 KB

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