llvmdef.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. {
  2. Copyright (c) 2013 by Jonas Maebe
  3. This unit implements some LLVM type helper routines.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {$i fpcdefs.inc}
  18. unit llvmdef;
  19. interface
  20. uses
  21. cclasses,globtype,
  22. aasmbase,
  23. parabase,
  24. symbase,symtype,symdef,
  25. llvmbase;
  26. type
  27. { there are three different circumstances in which procdefs are used:
  28. a) definition of a procdef that's implemented in the current module or
  29. declaration of an external routine that's called in the current one
  30. b) alias declaration of a procdef implemented in the current module
  31. c) defining a procvar type
  32. The main differences between the contexts are:
  33. a) information about sign extension of result type, proc name, parameter names & types
  34. b) no information about sign extension of result type, proc name, no parameter names, parameter types
  35. c) information about sign extension of result type, no proc name, no parameter names, parameter types
  36. }
  37. tllvmprocdefdecltype = (lpd_decl,lpd_alias,lpd_procvar);
  38. { Encode a type into the internal format used by LLVM. }
  39. function llvmencodetype(def: tdef): TSymStr;
  40. { incremental version of llvmencodetype(). "inaggregate" indicates whether
  41. this was a recursive call to get the type of an entity part of an
  42. aggregate type (array, record, ...) }
  43. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  44. { encode a procdef/procvardef into the internal format used by LLVM }
  45. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  46. { incremental version of the above }
  47. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  48. { function result types may have to be represented differently, e.g. a
  49. record consisting of 4 longints must be returned as a record consisting of
  50. two int64's on x86-64. This function is used to create (and reuse)
  51. temporary recorddefs for such purposes.}
  52. function llvmgettemprecorddef(fieldtypes: tfplist; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  53. { get the llvm type corresponding to a parameter, e.g. a record containing
  54. two integer int64 for an arbitrary record split over two individual int64
  55. parameters, or an int32 for an int16 parameter on a platform that requires
  56. such parameters to be zero/sign extended. The second parameter can be used
  57. to get the type before zero/sign extension, as e.g. required to generate
  58. function declarations. }
  59. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  60. { can be used to extract the value extension info from acgpara. Pass in
  61. the def of the cgpara as first parameter and a local variable holding
  62. a copy of the def of the location (value extension only makes sense for
  63. ordinal parameters that are smaller than a single location). The routine
  64. will return the def of the location without sign extension (if applicable)
  65. and the kind of sign extension that was originally performed in the
  66. signext parameter }
  67. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  68. { returns whether a paraloc should be translated into an llvm "byval"
  69. parameter. These are declared as pointers to a particular type, but
  70. usually turned into copies onto the stack. The exact behaviour for
  71. parameters that should be passed in registers is undefined and depends on
  72. the platform, and furthermore this modifier sometimes inhibits
  73. optimizations. As a result,we only use it for aggregate parameters of
  74. which we know that they should be passed on the stack }
  75. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  76. { returns whether a def is representated by an aggregate type in llvm
  77. (struct, array) }
  78. function llvmaggregatetype(def: tdef): boolean;
  79. function llvmconvop(fromsize, tosize: tdef): tllvmop;
  80. { mangle a global identifier so that it's recognised by LLVM as a global
  81. (in the sense of module-global) label and so that it won't mangle the
  82. name further according to platform conventions (we already did that) }
  83. function llvmmangledname(const s: TSymStr): TSymStr;
  84. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  85. implementation
  86. uses
  87. cutils,constexp,
  88. verbose,systems,
  89. fmodule,
  90. symtable,symconst,symsym,
  91. llvmsym,hlcgobj,
  92. defutil,cgbase,paramgr;
  93. {******************************************************************
  94. Type encoding
  95. *******************************************************************}
  96. function llvmaggregatetype(def: tdef): boolean;
  97. begin
  98. result:=
  99. (def.typ in [recorddef,filedef,variantdef]) or
  100. ((def.typ=arraydef) and
  101. not is_dynamic_array(def)) or
  102. ((def.typ=setdef) and
  103. not is_smallset(def)) or
  104. is_shortstring(def) or
  105. is_object(def) or
  106. ((def.typ=procvardef) and
  107. not tprocvardef(def).is_addressonly)
  108. end;
  109. function llvmconvop(fromsize, tosize: tdef): tllvmop;
  110. var
  111. fromregtyp,
  112. toregtyp: tregistertype;
  113. frombytesize,
  114. tobytesize: asizeint;
  115. begin
  116. fromregtyp:=chlcgobj.def2regtyp(fromsize);
  117. toregtyp:=chlcgobj.def2regtyp(tosize);
  118. { int to pointer or vice versa }
  119. if fromregtyp=R_ADDRESSREGISTER then
  120. begin
  121. case toregtyp of
  122. R_INTREGISTER:
  123. result:=la_ptrtoint;
  124. R_ADDRESSREGISTER:
  125. result:=la_bitcast;
  126. else
  127. result:=la_ptrtoint_to_x;
  128. end;
  129. end
  130. else if toregtyp=R_ADDRESSREGISTER then
  131. begin
  132. case fromregtyp of
  133. R_INTREGISTER:
  134. result:=la_inttoptr;
  135. R_ADDRESSREGISTER:
  136. result:=la_bitcast;
  137. else
  138. result:=la_x_to_inttoptr;
  139. end;
  140. end
  141. else
  142. begin
  143. frombytesize:=fromsize.size;
  144. tobytesize:=tosize.size;
  145. { need zero/sign extension, float truncation or plain bitcast? }
  146. if tobytesize<>frombytesize then
  147. begin
  148. case fromregtyp of
  149. R_FPUREGISTER,
  150. R_MMREGISTER:
  151. begin
  152. { todo: update once we support vectors }
  153. if not(toregtyp in [R_FPUREGISTER,R_MMREGISTER]) then
  154. internalerror(2014062203);
  155. if tobytesize<frombytesize then
  156. result:=la_fptrunc
  157. else
  158. result:=la_fpext
  159. end;
  160. else
  161. begin
  162. if tobytesize<frombytesize then
  163. result:=la_trunc
  164. else if is_signed(fromsize) then
  165. { fromsize is signed -> sign extension }
  166. result:=la_sext
  167. else
  168. result:=la_zext;
  169. end;
  170. end;
  171. end
  172. else
  173. result:=la_bitcast;
  174. end;
  175. end;
  176. function llvmmangledname(const s: TSymStr): TSymStr;
  177. begin
  178. result:='@"\01'+s+'"';
  179. end;
  180. function llvmasmsymname(const sym: TAsmSymbol): TSymStr;
  181. begin
  182. { AT_ADDR and AT_LABEL represent labels in the code, which have
  183. a different type in llvm compared to (global) data labels }
  184. if sym.bind=AB_TEMP then
  185. result:='%'+sym.name
  186. else if not(sym.typ in [AT_LABEL,AT_ADDR]) then
  187. result:=llvmmangledname(sym.name)
  188. else
  189. result:='label %'+sym.name;
  190. end;
  191. function llvmbyvalparaloc(paraloc: pcgparalocation): boolean;
  192. begin
  193. { "byval" is broken for register paras on several platforms in llvm
  194. (search for "byval" in llvm's bug tracker). Additionally, it should only
  195. be used to pass aggregate parameters on the stack, because it reportedly
  196. inhibits llvm's midlevel optimizers.
  197. Exception (for now?): parameters that have special shifting
  198. requirements, because modelling those in llvm is not easy (and clang
  199. nor llvm-gcc seem to do so either) }
  200. result:=
  201. ((paraloc^.loc=LOC_REFERENCE) and
  202. llvmaggregatetype(paraloc^.def)) or
  203. (paraloc^.shiftval<>0)
  204. end;
  205. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr); forward;
  206. procedure llvmaddencodedtype_intern(def: tdef; inaggregate, noimplicitderef: boolean; var encodedstr: TSymStr);
  207. begin
  208. case def.typ of
  209. stringdef :
  210. begin
  211. case tstringdef(def).stringtype of
  212. st_widestring,
  213. st_unicodestring:
  214. { the variable does not point to the header, but to a
  215. null-terminated string/array with undefined bounds }
  216. encodedstr:=encodedstr+'i16*';
  217. st_ansistring:
  218. encodedstr:=encodedstr+'i8*';
  219. st_shortstring:
  220. { length byte followed by string bytes }
  221. if tstringdef(def).len>0 then
  222. encodedstr:=encodedstr+'['+tostr(tstringdef(def).len+1)+' x i8]'
  223. else
  224. encodedstr:=encodedstr+'[0 x i8]';
  225. else
  226. internalerror(2013100201);
  227. end;
  228. end;
  229. enumdef:
  230. begin
  231. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  232. end;
  233. orddef :
  234. begin
  235. if is_void(def) then
  236. encodedstr:=encodedstr+'void'
  237. { mainly required because comparison operations return i1, and
  238. otherwise we always have to immediatel extend them to i8 for
  239. no good reason; besides, Pascal booleans can only contain 0
  240. or 1 in valid code anyway (famous last words...) }
  241. else if torddef(def).ordtype=pasbool8 then
  242. encodedstr:=encodedstr+'i1'
  243. else
  244. encodedstr:=encodedstr+'i'+tostr(def.size*8);
  245. end;
  246. pointerdef :
  247. begin
  248. if is_voidpointer(def) then
  249. encodedstr:=encodedstr+'i8*'
  250. else
  251. begin
  252. llvmaddencodedtype_intern(tpointerdef(def).pointeddef,inaggregate,false,encodedstr);
  253. encodedstr:=encodedstr+'*';
  254. end;
  255. end;
  256. floatdef :
  257. begin
  258. case tfloatdef(def).floattype of
  259. s32real:
  260. encodedstr:=encodedstr+'float';
  261. s64real:
  262. encodedstr:=encodedstr+'double';
  263. { necessary to be able to force our own size/alignment }
  264. s80real:
  265. { prevent llvm from allocating the standard ABI size for
  266. extended }
  267. if inaggregate then
  268. encodedstr:=encodedstr+'[10 x i8]'
  269. else
  270. encodedstr:=encodedstr+'x86_fp80';
  271. sc80real:
  272. encodedstr:=encodedstr+'x86_fp80';
  273. s64comp,
  274. s64currency:
  275. encodedstr:=encodedstr+'i64';
  276. s128real:
  277. {$if defined(powerpc) or defined(powerpc128)}
  278. encodedstr:=encodedstr+'ppc_fp128';
  279. {$else}
  280. encodedstr:=encodedstr+'fp128';
  281. {$endif}
  282. else
  283. internalerror(2013100202);
  284. end;
  285. end;
  286. filedef :
  287. begin
  288. case tfiledef(def).filetyp of
  289. ft_text :
  290. llvmaddencodedtype_intern(search_system_type('TEXTREC').typedef,inaggregate,false,encodedstr);
  291. ft_typed,
  292. ft_untyped :
  293. llvmaddencodedtype_intern(search_system_type('FILEREC').typedef,inaggregate,false,encodedstr);
  294. else
  295. internalerror(2013100203);
  296. end;
  297. end;
  298. recorddef :
  299. begin
  300. llvmaddencodedabstractrecordtype(trecorddef(def),encodedstr);
  301. end;
  302. variantdef :
  303. begin
  304. llvmaddencodedtype_intern(search_system_type('TVARDATA').typedef,inaggregate,false,encodedstr);
  305. end;
  306. classrefdef :
  307. begin
  308. { todo: define proper type for VMT and use that }
  309. encodedstr:=encodedstr+'i8*';
  310. end;
  311. setdef :
  312. begin
  313. { just an array as far as llvm is concerned; don't use a "packed
  314. array of i1" or so, this requires special support in backends
  315. and guarantees nothing about the internal format }
  316. if is_smallset(def) then
  317. llvmaddencodedtype_intern(cgsize_orddef(def_cgsize(def)),inaggregate,false,encodedstr)
  318. else
  319. encodedstr:=encodedstr+'['+tostr(tsetdef(def).size)+' x i8]';
  320. end;
  321. formaldef :
  322. begin
  323. { var/const/out x }
  324. encodedstr:=encodedstr+'i8*';
  325. end;
  326. arraydef :
  327. begin
  328. if is_array_of_const(def) then
  329. begin
  330. encodedstr:=encodedstr+'[0 x ';
  331. llvmaddencodedtype_intern(search_system_type('TVARREC').typedef,true,false,encodedstr);
  332. encodedstr:=encodedstr+']';
  333. end
  334. else if is_open_array(def) then
  335. begin
  336. encodedstr:=encodedstr+'[0 x ';
  337. llvmaddencodedtype_intern(tarraydef(def).elementdef,true,false,encodedstr);
  338. encodedstr:=encodedstr+']';
  339. end
  340. else if is_dynamic_array(def) then
  341. begin
  342. llvmaddencodedtype_intern(tarraydef(def).elementdef,inaggregate,false,encodedstr);
  343. encodedstr:=encodedstr+'*';
  344. end
  345. else if is_packed_array(def) then
  346. begin
  347. encodedstr:=encodedstr+'['+tostr(tarraydef(def).size div tarraydef(def).elementdef.packedbitsize)+' x ';
  348. { encode as an array of integers with the size on which we
  349. perform the packedbits operations }
  350. llvmaddencodedtype_intern(cgsize_orddef(int_cgsize(packedbitsloadsize(tarraydef(def).elementdef.packedbitsize))),true,false,encodedstr);
  351. encodedstr:=encodedstr+']';
  352. end
  353. else
  354. begin
  355. encodedstr:=encodedstr+'['+tostr(tarraydef(def).elecount)+' x ';
  356. llvmaddencodedtype_intern(tarraydef(def).elementdef,true,false,encodedstr);
  357. encodedstr:=encodedstr+']';
  358. end;
  359. end;
  360. procdef,
  361. procvardef :
  362. begin
  363. if (def.typ=procdef) or
  364. tprocvardef(def).is_addressonly then
  365. begin
  366. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  367. if def.typ=procvardef then
  368. encodedstr:=encodedstr+'*';
  369. end
  370. else
  371. begin
  372. encodedstr:=encodedstr+'{';
  373. { code pointer }
  374. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  375. { data pointer (maybe todo: generate actual layout if
  376. available) }
  377. encodedstr:=encodedstr+'*, i8*}';
  378. end;
  379. end;
  380. objectdef :
  381. case tobjectdef(def).objecttype of
  382. odt_class,
  383. odt_objcclass,
  384. odt_object,
  385. odt_cppclass:
  386. begin
  387. { for now don't handle fields yet }
  388. encodedstr:=encodedstr+'{[i8 x '+tostr(def.size)+']}';
  389. if not noimplicitderef and
  390. is_implicit_pointer_object_type(def) then
  391. encodedstr:=encodedstr+'*'
  392. end;
  393. odt_interfacecom,
  394. odt_interfacecom_function,
  395. odt_interfacecom_property,
  396. odt_interfacecorba,
  397. odt_dispinterface,
  398. odt_objcprotocol:
  399. begin
  400. { opaque for now }
  401. encodedstr:=encodedstr+'i8*'
  402. end;
  403. else
  404. internalerror(2013100601);
  405. end;
  406. undefineddef,
  407. errordef :
  408. internalerror(2013100604);
  409. else
  410. internalerror(2013100603);
  411. end;
  412. end;
  413. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  414. begin
  415. llvmaddencodedtype_intern(def,inaggregate,false,encodedstr);
  416. end;
  417. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  418. var
  419. st: tllvmshadowsymtable;
  420. symdeflist: tfpobjectlist;
  421. i: longint;
  422. begin
  423. st:=tabstractrecordsymtable(def.symtable).llvmst;
  424. symdeflist:=st.symdeflist;
  425. if tabstractrecordsymtable(def.symtable).usefieldalignment<>C_alignment then
  426. encodedstr:=encodedstr+'<';
  427. encodedstr:=encodedstr+'{ ';
  428. if symdeflist.count>0 then
  429. begin
  430. i:=0;
  431. if (def.typ=objectdef) and
  432. assigned(tobjectdef(def).childof) and
  433. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  434. begin
  435. { insert the struct for the class rather than a pointer to the struct }
  436. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  437. internalerror(2008070601);
  438. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,true,true,encodedstr);
  439. inc(i);
  440. end;
  441. while i<symdeflist.count do
  442. begin
  443. if i<>0 then
  444. encodedstr:=encodedstr+', ';
  445. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,true,false,encodedstr);
  446. inc(i);
  447. end;
  448. end;
  449. encodedstr:=encodedstr+' }';
  450. if tabstractrecordsymtable(def.symtable).usefieldalignment<>C_alignment then
  451. encodedstr:=encodedstr+'>';
  452. end;
  453. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  454. begin
  455. { implicit zero/sign extension for ABI compliance? (yes, if the size
  456. of a paraloc is larger than the size of the entire parameter) }
  457. if is_ordinal(paradef) and
  458. is_ordinal(paralocdef) and
  459. (paradef.size<paralocdef.size) then
  460. begin
  461. paralocdef:=paradef;
  462. if is_signed(paradef) then
  463. signext:=lve_signext
  464. else
  465. signext:=lve_zeroext
  466. end
  467. else
  468. signext:=lve_none;
  469. end;
  470. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname: boolean; var first: boolean; var encodedstr: TSymStr);
  471. var
  472. paraloc: PCGParaLocation;
  473. signext: tllvmvalueextension;
  474. usedef: tdef;
  475. begin
  476. if (proccalloption in cdecl_pocalls) and
  477. is_array_of_const(hp.vardef) then
  478. begin
  479. if not first then
  480. encodedstr:=encodedstr+', '
  481. else
  482. first:=false;
  483. encodedstr:=encodedstr+'...';
  484. exit
  485. end;
  486. paraloc:=hp.paraloc[calleeside].location;
  487. repeat
  488. usedef:=paraloc^.def;
  489. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  490. { implicit zero/sign extension for ABI compliance? }
  491. if not first then
  492. encodedstr:=encodedstr+', '
  493. else
  494. first:=false;
  495. llvmaddencodedtype(usedef,false,encodedstr);
  496. { in case signextstr<>'', there should be only one paraloc -> no need
  497. to clear (reason: it means that the paraloc is larger than the
  498. original parameter) }
  499. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  500. { sret: hidden pointer for structured function result }
  501. if vo_is_funcret in hp.varoptions then
  502. encodedstr:=encodedstr+' sret'
  503. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  504. llvmbyvalparaloc(paraloc) then
  505. encodedstr:=encodedstr+'* byval';
  506. if withparaname then
  507. begin
  508. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  509. internalerror(2014010803);
  510. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  511. end;
  512. paraloc:=paraloc^.next;
  513. until not assigned(paraloc);
  514. end;
  515. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  516. begin
  517. result:='';
  518. llvmaddencodedproctype(def,customname,pddecltype,result);
  519. end;
  520. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  521. var
  522. usedef: tdef;
  523. paranr: longint;
  524. hp: tparavarsym;
  525. signext: tllvmvalueextension;
  526. first: boolean;
  527. begin
  528. def.init_paraloc_info(calleeside);
  529. first:=true;
  530. { function result (return-by-ref is handled explicitly) }
  531. if not paramanager.ret_in_param(def.returndef,def) then
  532. begin
  533. usedef:=llvmgetcgparadef(def.funcretloc[calleeside],false);
  534. llvmextractvalueextinfo(def.returndef,usedef,signext);
  535. { specifying result sign extention information for an alias causes
  536. an error for some reason }
  537. if pddecltype in [lpd_decl,lpd_procvar] then
  538. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  539. encodedstr:=encodedstr+' ';
  540. llvmaddencodedtype_intern(usedef,false,false,encodedstr);
  541. end
  542. else
  543. begin
  544. encodedstr:=encodedstr+' ';
  545. llvmaddencodedtype(voidtype,false,encodedstr);
  546. end;
  547. encodedstr:=encodedstr+' ';
  548. { add procname? }
  549. if (pddecltype in [lpd_decl]) and
  550. (def.typ=procdef) then
  551. if customname='' then
  552. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  553. else
  554. encodedstr:=encodedstr+llvmmangledname(customname);
  555. encodedstr:=encodedstr+'(';
  556. { parameters }
  557. first:=true;
  558. for paranr:=0 to def.paras.count-1 do
  559. begin
  560. hp:=tparavarsym(def.paras[paranr]);
  561. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_decl],first,encodedstr);
  562. end;
  563. if po_varargs in def.procoptions then
  564. begin
  565. if not first then
  566. encodedstr:=encodedstr+', ';
  567. encodedstr:=encodedstr+'...';
  568. end;
  569. encodedstr:=encodedstr+')'
  570. end;
  571. function llvmgettemprecorddef(fieldtypes: tfplist; packrecords, recordalignmin, maxcrecordalign: shortint): trecorddef;
  572. var
  573. i: longint;
  574. res: PHashSetItem;
  575. oldsymtablestack: tsymtablestack;
  576. hrecst: trecordsymtable;
  577. hdef: tdef;
  578. hrecdef: trecorddef;
  579. sym: tfieldvarsym;
  580. typename: string;
  581. begin
  582. typename:='$llvmstruct_';
  583. for i:=0 to fieldtypes.count-1 do
  584. begin
  585. hdef:=tdef(fieldtypes[i]);
  586. case hdef.typ of
  587. orddef:
  588. case torddef(hdef).ordtype of
  589. s8bit,
  590. u8bit:
  591. typename:=typename+'i8';
  592. s16bit,
  593. u16bit:
  594. typename:=typename+'i16';
  595. s32bit,
  596. u32bit:
  597. typename:=typename+'i32';
  598. s64bit,
  599. u64bit:
  600. typename:=typename+'i64';
  601. else
  602. { other types should not appear currently, add as needed }
  603. internalerror(2014012001);
  604. end;
  605. floatdef:
  606. case tfloatdef(hdef).floattype of
  607. s32real:
  608. typename:=typename+'f32';
  609. s64real:
  610. typename:=typename+'f64';
  611. else
  612. { other types should not appear currently, add as needed }
  613. internalerror(2014012008);
  614. end;
  615. else
  616. { other types should not appear currently, add as needed }
  617. internalerror(2014012009);
  618. end;
  619. end;
  620. if not assigned(current_module) then
  621. internalerror(2014012002);
  622. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  623. if not assigned(res^.Data) then
  624. begin
  625. res^.Data:=crecorddef.create_global_internal(typename,packrecords,
  626. recordalignmin,maxcrecordalign);
  627. trecorddef(res^.Data).add_fields_from_deflist(fieldtypes);
  628. end;
  629. result:=trecorddef(res^.Data);
  630. end;
  631. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  632. var
  633. retdeflist: tfplist;
  634. retloc: pcgparalocation;
  635. usedef: tdef;
  636. valueext: tllvmvalueextension;
  637. begin
  638. { single location }
  639. if not assigned(cgpara.location^.next) then
  640. begin
  641. { def of the location, except in case of zero/sign-extension }
  642. usedef:=cgpara.location^.def;
  643. if beforevalueext then
  644. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  645. result:=usedef;
  646. exit
  647. end;
  648. { multiple locations -> create temp record }
  649. retdeflist:=tfplist.create;
  650. retloc:=cgpara.location;
  651. repeat
  652. retdeflist.add(retloc^.def);
  653. retloc:=retloc^.next;
  654. until not assigned(retloc);
  655. result:=llvmgettemprecorddef(retdeflist,C_alignment,
  656. targetinfos[target_info.system]^.alignment.recordalignmin,
  657. targetinfos[target_info.system]^.alignment.maxCrecordalign);
  658. end;
  659. function llvmencodetype(def: tdef): TSymStr;
  660. begin
  661. result:='';
  662. llvmaddencodedtype(def,false,result);
  663. end;
  664. end.