llvmdef.pas 30 KB

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