llvmdef.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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: 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. encodedstr:=encodedstr+'*';
  368. end
  369. else
  370. begin
  371. encodedstr:=encodedstr+'{';
  372. { code pointer }
  373. llvmaddencodedproctype(tabstractprocdef(def),'',lpd_procvar,encodedstr);
  374. { data pointer (maybe todo: generate actual layout if
  375. available) }
  376. encodedstr:=encodedstr+'*, i8*}';
  377. end;
  378. end;
  379. objectdef :
  380. case tobjectdef(def).objecttype of
  381. odt_class,
  382. odt_objcclass,
  383. odt_object,
  384. odt_cppclass:
  385. begin
  386. { for now don't handle fields yet }
  387. encodedstr:=encodedstr+'{[i8 x '+tostr(def.size)+']}';
  388. if not noimplicitderef and
  389. is_implicit_pointer_object_type(def) then
  390. encodedstr:=encodedstr+'*'
  391. end;
  392. odt_interfacecom,
  393. odt_interfacecom_function,
  394. odt_interfacecom_property,
  395. odt_interfacecorba,
  396. odt_dispinterface,
  397. odt_objcprotocol:
  398. begin
  399. { opaque for now }
  400. encodedstr:=encodedstr+'i8*'
  401. end;
  402. else
  403. internalerror(2013100601);
  404. end;
  405. undefineddef,
  406. errordef :
  407. internalerror(2013100604);
  408. else
  409. internalerror(2013100603);
  410. end;
  411. end;
  412. procedure llvmaddencodedtype(def: tdef; inaggregate: boolean; var encodedstr: TSymStr);
  413. begin
  414. llvmaddencodedtype_intern(def,inaggregate,false,encodedstr);
  415. end;
  416. procedure llvmaddencodedabstractrecordtype(def: tabstractrecorddef; var encodedstr: TSymStr);
  417. var
  418. st: tllvmshadowsymtable;
  419. symdeflist: tfpobjectlist;
  420. i: longint;
  421. begin
  422. st:=tabstractrecordsymtable(def.symtable).llvmst;
  423. symdeflist:=st.symdeflist;
  424. if tabstractrecordsymtable(def.symtable).usefieldalignment<>C_alignment then
  425. encodedstr:=encodedstr+'<';
  426. encodedstr:=encodedstr+'{ ';
  427. if symdeflist.count>0 then
  428. begin
  429. i:=0;
  430. if (def.typ=objectdef) and
  431. assigned(tobjectdef(def).childof) and
  432. is_class_or_interface_or_dispinterface(tllvmshadowsymtableentry(symdeflist[0]).def) then
  433. begin
  434. { insert the struct for the class rather than a pointer to the struct }
  435. if (tllvmshadowsymtableentry(symdeflist[0]).def.typ<>objectdef) then
  436. internalerror(2008070601);
  437. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[0]).def,true,true,encodedstr);
  438. inc(i);
  439. end;
  440. while i<symdeflist.count do
  441. begin
  442. if i<>0 then
  443. encodedstr:=encodedstr+', ';
  444. llvmaddencodedtype_intern(tllvmshadowsymtableentry(symdeflist[i]).def,true,false,encodedstr);
  445. inc(i);
  446. end;
  447. end;
  448. encodedstr:=encodedstr+' }';
  449. if tabstractrecordsymtable(def.symtable).usefieldalignment<>C_alignment then
  450. encodedstr:=encodedstr+'>';
  451. end;
  452. procedure llvmextractvalueextinfo(paradef: tdef; var paralocdef: tdef; out signext: tllvmvalueextension);
  453. begin
  454. { implicit zero/sign extension for ABI compliance? (yes, if the size
  455. of a paraloc is larger than the size of the entire parameter) }
  456. if is_ordinal(paradef) and
  457. is_ordinal(paralocdef) and
  458. (paradef.size<paralocdef.size) then
  459. begin
  460. paralocdef:=paradef;
  461. if is_signed(paradef) then
  462. signext:=lve_signext
  463. else
  464. signext:=lve_zeroext
  465. end
  466. else
  467. signext:=lve_none;
  468. end;
  469. procedure llvmaddencodedparaloctype(hp: tparavarsym; proccalloption: tproccalloption; withparaname: boolean; var first: boolean; var encodedstr: TSymStr);
  470. var
  471. paraloc: PCGParaLocation;
  472. signext: tllvmvalueextension;
  473. usedef: tdef;
  474. begin
  475. if (proccalloption in cdecl_pocalls) and
  476. is_array_of_const(hp.vardef) then
  477. begin
  478. if not first then
  479. encodedstr:=encodedstr+', '
  480. else
  481. first:=false;
  482. encodedstr:=encodedstr+'...';
  483. exit
  484. end;
  485. paraloc:=hp.paraloc[calleeside].location;
  486. repeat
  487. usedef:=paraloc^.def;
  488. llvmextractvalueextinfo(hp.vardef,usedef,signext);
  489. { implicit zero/sign extension for ABI compliance? }
  490. if not first then
  491. encodedstr:=encodedstr+', '
  492. else
  493. first:=false;
  494. llvmaddencodedtype(usedef,false,encodedstr);
  495. { in case signextstr<>'', there should be only one paraloc -> no need
  496. to clear (reason: it means that the paraloc is larger than the
  497. original parameter) }
  498. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  499. { sret: hidden pointer for structured function result }
  500. if vo_is_funcret in hp.varoptions then
  501. encodedstr:=encodedstr+' sret'
  502. else if not paramanager.push_addr_param(hp.varspez,hp.vardef,proccalloption) and
  503. llvmbyvalparaloc(paraloc) then
  504. encodedstr:=encodedstr+'* byval';
  505. if withparaname then
  506. begin
  507. if paraloc^.llvmloc.loc<>LOC_REFERENCE then
  508. internalerror(2014010803);
  509. encodedstr:=encodedstr+' '+llvmasmsymname(paraloc^.llvmloc.sym);
  510. end;
  511. paraloc:=paraloc^.next;
  512. until not assigned(paraloc);
  513. end;
  514. function llvmencodeproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype): TSymStr;
  515. begin
  516. result:='';
  517. llvmaddencodedproctype(def,customname,pddecltype,result);
  518. end;
  519. procedure llvmaddencodedproctype(def: tabstractprocdef; const customname: TSymStr; pddecltype: tllvmprocdefdecltype; var encodedstr: TSymStr);
  520. var
  521. usedef: tdef;
  522. paranr: longint;
  523. hp: tparavarsym;
  524. signext: tllvmvalueextension;
  525. first: boolean;
  526. begin
  527. def.init_paraloc_info(calleeside);
  528. first:=true;
  529. { function result (return-by-ref is handled explicitly) }
  530. if not paramanager.ret_in_param(def.returndef,def) then
  531. begin
  532. usedef:=llvmgetcgparadef(def.funcretloc[calleeside],false);
  533. llvmextractvalueextinfo(def.returndef,usedef,signext);
  534. { specifying result sign extention information for an alias causes
  535. an error for some reason }
  536. if pddecltype in [lpd_decl,lpd_procvar] then
  537. encodedstr:=encodedstr+llvmvalueextension2str[signext];
  538. encodedstr:=encodedstr+' ';
  539. llvmaddencodedtype_intern(usedef,false,false,encodedstr);
  540. end
  541. else
  542. begin
  543. encodedstr:=encodedstr+' ';
  544. llvmaddencodedtype(voidtype,false,encodedstr);
  545. end;
  546. encodedstr:=encodedstr+' ';
  547. { add procname? }
  548. if (pddecltype in [lpd_decl]) and
  549. (def.typ=procdef) then
  550. if customname='' then
  551. encodedstr:=encodedstr+llvmmangledname(tprocdef(def).mangledname)
  552. else
  553. encodedstr:=encodedstr+llvmmangledname(customname);
  554. encodedstr:=encodedstr+'(';
  555. { parameters }
  556. first:=true;
  557. for paranr:=0 to def.paras.count-1 do
  558. begin
  559. hp:=tparavarsym(def.paras[paranr]);
  560. llvmaddencodedparaloctype(hp,def.proccalloption,pddecltype in [lpd_decl],first,encodedstr);
  561. end;
  562. if po_varargs in def.procoptions then
  563. begin
  564. if not first then
  565. encodedstr:=encodedstr+', ';
  566. encodedstr:=encodedstr+'...';
  567. end;
  568. encodedstr:=encodedstr+')'
  569. end;
  570. function llvmgettemprecorddef(fieldtypes: tfplist; packrecords: shortint): trecorddef;
  571. var
  572. i: longint;
  573. res: PHashSetItem;
  574. oldsymtablestack: tsymtablestack;
  575. hrecst: trecordsymtable;
  576. hdef: tdef;
  577. hrecdef: trecorddef;
  578. sym: tfieldvarsym;
  579. typename: string;
  580. begin
  581. typename:='$llvmstruct_';
  582. for i:=0 to fieldtypes.count-1 do
  583. begin
  584. hdef:=tdef(fieldtypes[i]);
  585. case hdef.typ of
  586. orddef:
  587. case torddef(hdef).ordtype of
  588. s8bit,
  589. u8bit:
  590. typename:=typename+'i8';
  591. s16bit,
  592. u16bit:
  593. typename:=typename+'i16';
  594. s32bit,
  595. u32bit:
  596. typename:=typename+'i32';
  597. s64bit,
  598. u64bit:
  599. typename:=typename+'i64';
  600. else
  601. { other types should not appear currently, add as needed }
  602. internalerror(2014012001);
  603. end;
  604. floatdef:
  605. case tfloatdef(hdef).floattype of
  606. s32real:
  607. typename:=typename+'f32';
  608. s64real:
  609. typename:=typename+'f64';
  610. else
  611. { other types should not appear currently, add as needed }
  612. internalerror(2014012008);
  613. end;
  614. else
  615. { other types should not appear currently, add as needed }
  616. internalerror(2014012009);
  617. end;
  618. end;
  619. if not assigned(current_module) then
  620. internalerror(2014012002);
  621. res:=current_module.llvmdefs.FindOrAdd(@typename[1],length(typename));
  622. if not assigned(res^.Data) then
  623. res^.Data:=crecorddef.create_global_from_deflist(typename,fieldtypes,packrecords);
  624. result:=trecorddef(res^.Data);
  625. end;
  626. function llvmgetcgparadef(const cgpara: tcgpara; beforevalueext: boolean): tdef;
  627. var
  628. retdeflist: tfplist;
  629. retloc: pcgparalocation;
  630. usedef: tdef;
  631. valueext: tllvmvalueextension;
  632. begin
  633. { single location }
  634. if not assigned(cgpara.location^.next) then
  635. begin
  636. { def of the location, except in case of zero/sign-extension }
  637. usedef:=cgpara.location^.def;
  638. if beforevalueext then
  639. llvmextractvalueextinfo(cgpara.def,usedef,valueext);
  640. result:=usedef;
  641. exit
  642. end;
  643. { multiple locations -> create temp record }
  644. retdeflist:=tfplist.create;
  645. retloc:=cgpara.location;
  646. repeat
  647. retdeflist.add(retloc^.def);
  648. retloc:=retloc^.next;
  649. until not assigned(retloc);
  650. result:=llvmgettemprecorddef(retdeflist,C_alignment);
  651. end;
  652. function llvmencodetype(def: tdef): TSymStr;
  653. begin
  654. result:='';
  655. llvmaddencodedtype(def,false,result);
  656. end;
  657. end.