llvmdef.pas 27 KB

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