go-gcc.cc 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127
  1. // go-gcc.cc -- Go frontend to gcc IR.
  2. // Copyright (C) 2011-2015 Free Software Foundation, Inc.
  3. // Contributed by Ian Lance Taylor, Google.
  4. // This file is part of GCC.
  5. // GCC is free software; you can redistribute it and/or modify it under
  6. // the terms of the GNU General Public License as published by the Free
  7. // Software Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. // WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. // for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with GCC; see the file COPYING3. If not see
  15. // <http://www.gnu.org/licenses/>.
  16. #include "go-system.h"
  17. // This has to be included outside of extern "C", so we have to
  18. // include it here before tree.h includes it later.
  19. #include <gmp.h>
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "options.h"
  28. #include "wide-int.h"
  29. #include "inchash.h"
  30. #include "tree.h"
  31. #include "fold-const.h"
  32. #include "stringpool.h"
  33. #include "stor-layout.h"
  34. #include "varasm.h"
  35. #include "tree-iterator.h"
  36. #include "hash-map.h"
  37. #include "is-a.h"
  38. #include "plugin-api.h"
  39. #include "tm.h"
  40. #include "hard-reg-set.h"
  41. #include "input.h"
  42. #include "function.h"
  43. #include "ipa-ref.h"
  44. #include "cgraph.h"
  45. #include "convert.h"
  46. #include "gimple-expr.h"
  47. #include "gimplify.h"
  48. #include "langhooks.h"
  49. #include "toplev.h"
  50. #include "output.h"
  51. #include "real.h"
  52. #include "realmpfr.h"
  53. #include "builtins.h"
  54. #include "go-c.h"
  55. #include "gogo.h"
  56. #include "backend.h"
  57. // A class wrapping a tree.
  58. class Gcc_tree
  59. {
  60. public:
  61. Gcc_tree(tree t)
  62. : t_(t)
  63. { }
  64. tree
  65. get_tree() const
  66. { return this->t_; }
  67. void
  68. set_tree(tree t)
  69. { this->t_ = t; }
  70. private:
  71. tree t_;
  72. };
  73. // In gcc, types, expressions, and statements are all trees.
  74. class Btype : public Gcc_tree
  75. {
  76. public:
  77. Btype(tree t)
  78. : Gcc_tree(t)
  79. { }
  80. };
  81. class Bexpression : public Gcc_tree
  82. {
  83. public:
  84. Bexpression(tree t)
  85. : Gcc_tree(t)
  86. { }
  87. };
  88. class Bstatement : public Gcc_tree
  89. {
  90. public:
  91. Bstatement(tree t)
  92. : Gcc_tree(t)
  93. { }
  94. };
  95. class Bfunction : public Gcc_tree
  96. {
  97. public:
  98. Bfunction(tree t)
  99. : Gcc_tree(t)
  100. { }
  101. };
  102. class Bblock : public Gcc_tree
  103. {
  104. public:
  105. Bblock(tree t)
  106. : Gcc_tree(t)
  107. { }
  108. };
  109. class Bvariable : public Gcc_tree
  110. {
  111. public:
  112. Bvariable(tree t)
  113. : Gcc_tree(t)
  114. { }
  115. };
  116. class Blabel : public Gcc_tree
  117. {
  118. public:
  119. Blabel(tree t)
  120. : Gcc_tree(t)
  121. { }
  122. };
  123. // This file implements the interface between the Go frontend proper
  124. // and the gcc IR. This implements specific instantiations of
  125. // abstract classes defined by the Go frontend proper. The Go
  126. // frontend proper class methods of these classes to generate the
  127. // backend representation.
  128. class Gcc_backend : public Backend
  129. {
  130. public:
  131. Gcc_backend();
  132. // Types.
  133. Btype*
  134. error_type()
  135. { return this->make_type(error_mark_node); }
  136. Btype*
  137. void_type()
  138. { return this->make_type(void_type_node); }
  139. Btype*
  140. bool_type()
  141. { return this->make_type(boolean_type_node); }
  142. Btype*
  143. integer_type(bool, int);
  144. Btype*
  145. float_type(int);
  146. Btype*
  147. complex_type(int);
  148. Btype*
  149. pointer_type(Btype*);
  150. Btype*
  151. function_type(const Btyped_identifier&,
  152. const std::vector<Btyped_identifier>&,
  153. const std::vector<Btyped_identifier>&,
  154. Btype*,
  155. const Location);
  156. Btype*
  157. struct_type(const std::vector<Btyped_identifier>&);
  158. Btype*
  159. array_type(Btype*, Bexpression*);
  160. Btype*
  161. placeholder_pointer_type(const std::string&, Location, bool);
  162. bool
  163. set_placeholder_pointer_type(Btype*, Btype*);
  164. bool
  165. set_placeholder_function_type(Btype*, Btype*);
  166. Btype*
  167. placeholder_struct_type(const std::string&, Location);
  168. bool
  169. set_placeholder_struct_type(Btype* placeholder,
  170. const std::vector<Btyped_identifier>&);
  171. Btype*
  172. placeholder_array_type(const std::string&, Location);
  173. bool
  174. set_placeholder_array_type(Btype*, Btype*, Bexpression*);
  175. Btype*
  176. named_type(const std::string&, Btype*, Location);
  177. Btype*
  178. circular_pointer_type(Btype*, bool);
  179. bool
  180. is_circular_pointer_type(Btype*);
  181. int64_t
  182. type_size(Btype*);
  183. int64_t
  184. type_alignment(Btype*);
  185. int64_t
  186. type_field_alignment(Btype*);
  187. int64_t
  188. type_field_offset(Btype*, size_t index);
  189. // Expressions.
  190. Bexpression*
  191. zero_expression(Btype*);
  192. Bexpression*
  193. error_expression()
  194. { return this->make_expression(error_mark_node); }
  195. Bexpression*
  196. nil_pointer_expression()
  197. { return this->make_expression(null_pointer_node); }
  198. Bexpression*
  199. var_expression(Bvariable* var, Location);
  200. Bexpression*
  201. indirect_expression(Btype*, Bexpression* expr, bool known_valid, Location);
  202. Bexpression*
  203. named_constant_expression(Btype* btype, const std::string& name,
  204. Bexpression* val, Location);
  205. Bexpression*
  206. integer_constant_expression(Btype* btype, mpz_t val);
  207. Bexpression*
  208. float_constant_expression(Btype* btype, mpfr_t val);
  209. Bexpression*
  210. complex_constant_expression(Btype* btype, mpc_t val);
  211. Bexpression*
  212. string_constant_expression(const std::string& val);
  213. Bexpression*
  214. boolean_constant_expression(bool val);
  215. Bexpression*
  216. real_part_expression(Bexpression* bcomplex, Location);
  217. Bexpression*
  218. imag_part_expression(Bexpression* bcomplex, Location);
  219. Bexpression*
  220. complex_expression(Bexpression* breal, Bexpression* bimag, Location);
  221. Bexpression*
  222. convert_expression(Btype* type, Bexpression* expr, Location);
  223. Bexpression*
  224. function_code_expression(Bfunction*, Location);
  225. Bexpression*
  226. address_expression(Bexpression*, Location);
  227. Bexpression*
  228. struct_field_expression(Bexpression*, size_t, Location);
  229. Bexpression*
  230. compound_expression(Bstatement*, Bexpression*, Location);
  231. Bexpression*
  232. conditional_expression(Btype*, Bexpression*, Bexpression*, Bexpression*,
  233. Location);
  234. Bexpression*
  235. unary_expression(Operator, Bexpression*, Location);
  236. Bexpression*
  237. binary_expression(Operator, Bexpression*, Bexpression*, Location);
  238. Bexpression*
  239. constructor_expression(Btype*, const std::vector<Bexpression*>&, Location);
  240. Bexpression*
  241. array_constructor_expression(Btype*, const std::vector<unsigned long>&,
  242. const std::vector<Bexpression*>&, Location);
  243. Bexpression*
  244. pointer_offset_expression(Bexpression* base, Bexpression* offset, Location);
  245. Bexpression*
  246. array_index_expression(Bexpression* array, Bexpression* index, Location);
  247. Bexpression*
  248. call_expression(Bexpression* fn, const std::vector<Bexpression*>& args,
  249. Bexpression* static_chain, Location);
  250. // Statements.
  251. Bstatement*
  252. error_statement()
  253. { return this->make_statement(error_mark_node); }
  254. Bstatement*
  255. expression_statement(Bexpression*);
  256. Bstatement*
  257. init_statement(Bvariable* var, Bexpression* init);
  258. Bstatement*
  259. assignment_statement(Bexpression* lhs, Bexpression* rhs, Location);
  260. Bstatement*
  261. return_statement(Bfunction*, const std::vector<Bexpression*>&,
  262. Location);
  263. Bstatement*
  264. if_statement(Bexpression* condition, Bblock* then_block, Bblock* else_block,
  265. Location);
  266. Bstatement*
  267. switch_statement(Bfunction* function, Bexpression* value,
  268. const std::vector<std::vector<Bexpression*> >& cases,
  269. const std::vector<Bstatement*>& statements,
  270. Location);
  271. Bstatement*
  272. compound_statement(Bstatement*, Bstatement*);
  273. Bstatement*
  274. statement_list(const std::vector<Bstatement*>&);
  275. Bstatement*
  276. exception_handler_statement(Bstatement* bstat, Bstatement* except_stmt,
  277. Bstatement* finally_stmt, Location);
  278. // Blocks.
  279. Bblock*
  280. block(Bfunction*, Bblock*, const std::vector<Bvariable*>&,
  281. Location, Location);
  282. void
  283. block_add_statements(Bblock*, const std::vector<Bstatement*>&);
  284. Bstatement*
  285. block_statement(Bblock*);
  286. // Variables.
  287. Bvariable*
  288. error_variable()
  289. { return new Bvariable(error_mark_node); }
  290. Bvariable*
  291. global_variable(const std::string& package_name,
  292. const std::string& pkgpath,
  293. const std::string& name,
  294. Btype* btype,
  295. bool is_external,
  296. bool is_hidden,
  297. bool in_unique_section,
  298. Location location);
  299. void
  300. global_variable_set_init(Bvariable*, Bexpression*);
  301. Bvariable*
  302. local_variable(Bfunction*, const std::string&, Btype*, bool,
  303. Location);
  304. Bvariable*
  305. parameter_variable(Bfunction*, const std::string&, Btype*, bool,
  306. Location);
  307. Bvariable*
  308. static_chain_variable(Bfunction*, const std::string&, Btype*, Location);
  309. Bvariable*
  310. temporary_variable(Bfunction*, Bblock*, Btype*, Bexpression*, bool,
  311. Location, Bstatement**);
  312. Bvariable*
  313. implicit_variable(const std::string&, Btype*, bool, bool, bool,
  314. int64_t);
  315. void
  316. implicit_variable_set_init(Bvariable*, const std::string&, Btype*,
  317. bool, bool, bool, Bexpression*);
  318. Bvariable*
  319. implicit_variable_reference(const std::string&, Btype*);
  320. Bvariable*
  321. immutable_struct(const std::string&, bool, bool, Btype*, Location);
  322. void
  323. immutable_struct_set_init(Bvariable*, const std::string&, bool, bool, Btype*,
  324. Location, Bexpression*);
  325. Bvariable*
  326. immutable_struct_reference(const std::string&, Btype*, Location);
  327. // Labels.
  328. Blabel*
  329. label(Bfunction*, const std::string& name, Location);
  330. Bstatement*
  331. label_definition_statement(Blabel*);
  332. Bstatement*
  333. goto_statement(Blabel*, Location);
  334. Bexpression*
  335. label_address(Blabel*, Location);
  336. // Functions.
  337. Bfunction*
  338. error_function()
  339. { return this->make_function(error_mark_node); }
  340. Bfunction*
  341. function(Btype* fntype, const std::string& name, const std::string& asm_name,
  342. bool is_visible, bool is_declaration, bool is_inlinable,
  343. bool disable_split_stack, bool in_unique_section, Location);
  344. Bstatement*
  345. function_defer_statement(Bfunction* function, Bexpression* undefer,
  346. Bexpression* defer, Location);
  347. bool
  348. function_set_parameters(Bfunction* function, const std::vector<Bvariable*>&);
  349. bool
  350. function_set_body(Bfunction* function, Bstatement* code_stmt);
  351. Bfunction*
  352. lookup_builtin(const std::string&);
  353. void
  354. write_global_definitions(const std::vector<Btype*>&,
  355. const std::vector<Bexpression*>&,
  356. const std::vector<Bfunction*>&,
  357. const std::vector<Bvariable*>&);
  358. private:
  359. // Make a Bexpression from a tree.
  360. Bexpression*
  361. make_expression(tree t)
  362. { return new Bexpression(t); }
  363. // Make a Bstatement from a tree.
  364. Bstatement*
  365. make_statement(tree t)
  366. { return new Bstatement(t); }
  367. // Make a Btype from a tree.
  368. Btype*
  369. make_type(tree t)
  370. { return new Btype(t); }
  371. Bfunction*
  372. make_function(tree t)
  373. { return new Bfunction(t); }
  374. Btype*
  375. fill_in_struct(Btype*, const std::vector<Btyped_identifier>&);
  376. Btype*
  377. fill_in_array(Btype*, Btype*, Bexpression*);
  378. tree
  379. non_zero_size_type(tree);
  380. private:
  381. void
  382. define_builtin(built_in_function bcode, const char* name, const char* libname,
  383. tree fntype, bool const_p);
  384. // A mapping of the GCC built-ins exposed to GCCGo.
  385. std::map<std::string, Bfunction*> builtin_functions_;
  386. };
  387. // A helper function.
  388. static inline tree
  389. get_identifier_from_string(const std::string& str)
  390. {
  391. return get_identifier_with_length(str.data(), str.length());
  392. }
  393. // Define the built-in functions that are exposed to GCCGo.
  394. Gcc_backend::Gcc_backend()
  395. {
  396. /* We need to define the fetch_and_add functions, since we use them
  397. for ++ and --. */
  398. tree t = this->integer_type(BITS_PER_UNIT, 1)->get_tree();
  399. tree p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
  400. this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_1, "__sync_fetch_and_add_1",
  401. NULL, build_function_type_list(t, p, t, NULL_TREE),
  402. false);
  403. t = this->integer_type(BITS_PER_UNIT * 2, 1)->get_tree();
  404. p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
  405. this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_2, "__sync_fetch_and_add_2",
  406. NULL, build_function_type_list(t, p, t, NULL_TREE),
  407. false);
  408. t = this->integer_type(BITS_PER_UNIT * 4, 1)->get_tree();
  409. p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
  410. this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_4, "__sync_fetch_and_add_4",
  411. NULL, build_function_type_list(t, p, t, NULL_TREE),
  412. false);
  413. t = this->integer_type(BITS_PER_UNIT * 8, 1)->get_tree();
  414. p = build_pointer_type(build_qualified_type(t, TYPE_QUAL_VOLATILE));
  415. this->define_builtin(BUILT_IN_SYNC_ADD_AND_FETCH_8, "__sync_fetch_and_add_8",
  416. NULL, build_function_type_list(t, p, t, NULL_TREE),
  417. false);
  418. // We use __builtin_expect for magic import functions.
  419. this->define_builtin(BUILT_IN_EXPECT, "__builtin_expect", NULL,
  420. build_function_type_list(long_integer_type_node,
  421. long_integer_type_node,
  422. long_integer_type_node,
  423. NULL_TREE),
  424. true);
  425. // We use __builtin_memcmp for struct comparisons.
  426. this->define_builtin(BUILT_IN_MEMCMP, "__builtin_memcmp", "memcmp",
  427. build_function_type_list(integer_type_node,
  428. const_ptr_type_node,
  429. const_ptr_type_node,
  430. size_type_node,
  431. NULL_TREE),
  432. false);
  433. // We provide some functions for the math library.
  434. tree math_function_type = build_function_type_list(double_type_node,
  435. double_type_node,
  436. NULL_TREE);
  437. tree math_function_type_long =
  438. build_function_type_list(long_double_type_node, long_double_type_node,
  439. long_double_type_node, NULL_TREE);
  440. tree math_function_type_two = build_function_type_list(double_type_node,
  441. double_type_node,
  442. double_type_node,
  443. NULL_TREE);
  444. tree math_function_type_long_two =
  445. build_function_type_list(long_double_type_node, long_double_type_node,
  446. long_double_type_node, NULL_TREE);
  447. this->define_builtin(BUILT_IN_ACOS, "__builtin_acos", "acos",
  448. math_function_type, true);
  449. this->define_builtin(BUILT_IN_ACOSL, "__builtin_acosl", "acosl",
  450. math_function_type_long, true);
  451. this->define_builtin(BUILT_IN_ASIN, "__builtin_asin", "asin",
  452. math_function_type, true);
  453. this->define_builtin(BUILT_IN_ASINL, "__builtin_asinl", "asinl",
  454. math_function_type_long, true);
  455. this->define_builtin(BUILT_IN_ATAN, "__builtin_atan", "atan",
  456. math_function_type, true);
  457. this->define_builtin(BUILT_IN_ATANL, "__builtin_atanl", "atanl",
  458. math_function_type_long, true);
  459. this->define_builtin(BUILT_IN_ATAN2, "__builtin_atan2", "atan2",
  460. math_function_type_two, true);
  461. this->define_builtin(BUILT_IN_ATAN2L, "__builtin_atan2l", "atan2l",
  462. math_function_type_long_two, true);
  463. this->define_builtin(BUILT_IN_CEIL, "__builtin_ceil", "ceil",
  464. math_function_type, true);
  465. this->define_builtin(BUILT_IN_CEILL, "__builtin_ceill", "ceill",
  466. math_function_type_long, true);
  467. this->define_builtin(BUILT_IN_COS, "__builtin_cos", "cos",
  468. math_function_type, true);
  469. this->define_builtin(BUILT_IN_COSL, "__builtin_cosl", "cosl",
  470. math_function_type_long, true);
  471. this->define_builtin(BUILT_IN_EXP, "__builtin_exp", "exp",
  472. math_function_type, true);
  473. this->define_builtin(BUILT_IN_EXPL, "__builtin_expl", "expl",
  474. math_function_type_long, true);
  475. this->define_builtin(BUILT_IN_EXPM1, "__builtin_expm1", "expm1",
  476. math_function_type, true);
  477. this->define_builtin(BUILT_IN_EXPM1L, "__builtin_expm1l", "expm1l",
  478. math_function_type_long, true);
  479. this->define_builtin(BUILT_IN_FABS, "__builtin_fabs", "fabs",
  480. math_function_type, true);
  481. this->define_builtin(BUILT_IN_FABSL, "__builtin_fabsl", "fabsl",
  482. math_function_type_long, true);
  483. this->define_builtin(BUILT_IN_FLOOR, "__builtin_floor", "floor",
  484. math_function_type, true);
  485. this->define_builtin(BUILT_IN_FLOORL, "__builtin_floorl", "floorl",
  486. math_function_type_long, true);
  487. this->define_builtin(BUILT_IN_FMOD, "__builtin_fmod", "fmod",
  488. math_function_type_two, true);
  489. this->define_builtin(BUILT_IN_FMODL, "__builtin_fmodl", "fmodl",
  490. math_function_type_long_two, true);
  491. this->define_builtin(BUILT_IN_LDEXP, "__builtin_ldexp", "ldexp",
  492. build_function_type_list(double_type_node,
  493. double_type_node,
  494. integer_type_node,
  495. NULL_TREE),
  496. true);
  497. this->define_builtin(BUILT_IN_LDEXPL, "__builtin_ldexpl", "ldexpl",
  498. build_function_type_list(long_double_type_node,
  499. long_double_type_node,
  500. integer_type_node,
  501. NULL_TREE),
  502. true);
  503. this->define_builtin(BUILT_IN_LOG, "__builtin_log", "log",
  504. math_function_type, true);
  505. this->define_builtin(BUILT_IN_LOGL, "__builtin_logl", "logl",
  506. math_function_type_long, true);
  507. this->define_builtin(BUILT_IN_LOG1P, "__builtin_log1p", "log1p",
  508. math_function_type, true);
  509. this->define_builtin(BUILT_IN_LOG1PL, "__builtin_log1pl", "log1pl",
  510. math_function_type_long, true);
  511. this->define_builtin(BUILT_IN_LOG10, "__builtin_log10", "log10",
  512. math_function_type, true);
  513. this->define_builtin(BUILT_IN_LOG10L, "__builtin_log10l", "log10l",
  514. math_function_type_long, true);
  515. this->define_builtin(BUILT_IN_LOG2, "__builtin_log2", "log2",
  516. math_function_type, true);
  517. this->define_builtin(BUILT_IN_LOG2L, "__builtin_log2l", "log2l",
  518. math_function_type_long, true);
  519. this->define_builtin(BUILT_IN_SIN, "__builtin_sin", "sin",
  520. math_function_type, true);
  521. this->define_builtin(BUILT_IN_SINL, "__builtin_sinl", "sinl",
  522. math_function_type_long, true);
  523. this->define_builtin(BUILT_IN_SQRT, "__builtin_sqrt", "sqrt",
  524. math_function_type, true);
  525. this->define_builtin(BUILT_IN_SQRTL, "__builtin_sqrtl", "sqrtl",
  526. math_function_type_long, true);
  527. this->define_builtin(BUILT_IN_TAN, "__builtin_tan", "tan",
  528. math_function_type, true);
  529. this->define_builtin(BUILT_IN_TANL, "__builtin_tanl", "tanl",
  530. math_function_type_long, true);
  531. this->define_builtin(BUILT_IN_TRUNC, "__builtin_trunc", "trunc",
  532. math_function_type, true);
  533. this->define_builtin(BUILT_IN_TRUNCL, "__builtin_truncl", "truncl",
  534. math_function_type_long, true);
  535. // We use __builtin_return_address in the thunk we build for
  536. // functions which call recover.
  537. this->define_builtin(BUILT_IN_RETURN_ADDRESS, "__builtin_return_address",
  538. NULL,
  539. build_function_type_list(ptr_type_node,
  540. unsigned_type_node,
  541. NULL_TREE),
  542. false);
  543. // The compiler uses __builtin_trap for some exception handling
  544. // cases.
  545. this->define_builtin(BUILT_IN_TRAP, "__builtin_trap", NULL,
  546. build_function_type(void_type_node, void_list_node),
  547. false);
  548. }
  549. // Get an unnamed integer type.
  550. Btype*
  551. Gcc_backend::integer_type(bool is_unsigned, int bits)
  552. {
  553. tree type;
  554. if (is_unsigned)
  555. {
  556. if (bits == INT_TYPE_SIZE)
  557. type = unsigned_type_node;
  558. else if (bits == CHAR_TYPE_SIZE)
  559. type = unsigned_char_type_node;
  560. else if (bits == SHORT_TYPE_SIZE)
  561. type = short_unsigned_type_node;
  562. else if (bits == LONG_TYPE_SIZE)
  563. type = long_unsigned_type_node;
  564. else if (bits == LONG_LONG_TYPE_SIZE)
  565. type = long_long_unsigned_type_node;
  566. else
  567. type = make_unsigned_type(bits);
  568. }
  569. else
  570. {
  571. if (bits == INT_TYPE_SIZE)
  572. type = integer_type_node;
  573. else if (bits == CHAR_TYPE_SIZE)
  574. type = signed_char_type_node;
  575. else if (bits == SHORT_TYPE_SIZE)
  576. type = short_integer_type_node;
  577. else if (bits == LONG_TYPE_SIZE)
  578. type = long_integer_type_node;
  579. else if (bits == LONG_LONG_TYPE_SIZE)
  580. type = long_long_integer_type_node;
  581. else
  582. type = make_signed_type(bits);
  583. }
  584. return this->make_type(type);
  585. }
  586. // Get an unnamed float type.
  587. Btype*
  588. Gcc_backend::float_type(int bits)
  589. {
  590. tree type;
  591. if (bits == FLOAT_TYPE_SIZE)
  592. type = float_type_node;
  593. else if (bits == DOUBLE_TYPE_SIZE)
  594. type = double_type_node;
  595. else if (bits == LONG_DOUBLE_TYPE_SIZE)
  596. type = long_double_type_node;
  597. else
  598. {
  599. type = make_node(REAL_TYPE);
  600. TYPE_PRECISION(type) = bits;
  601. layout_type(type);
  602. }
  603. return this->make_type(type);
  604. }
  605. // Get an unnamed complex type.
  606. Btype*
  607. Gcc_backend::complex_type(int bits)
  608. {
  609. tree type;
  610. if (bits == FLOAT_TYPE_SIZE * 2)
  611. type = complex_float_type_node;
  612. else if (bits == DOUBLE_TYPE_SIZE * 2)
  613. type = complex_double_type_node;
  614. else if (bits == LONG_DOUBLE_TYPE_SIZE * 2)
  615. type = complex_long_double_type_node;
  616. else
  617. {
  618. type = make_node(REAL_TYPE);
  619. TYPE_PRECISION(type) = bits / 2;
  620. layout_type(type);
  621. type = build_complex_type(type);
  622. }
  623. return this->make_type(type);
  624. }
  625. // Get a pointer type.
  626. Btype*
  627. Gcc_backend::pointer_type(Btype* to_type)
  628. {
  629. tree to_type_tree = to_type->get_tree();
  630. if (to_type_tree == error_mark_node)
  631. return this->error_type();
  632. tree type = build_pointer_type(to_type_tree);
  633. return this->make_type(type);
  634. }
  635. // Make a function type.
  636. Btype*
  637. Gcc_backend::function_type(const Btyped_identifier& receiver,
  638. const std::vector<Btyped_identifier>& parameters,
  639. const std::vector<Btyped_identifier>& results,
  640. Btype* result_struct,
  641. Location)
  642. {
  643. tree args = NULL_TREE;
  644. tree* pp = &args;
  645. if (receiver.btype != NULL)
  646. {
  647. tree t = receiver.btype->get_tree();
  648. if (t == error_mark_node)
  649. return this->error_type();
  650. *pp = tree_cons(NULL_TREE, t, NULL_TREE);
  651. pp = &TREE_CHAIN(*pp);
  652. }
  653. for (std::vector<Btyped_identifier>::const_iterator p = parameters.begin();
  654. p != parameters.end();
  655. ++p)
  656. {
  657. tree t = p->btype->get_tree();
  658. if (t == error_mark_node)
  659. return this->error_type();
  660. *pp = tree_cons(NULL_TREE, t, NULL_TREE);
  661. pp = &TREE_CHAIN(*pp);
  662. }
  663. // Varargs is handled entirely at the Go level. When converted to
  664. // GENERIC functions are not varargs.
  665. *pp = void_list_node;
  666. tree result;
  667. if (results.empty())
  668. result = void_type_node;
  669. else if (results.size() == 1)
  670. result = results.front().btype->get_tree();
  671. else
  672. {
  673. gcc_assert(result_struct != NULL);
  674. result = result_struct->get_tree();
  675. }
  676. if (result == error_mark_node)
  677. return this->error_type();
  678. tree fntype = build_function_type(result, args);
  679. if (fntype == error_mark_node)
  680. return this->error_type();
  681. return this->make_type(build_pointer_type(fntype));
  682. }
  683. // Make a struct type.
  684. Btype*
  685. Gcc_backend::struct_type(const std::vector<Btyped_identifier>& fields)
  686. {
  687. return this->fill_in_struct(this->make_type(make_node(RECORD_TYPE)), fields);
  688. }
  689. // Fill in the fields of a struct type.
  690. Btype*
  691. Gcc_backend::fill_in_struct(Btype* fill,
  692. const std::vector<Btyped_identifier>& fields)
  693. {
  694. tree fill_tree = fill->get_tree();
  695. tree field_trees = NULL_TREE;
  696. tree* pp = &field_trees;
  697. for (std::vector<Btyped_identifier>::const_iterator p = fields.begin();
  698. p != fields.end();
  699. ++p)
  700. {
  701. tree name_tree = get_identifier_from_string(p->name);
  702. tree type_tree = p->btype->get_tree();
  703. if (type_tree == error_mark_node)
  704. return this->error_type();
  705. tree field = build_decl(p->location.gcc_location(), FIELD_DECL, name_tree,
  706. type_tree);
  707. DECL_CONTEXT(field) = fill_tree;
  708. *pp = field;
  709. pp = &DECL_CHAIN(field);
  710. }
  711. TYPE_FIELDS(fill_tree) = field_trees;
  712. layout_type(fill_tree);
  713. return fill;
  714. }
  715. // Make an array type.
  716. Btype*
  717. Gcc_backend::array_type(Btype* element_btype, Bexpression* length)
  718. {
  719. return this->fill_in_array(this->make_type(make_node(ARRAY_TYPE)),
  720. element_btype, length);
  721. }
  722. // Fill in an array type.
  723. Btype*
  724. Gcc_backend::fill_in_array(Btype* fill, Btype* element_type,
  725. Bexpression* length)
  726. {
  727. tree element_type_tree = element_type->get_tree();
  728. tree length_tree = length->get_tree();
  729. if (element_type_tree == error_mark_node || length_tree == error_mark_node)
  730. return this->error_type();
  731. gcc_assert(TYPE_SIZE(element_type_tree) != NULL_TREE);
  732. length_tree = fold_convert(sizetype, length_tree);
  733. // build_index_type takes the maximum index, which is one less than
  734. // the length.
  735. tree index_type_tree = build_index_type(fold_build2(MINUS_EXPR, sizetype,
  736. length_tree,
  737. size_one_node));
  738. tree fill_tree = fill->get_tree();
  739. TREE_TYPE(fill_tree) = element_type_tree;
  740. TYPE_DOMAIN(fill_tree) = index_type_tree;
  741. TYPE_ADDR_SPACE(fill_tree) = TYPE_ADDR_SPACE(element_type_tree);
  742. layout_type(fill_tree);
  743. if (TYPE_STRUCTURAL_EQUALITY_P(element_type_tree))
  744. SET_TYPE_STRUCTURAL_EQUALITY(fill_tree);
  745. else if (TYPE_CANONICAL(element_type_tree) != element_type_tree
  746. || TYPE_CANONICAL(index_type_tree) != index_type_tree)
  747. TYPE_CANONICAL(fill_tree) =
  748. build_array_type(TYPE_CANONICAL(element_type_tree),
  749. TYPE_CANONICAL(index_type_tree));
  750. return fill;
  751. }
  752. // Create a placeholder for a pointer type.
  753. Btype*
  754. Gcc_backend::placeholder_pointer_type(const std::string& name,
  755. Location location, bool)
  756. {
  757. tree ret = build_distinct_type_copy(ptr_type_node);
  758. if (!name.empty())
  759. {
  760. tree decl = build_decl(location.gcc_location(), TYPE_DECL,
  761. get_identifier_from_string(name),
  762. ret);
  763. TYPE_NAME(ret) = decl;
  764. }
  765. return this->make_type(ret);
  766. }
  767. // Set the real target type for a placeholder pointer type.
  768. bool
  769. Gcc_backend::set_placeholder_pointer_type(Btype* placeholder,
  770. Btype* to_type)
  771. {
  772. tree pt = placeholder->get_tree();
  773. if (pt == error_mark_node)
  774. return false;
  775. gcc_assert(TREE_CODE(pt) == POINTER_TYPE);
  776. tree tt = to_type->get_tree();
  777. if (tt == error_mark_node)
  778. {
  779. placeholder->set_tree(error_mark_node);
  780. return false;
  781. }
  782. gcc_assert(TREE_CODE(tt) == POINTER_TYPE);
  783. TREE_TYPE(pt) = TREE_TYPE(tt);
  784. if (TYPE_NAME(pt) != NULL_TREE)
  785. {
  786. // Build the data structure gcc wants to see for a typedef.
  787. tree copy = build_variant_type_copy(pt);
  788. TYPE_NAME(copy) = NULL_TREE;
  789. DECL_ORIGINAL_TYPE(TYPE_NAME(pt)) = copy;
  790. }
  791. return true;
  792. }
  793. // Set the real values for a placeholder function type.
  794. bool
  795. Gcc_backend::set_placeholder_function_type(Btype* placeholder, Btype* ft)
  796. {
  797. return this->set_placeholder_pointer_type(placeholder, ft);
  798. }
  799. // Create a placeholder for a struct type.
  800. Btype*
  801. Gcc_backend::placeholder_struct_type(const std::string& name,
  802. Location location)
  803. {
  804. tree ret = make_node(RECORD_TYPE);
  805. if (!name.empty())
  806. {
  807. tree decl = build_decl(location.gcc_location(), TYPE_DECL,
  808. get_identifier_from_string(name),
  809. ret);
  810. TYPE_NAME(ret) = decl;
  811. }
  812. return this->make_type(ret);
  813. }
  814. // Fill in the fields of a placeholder struct type.
  815. bool
  816. Gcc_backend::set_placeholder_struct_type(
  817. Btype* placeholder,
  818. const std::vector<Btyped_identifier>& fields)
  819. {
  820. tree t = placeholder->get_tree();
  821. gcc_assert(TREE_CODE(t) == RECORD_TYPE && TYPE_FIELDS(t) == NULL_TREE);
  822. Btype* r = this->fill_in_struct(placeholder, fields);
  823. if (TYPE_NAME(t) != NULL_TREE)
  824. {
  825. // Build the data structure gcc wants to see for a typedef.
  826. tree copy = build_distinct_type_copy(t);
  827. TYPE_NAME(copy) = NULL_TREE;
  828. DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
  829. }
  830. return r->get_tree() != error_mark_node;
  831. }
  832. // Create a placeholder for an array type.
  833. Btype*
  834. Gcc_backend::placeholder_array_type(const std::string& name,
  835. Location location)
  836. {
  837. tree ret = make_node(ARRAY_TYPE);
  838. tree decl = build_decl(location.gcc_location(), TYPE_DECL,
  839. get_identifier_from_string(name),
  840. ret);
  841. TYPE_NAME(ret) = decl;
  842. return this->make_type(ret);
  843. }
  844. // Fill in the fields of a placeholder array type.
  845. bool
  846. Gcc_backend::set_placeholder_array_type(Btype* placeholder,
  847. Btype* element_btype,
  848. Bexpression* length)
  849. {
  850. tree t = placeholder->get_tree();
  851. gcc_assert(TREE_CODE(t) == ARRAY_TYPE && TREE_TYPE(t) == NULL_TREE);
  852. Btype* r = this->fill_in_array(placeholder, element_btype, length);
  853. // Build the data structure gcc wants to see for a typedef.
  854. tree copy = build_distinct_type_copy(t);
  855. TYPE_NAME(copy) = NULL_TREE;
  856. DECL_ORIGINAL_TYPE(TYPE_NAME(t)) = copy;
  857. return r->get_tree() != error_mark_node;
  858. }
  859. // Return a named version of a type.
  860. Btype*
  861. Gcc_backend::named_type(const std::string& name, Btype* btype,
  862. Location location)
  863. {
  864. tree type = btype->get_tree();
  865. if (type == error_mark_node)
  866. return this->error_type();
  867. // The middle-end expects a basic type to have a name. In Go every
  868. // basic type will have a name. The first time we see a basic type,
  869. // give it whatever Go name we have at this point.
  870. if (TYPE_NAME(type) == NULL_TREE
  871. && location.gcc_location() == BUILTINS_LOCATION
  872. && (TREE_CODE(type) == INTEGER_TYPE
  873. || TREE_CODE(type) == REAL_TYPE
  874. || TREE_CODE(type) == COMPLEX_TYPE
  875. || TREE_CODE(type) == BOOLEAN_TYPE))
  876. {
  877. tree decl = build_decl(BUILTINS_LOCATION, TYPE_DECL,
  878. get_identifier_from_string(name),
  879. type);
  880. TYPE_NAME(type) = decl;
  881. return this->make_type(type);
  882. }
  883. tree copy = build_variant_type_copy(type);
  884. tree decl = build_decl(location.gcc_location(), TYPE_DECL,
  885. get_identifier_from_string(name),
  886. copy);
  887. DECL_ORIGINAL_TYPE(decl) = type;
  888. TYPE_NAME(copy) = decl;
  889. return this->make_type(copy);
  890. }
  891. // Return a pointer type used as a marker for a circular type.
  892. Btype*
  893. Gcc_backend::circular_pointer_type(Btype*, bool)
  894. {
  895. return this->make_type(ptr_type_node);
  896. }
  897. // Return whether we might be looking at a circular type.
  898. bool
  899. Gcc_backend::is_circular_pointer_type(Btype* btype)
  900. {
  901. return btype->get_tree() == ptr_type_node;
  902. }
  903. // Return the size of a type.
  904. int64_t
  905. Gcc_backend::type_size(Btype* btype)
  906. {
  907. tree t = btype->get_tree();
  908. if (t == error_mark_node)
  909. return 1;
  910. t = TYPE_SIZE_UNIT(t);
  911. gcc_assert(tree_fits_uhwi_p (t));
  912. unsigned HOST_WIDE_INT val_wide = TREE_INT_CST_LOW(t);
  913. int64_t ret = static_cast<int64_t>(val_wide);
  914. gcc_assert(ret >= 0 && static_cast<unsigned HOST_WIDE_INT>(ret) == val_wide);
  915. return ret;
  916. }
  917. // Return the alignment of a type.
  918. int64_t
  919. Gcc_backend::type_alignment(Btype* btype)
  920. {
  921. tree t = btype->get_tree();
  922. if (t == error_mark_node)
  923. return 1;
  924. return TYPE_ALIGN_UNIT(t);
  925. }
  926. // Return the alignment of a struct field of type BTYPE.
  927. int64_t
  928. Gcc_backend::type_field_alignment(Btype* btype)
  929. {
  930. tree t = btype->get_tree();
  931. if (t == error_mark_node)
  932. return 1;
  933. return go_field_alignment(t);
  934. }
  935. // Return the offset of a field in a struct.
  936. int64_t
  937. Gcc_backend::type_field_offset(Btype* btype, size_t index)
  938. {
  939. tree struct_tree = btype->get_tree();
  940. if (struct_tree == error_mark_node)
  941. return 0;
  942. gcc_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
  943. tree field = TYPE_FIELDS(struct_tree);
  944. for (; index > 0; --index)
  945. {
  946. field = DECL_CHAIN(field);
  947. gcc_assert(field != NULL_TREE);
  948. }
  949. HOST_WIDE_INT offset_wide = int_byte_position(field);
  950. int64_t ret = static_cast<int64_t>(offset_wide);
  951. gcc_assert(ret == offset_wide);
  952. return ret;
  953. }
  954. // Return the zero value for a type.
  955. Bexpression*
  956. Gcc_backend::zero_expression(Btype* btype)
  957. {
  958. tree t = btype->get_tree();
  959. tree ret;
  960. if (t == error_mark_node)
  961. ret = error_mark_node;
  962. else
  963. ret = build_zero_cst(t);
  964. return this->make_expression(ret);
  965. }
  966. // An expression that references a variable.
  967. Bexpression*
  968. Gcc_backend::var_expression(Bvariable* var, Location)
  969. {
  970. tree ret = var->get_tree();
  971. if (ret == error_mark_node)
  972. return this->error_expression();
  973. return this->make_expression(ret);
  974. }
  975. // An expression that indirectly references an expression.
  976. Bexpression*
  977. Gcc_backend::indirect_expression(Btype* btype, Bexpression* expr,
  978. bool known_valid, Location location)
  979. {
  980. tree expr_tree = expr->get_tree();
  981. tree type_tree = btype->get_tree();
  982. if (expr_tree == error_mark_node || type_tree == error_mark_node)
  983. return this->error_expression();
  984. // If the type of EXPR is a recursive pointer type, then we
  985. // need to insert a cast before indirecting.
  986. tree target_type_tree = TREE_TYPE(TREE_TYPE(expr_tree));
  987. if (VOID_TYPE_P(target_type_tree))
  988. expr_tree = fold_convert_loc(location.gcc_location(),
  989. build_pointer_type(type_tree), expr_tree);
  990. tree ret = build_fold_indirect_ref_loc(location.gcc_location(),
  991. expr_tree);
  992. if (known_valid)
  993. TREE_THIS_NOTRAP(ret) = 1;
  994. return this->make_expression(ret);
  995. }
  996. // Return an expression that declares a constant named NAME with the
  997. // constant value VAL in BTYPE.
  998. Bexpression*
  999. Gcc_backend::named_constant_expression(Btype* btype, const std::string& name,
  1000. Bexpression* val, Location location)
  1001. {
  1002. tree type_tree = btype->get_tree();
  1003. tree const_val = val->get_tree();
  1004. if (type_tree == error_mark_node || const_val == error_mark_node)
  1005. return this->error_expression();
  1006. tree name_tree = get_identifier_from_string(name);
  1007. tree decl = build_decl(location.gcc_location(), CONST_DECL, name_tree,
  1008. type_tree);
  1009. DECL_INITIAL(decl) = const_val;
  1010. TREE_CONSTANT(decl) = 1;
  1011. TREE_READONLY(decl) = 1;
  1012. go_preserve_from_gc(decl);
  1013. return this->make_expression(decl);
  1014. }
  1015. // Return a typed value as a constant integer.
  1016. Bexpression*
  1017. Gcc_backend::integer_constant_expression(Btype* btype, mpz_t val)
  1018. {
  1019. tree t = btype->get_tree();
  1020. if (t == error_mark_node)
  1021. return this->error_expression();
  1022. tree ret = double_int_to_tree(t, mpz_get_double_int(t, val, true));
  1023. return this->make_expression(ret);
  1024. }
  1025. // Return a typed value as a constant floating-point number.
  1026. Bexpression*
  1027. Gcc_backend::float_constant_expression(Btype* btype, mpfr_t val)
  1028. {
  1029. tree t = btype->get_tree();
  1030. tree ret;
  1031. if (t == error_mark_node)
  1032. return this->error_expression();
  1033. REAL_VALUE_TYPE r1;
  1034. real_from_mpfr(&r1, val, t, GMP_RNDN);
  1035. REAL_VALUE_TYPE r2;
  1036. real_convert(&r2, TYPE_MODE(t), &r1);
  1037. ret = build_real(t, r2);
  1038. return this->make_expression(ret);
  1039. }
  1040. // Return a typed real and imaginary value as a constant complex number.
  1041. Bexpression*
  1042. Gcc_backend::complex_constant_expression(Btype* btype, mpc_t val)
  1043. {
  1044. tree t = btype->get_tree();
  1045. tree ret;
  1046. if (t == error_mark_node)
  1047. return this->error_expression();
  1048. REAL_VALUE_TYPE r1;
  1049. real_from_mpfr(&r1, mpc_realref(val), TREE_TYPE(t), GMP_RNDN);
  1050. REAL_VALUE_TYPE r2;
  1051. real_convert(&r2, TYPE_MODE(TREE_TYPE(t)), &r1);
  1052. REAL_VALUE_TYPE r3;
  1053. real_from_mpfr(&r3, mpc_imagref(val), TREE_TYPE(t), GMP_RNDN);
  1054. REAL_VALUE_TYPE r4;
  1055. real_convert(&r4, TYPE_MODE(TREE_TYPE(t)), &r3);
  1056. ret = build_complex(t, build_real(TREE_TYPE(t), r2),
  1057. build_real(TREE_TYPE(t), r4));
  1058. return this->make_expression(ret);
  1059. }
  1060. // Make a constant string expression.
  1061. Bexpression*
  1062. Gcc_backend::string_constant_expression(const std::string& val)
  1063. {
  1064. tree index_type = build_index_type(size_int(val.length()));
  1065. tree const_char_type = build_qualified_type(unsigned_char_type_node,
  1066. TYPE_QUAL_CONST);
  1067. tree string_type = build_array_type(const_char_type, index_type);
  1068. string_type = build_variant_type_copy(string_type);
  1069. TYPE_STRING_FLAG(string_type) = 1;
  1070. tree string_val = build_string(val.length(), val.data());
  1071. TREE_TYPE(string_val) = string_type;
  1072. return this->make_expression(string_val);
  1073. }
  1074. // Make a constant boolean expression.
  1075. Bexpression*
  1076. Gcc_backend::boolean_constant_expression(bool val)
  1077. {
  1078. tree bool_cst = val ? boolean_true_node : boolean_false_node;
  1079. return this->make_expression(bool_cst);
  1080. }
  1081. // Return the real part of a complex expression.
  1082. Bexpression*
  1083. Gcc_backend::real_part_expression(Bexpression* bcomplex, Location location)
  1084. {
  1085. tree complex_tree = bcomplex->get_tree();
  1086. if (complex_tree == error_mark_node)
  1087. return this->error_expression();
  1088. gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
  1089. tree ret = fold_build1_loc(location.gcc_location(), REALPART_EXPR,
  1090. TREE_TYPE(TREE_TYPE(complex_tree)),
  1091. complex_tree);
  1092. return this->make_expression(ret);
  1093. }
  1094. // Return the imaginary part of a complex expression.
  1095. Bexpression*
  1096. Gcc_backend::imag_part_expression(Bexpression* bcomplex, Location location)
  1097. {
  1098. tree complex_tree = bcomplex->get_tree();
  1099. if (complex_tree == error_mark_node)
  1100. return this->error_expression();
  1101. gcc_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(complex_tree)));
  1102. tree ret = fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
  1103. TREE_TYPE(TREE_TYPE(complex_tree)),
  1104. complex_tree);
  1105. return this->make_expression(ret);
  1106. }
  1107. // Make a complex expression given its real and imaginary parts.
  1108. Bexpression*
  1109. Gcc_backend::complex_expression(Bexpression* breal, Bexpression* bimag,
  1110. Location location)
  1111. {
  1112. tree real_tree = breal->get_tree();
  1113. tree imag_tree = bimag->get_tree();
  1114. if (real_tree == error_mark_node || imag_tree == error_mark_node)
  1115. return this->error_expression();
  1116. gcc_assert(TYPE_MAIN_VARIANT(TREE_TYPE(real_tree))
  1117. == TYPE_MAIN_VARIANT(TREE_TYPE(imag_tree)));
  1118. gcc_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(real_tree)));
  1119. tree ret = fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
  1120. build_complex_type(TREE_TYPE(real_tree)),
  1121. real_tree, imag_tree);
  1122. return this->make_expression(ret);
  1123. }
  1124. // An expression that converts an expression to a different type.
  1125. Bexpression*
  1126. Gcc_backend::convert_expression(Btype* type, Bexpression* expr,
  1127. Location location)
  1128. {
  1129. tree type_tree = type->get_tree();
  1130. tree expr_tree = expr->get_tree();
  1131. if (type_tree == error_mark_node
  1132. || expr_tree == error_mark_node
  1133. || TREE_TYPE(expr_tree) == error_mark_node)
  1134. return this->error_expression();
  1135. tree ret;
  1136. if (this->type_size(type) == 0)
  1137. {
  1138. // Do not convert zero-sized types.
  1139. ret = expr_tree;
  1140. }
  1141. else if (TREE_CODE(type_tree) == INTEGER_TYPE)
  1142. ret = fold(convert_to_integer(type_tree, expr_tree));
  1143. else if (TREE_CODE(type_tree) == REAL_TYPE)
  1144. ret = fold(convert_to_real(type_tree, expr_tree));
  1145. else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
  1146. ret = fold(convert_to_complex(type_tree, expr_tree));
  1147. else if (TREE_CODE(type_tree) == POINTER_TYPE
  1148. && TREE_CODE(TREE_TYPE(expr_tree)) == INTEGER_TYPE)
  1149. ret = fold(convert_to_pointer(type_tree, expr_tree));
  1150. else if (TREE_CODE(type_tree) == RECORD_TYPE
  1151. || TREE_CODE(type_tree) == ARRAY_TYPE)
  1152. ret = fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
  1153. type_tree, expr_tree);
  1154. else
  1155. ret = fold_convert_loc(location.gcc_location(), type_tree, expr_tree);
  1156. return this->make_expression(ret);
  1157. }
  1158. // Get the address of a function.
  1159. Bexpression*
  1160. Gcc_backend::function_code_expression(Bfunction* bfunc, Location location)
  1161. {
  1162. tree func = bfunc->get_tree();
  1163. if (func == error_mark_node)
  1164. return this->error_expression();
  1165. tree ret = build_fold_addr_expr_loc(location.gcc_location(), func);
  1166. return this->make_expression(ret);
  1167. }
  1168. // Get the address of an expression.
  1169. Bexpression*
  1170. Gcc_backend::address_expression(Bexpression* bexpr, Location location)
  1171. {
  1172. tree expr = bexpr->get_tree();
  1173. if (expr == error_mark_node)
  1174. return this->error_expression();
  1175. tree ret = build_fold_addr_expr_loc(location.gcc_location(), expr);
  1176. return this->make_expression(ret);
  1177. }
  1178. // Return an expression for the field at INDEX in BSTRUCT.
  1179. Bexpression*
  1180. Gcc_backend::struct_field_expression(Bexpression* bstruct, size_t index,
  1181. Location location)
  1182. {
  1183. tree struct_tree = bstruct->get_tree();
  1184. if (struct_tree == error_mark_node
  1185. || TREE_TYPE(struct_tree) == error_mark_node)
  1186. return this->error_expression();
  1187. gcc_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
  1188. tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
  1189. if (field == NULL_TREE)
  1190. {
  1191. // This can happen for a type which refers to itself indirectly
  1192. // and then turns out to be erroneous.
  1193. return this->error_expression();
  1194. }
  1195. for (unsigned int i = index; i > 0; --i)
  1196. {
  1197. field = DECL_CHAIN(field);
  1198. gcc_assert(field != NULL_TREE);
  1199. }
  1200. if (TREE_TYPE(field) == error_mark_node)
  1201. return this->error_expression();
  1202. tree ret = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
  1203. TREE_TYPE(field), struct_tree, field,
  1204. NULL_TREE);
  1205. if (TREE_CONSTANT(struct_tree))
  1206. TREE_CONSTANT(ret) = 1;
  1207. return this->make_expression(ret);
  1208. }
  1209. // Return an expression that executes BSTAT before BEXPR.
  1210. Bexpression*
  1211. Gcc_backend::compound_expression(Bstatement* bstat, Bexpression* bexpr,
  1212. Location location)
  1213. {
  1214. tree stat = bstat->get_tree();
  1215. tree expr = bexpr->get_tree();
  1216. if (stat == error_mark_node || expr == error_mark_node)
  1217. return this->error_expression();
  1218. tree ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
  1219. TREE_TYPE(expr), stat, expr);
  1220. return this->make_expression(ret);
  1221. }
  1222. // Return an expression that executes THEN_EXPR if CONDITION is true, or
  1223. // ELSE_EXPR otherwise.
  1224. Bexpression*
  1225. Gcc_backend::conditional_expression(Btype* btype, Bexpression* condition,
  1226. Bexpression* then_expr,
  1227. Bexpression* else_expr, Location location)
  1228. {
  1229. tree type_tree = btype == NULL ? void_type_node : btype->get_tree();
  1230. tree cond_tree = condition->get_tree();
  1231. tree then_tree = then_expr->get_tree();
  1232. tree else_tree = else_expr == NULL ? NULL_TREE : else_expr->get_tree();
  1233. if (type_tree == error_mark_node
  1234. || cond_tree == error_mark_node
  1235. || then_tree == error_mark_node
  1236. || else_tree == error_mark_node)
  1237. return this->error_expression();
  1238. tree ret = build3_loc(location.gcc_location(), COND_EXPR, type_tree,
  1239. cond_tree, then_tree, else_tree);
  1240. return this->make_expression(ret);
  1241. }
  1242. // Return an expression for the unary operation OP EXPR.
  1243. Bexpression*
  1244. Gcc_backend::unary_expression(Operator op, Bexpression* expr, Location location)
  1245. {
  1246. tree expr_tree = expr->get_tree();
  1247. if (expr_tree == error_mark_node
  1248. || TREE_TYPE(expr_tree) == error_mark_node)
  1249. return this->error_expression();
  1250. tree type_tree = TREE_TYPE(expr_tree);
  1251. enum tree_code code;
  1252. switch (op)
  1253. {
  1254. case OPERATOR_MINUS:
  1255. {
  1256. tree computed_type = excess_precision_type(type_tree);
  1257. if (computed_type != NULL_TREE)
  1258. {
  1259. expr_tree = convert(computed_type, expr_tree);
  1260. type_tree = computed_type;
  1261. }
  1262. code = NEGATE_EXPR;
  1263. break;
  1264. }
  1265. case OPERATOR_NOT:
  1266. code = TRUTH_NOT_EXPR;
  1267. break;
  1268. case OPERATOR_XOR:
  1269. code = BIT_NOT_EXPR;
  1270. break;
  1271. default:
  1272. gcc_unreachable();
  1273. break;
  1274. }
  1275. tree ret = fold_build1_loc(location.gcc_location(), code, type_tree,
  1276. expr_tree);
  1277. return this->make_expression(ret);
  1278. }
  1279. // Convert a gofrontend operator to an equivalent tree_code.
  1280. static enum tree_code
  1281. operator_to_tree_code(Operator op, tree type)
  1282. {
  1283. enum tree_code code;
  1284. switch (op)
  1285. {
  1286. case OPERATOR_EQEQ:
  1287. code = EQ_EXPR;
  1288. break;
  1289. case OPERATOR_NOTEQ:
  1290. code = NE_EXPR;
  1291. break;
  1292. case OPERATOR_LT:
  1293. code = LT_EXPR;
  1294. break;
  1295. case OPERATOR_LE:
  1296. code = LE_EXPR;
  1297. break;
  1298. case OPERATOR_GT:
  1299. code = GT_EXPR;
  1300. break;
  1301. case OPERATOR_GE:
  1302. code = GE_EXPR;
  1303. break;
  1304. case OPERATOR_OROR:
  1305. code = TRUTH_ORIF_EXPR;
  1306. break;
  1307. case OPERATOR_ANDAND:
  1308. code = TRUTH_ANDIF_EXPR;
  1309. break;
  1310. case OPERATOR_PLUS:
  1311. code = PLUS_EXPR;
  1312. break;
  1313. case OPERATOR_MINUS:
  1314. code = MINUS_EXPR;
  1315. break;
  1316. case OPERATOR_OR:
  1317. code = BIT_IOR_EXPR;
  1318. break;
  1319. case OPERATOR_XOR:
  1320. code = BIT_XOR_EXPR;
  1321. break;
  1322. case OPERATOR_MULT:
  1323. code = MULT_EXPR;
  1324. break;
  1325. case OPERATOR_DIV:
  1326. if (TREE_CODE(type) == REAL_TYPE || TREE_CODE(type) == COMPLEX_TYPE)
  1327. code = RDIV_EXPR;
  1328. else
  1329. code = TRUNC_DIV_EXPR;
  1330. break;
  1331. case OPERATOR_MOD:
  1332. code = TRUNC_MOD_EXPR;
  1333. break;
  1334. case OPERATOR_LSHIFT:
  1335. code = LSHIFT_EXPR;
  1336. break;
  1337. case OPERATOR_RSHIFT:
  1338. code = RSHIFT_EXPR;
  1339. break;
  1340. case OPERATOR_AND:
  1341. code = BIT_AND_EXPR;
  1342. break;
  1343. case OPERATOR_BITCLEAR:
  1344. code = BIT_AND_EXPR;
  1345. break;
  1346. default:
  1347. gcc_unreachable();
  1348. }
  1349. return code;
  1350. }
  1351. // Return an expression for the binary operation LEFT OP RIGHT.
  1352. Bexpression*
  1353. Gcc_backend::binary_expression(Operator op, Bexpression* left,
  1354. Bexpression* right, Location location)
  1355. {
  1356. tree left_tree = left->get_tree();
  1357. tree right_tree = right->get_tree();
  1358. if (left_tree == error_mark_node
  1359. || right_tree == error_mark_node)
  1360. return this->error_expression();
  1361. enum tree_code code = operator_to_tree_code(op, TREE_TYPE(left_tree));
  1362. bool use_left_type = op != OPERATOR_OROR && op != OPERATOR_ANDAND;
  1363. tree type_tree = use_left_type ? TREE_TYPE(left_tree) : TREE_TYPE(right_tree);
  1364. tree computed_type = excess_precision_type(type_tree);
  1365. if (computed_type != NULL_TREE)
  1366. {
  1367. left_tree = convert(computed_type, left_tree);
  1368. right_tree = convert(computed_type, right_tree);
  1369. type_tree = computed_type;
  1370. }
  1371. // For comparison operators, the resulting type should be boolean.
  1372. switch (op)
  1373. {
  1374. case OPERATOR_EQEQ:
  1375. case OPERATOR_NOTEQ:
  1376. case OPERATOR_LT:
  1377. case OPERATOR_LE:
  1378. case OPERATOR_GT:
  1379. case OPERATOR_GE:
  1380. type_tree = boolean_type_node;
  1381. break;
  1382. default:
  1383. break;
  1384. }
  1385. tree ret = fold_build2_loc(location.gcc_location(), code, type_tree,
  1386. left_tree, right_tree);
  1387. return this->make_expression(ret);
  1388. }
  1389. // Return an expression that constructs BTYPE with VALS.
  1390. Bexpression*
  1391. Gcc_backend::constructor_expression(Btype* btype,
  1392. const std::vector<Bexpression*>& vals,
  1393. Location location)
  1394. {
  1395. tree type_tree = btype->get_tree();
  1396. if (type_tree == error_mark_node)
  1397. return this->error_expression();
  1398. vec<constructor_elt, va_gc> *init;
  1399. vec_alloc(init, vals.size());
  1400. tree sink = NULL_TREE;
  1401. bool is_constant = true;
  1402. tree field = TYPE_FIELDS(type_tree);
  1403. for (std::vector<Bexpression*>::const_iterator p = vals.begin();
  1404. p != vals.end();
  1405. ++p, field = DECL_CHAIN(field))
  1406. {
  1407. gcc_assert(field != NULL_TREE);
  1408. tree val = (*p)->get_tree();
  1409. if (TREE_TYPE(field) == error_mark_node
  1410. || val == error_mark_node
  1411. || TREE_TYPE(val) == error_mark_node)
  1412. return this->error_expression();
  1413. if (int_size_in_bytes(TREE_TYPE(field)) == 0)
  1414. {
  1415. // GIMPLE cannot represent indices of zero-sized types so
  1416. // trying to construct a map with zero-sized keys might lead
  1417. // to errors. Instead, we evaluate each expression that
  1418. // would have been added as a map element for its
  1419. // side-effects and construct an empty map.
  1420. append_to_statement_list(val, &sink);
  1421. continue;
  1422. }
  1423. constructor_elt empty = {NULL, NULL};
  1424. constructor_elt* elt = init->quick_push(empty);
  1425. elt->index = field;
  1426. elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
  1427. val);
  1428. if (!TREE_CONSTANT(elt->value))
  1429. is_constant = false;
  1430. }
  1431. gcc_assert(field == NULL_TREE);
  1432. tree ret = build_constructor(type_tree, init);
  1433. if (is_constant)
  1434. TREE_CONSTANT(ret) = 1;
  1435. if (sink != NULL_TREE)
  1436. ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
  1437. type_tree, sink, ret);
  1438. return this->make_expression(ret);
  1439. }
  1440. Bexpression*
  1441. Gcc_backend::array_constructor_expression(
  1442. Btype* array_btype, const std::vector<unsigned long>& indexes,
  1443. const std::vector<Bexpression*>& vals, Location location)
  1444. {
  1445. tree type_tree = array_btype->get_tree();
  1446. if (type_tree == error_mark_node)
  1447. return this->error_expression();
  1448. gcc_assert(indexes.size() == vals.size());
  1449. tree element_type = TREE_TYPE(type_tree);
  1450. HOST_WIDE_INT element_size = int_size_in_bytes(element_type);
  1451. vec<constructor_elt, va_gc> *init;
  1452. vec_alloc(init, element_size == 0 ? 0 : vals.size());
  1453. tree sink = NULL_TREE;
  1454. bool is_constant = true;
  1455. for (size_t i = 0; i < vals.size(); ++i)
  1456. {
  1457. tree index = size_int(indexes[i]);
  1458. tree val = (vals[i])->get_tree();
  1459. if (index == error_mark_node
  1460. || val == error_mark_node)
  1461. return this->error_expression();
  1462. if (element_size == 0)
  1463. {
  1464. // GIMPLE cannot represent arrays of zero-sized types so trying
  1465. // to construct an array of zero-sized values might lead to errors.
  1466. // Instead, we evaluate each expression that would have been added as
  1467. // an array value for its side-effects and construct an empty array.
  1468. append_to_statement_list(val, &sink);
  1469. continue;
  1470. }
  1471. if (!TREE_CONSTANT(val))
  1472. is_constant = false;
  1473. constructor_elt empty = {NULL, NULL};
  1474. constructor_elt* elt = init->quick_push(empty);
  1475. elt->index = index;
  1476. elt->value = val;
  1477. }
  1478. tree ret = build_constructor(type_tree, init);
  1479. if (is_constant)
  1480. TREE_CONSTANT(ret) = 1;
  1481. if (sink != NULL_TREE)
  1482. ret = fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
  1483. type_tree, sink, ret);
  1484. return this->make_expression(ret);
  1485. }
  1486. // Return an expression for the address of BASE[INDEX].
  1487. Bexpression*
  1488. Gcc_backend::pointer_offset_expression(Bexpression* base, Bexpression* index,
  1489. Location location)
  1490. {
  1491. tree base_tree = base->get_tree();
  1492. tree index_tree = index->get_tree();
  1493. tree element_type_tree = TREE_TYPE(TREE_TYPE(base_tree));
  1494. if (base_tree == error_mark_node
  1495. || TREE_TYPE(base_tree) == error_mark_node
  1496. || index_tree == error_mark_node
  1497. || element_type_tree == error_mark_node)
  1498. return this->error_expression();
  1499. tree element_size = TYPE_SIZE_UNIT(element_type_tree);
  1500. index_tree = fold_convert_loc(location.gcc_location(), sizetype, index_tree);
  1501. tree offset = fold_build2_loc(location.gcc_location(), MULT_EXPR, sizetype,
  1502. index_tree, element_size);
  1503. tree ptr = fold_build2_loc(location.gcc_location(), POINTER_PLUS_EXPR,
  1504. TREE_TYPE(base_tree), base_tree, offset);
  1505. return this->make_expression(ptr);
  1506. }
  1507. // Return an expression representing ARRAY[INDEX]
  1508. Bexpression*
  1509. Gcc_backend::array_index_expression(Bexpression* array, Bexpression* index,
  1510. Location location)
  1511. {
  1512. tree array_tree = array->get_tree();
  1513. tree index_tree = index->get_tree();
  1514. if (array_tree == error_mark_node
  1515. || TREE_TYPE(array_tree) == error_mark_node
  1516. || index_tree == error_mark_node)
  1517. return this->error_expression();
  1518. tree ret = build4_loc(location.gcc_location(), ARRAY_REF,
  1519. TREE_TYPE(TREE_TYPE(array_tree)), array_tree,
  1520. index_tree, NULL_TREE, NULL_TREE);
  1521. return this->make_expression(ret);
  1522. }
  1523. // Create an expression for a call to FN_EXPR with FN_ARGS.
  1524. Bexpression*
  1525. Gcc_backend::call_expression(Bexpression* fn_expr,
  1526. const std::vector<Bexpression*>& fn_args,
  1527. Bexpression* chain_expr, Location location)
  1528. {
  1529. tree fn = fn_expr->get_tree();
  1530. if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
  1531. return this->error_expression();
  1532. gcc_assert(FUNCTION_POINTER_TYPE_P(TREE_TYPE(fn)));
  1533. tree rettype = TREE_TYPE(TREE_TYPE(TREE_TYPE(fn)));
  1534. size_t nargs = fn_args.size();
  1535. tree* args = nargs == 0 ? NULL : new tree[nargs];
  1536. for (size_t i = 0; i < nargs; ++i)
  1537. {
  1538. args[i] = fn_args.at(i)->get_tree();
  1539. if (args[i] == error_mark_node)
  1540. return this->error_expression();
  1541. }
  1542. tree fndecl = fn;
  1543. if (TREE_CODE(fndecl) == ADDR_EXPR)
  1544. fndecl = TREE_OPERAND(fndecl, 0);
  1545. // This is to support builtin math functions when using 80387 math.
  1546. tree excess_type = NULL_TREE;
  1547. if (optimize
  1548. && TREE_CODE(fndecl) == FUNCTION_DECL
  1549. && DECL_IS_BUILTIN(fndecl)
  1550. && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
  1551. && nargs > 0
  1552. && ((SCALAR_FLOAT_TYPE_P(rettype)
  1553. && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
  1554. || (COMPLEX_FLOAT_TYPE_P(rettype)
  1555. && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
  1556. {
  1557. excess_type = excess_precision_type(TREE_TYPE(args[0]));
  1558. if (excess_type != NULL_TREE)
  1559. {
  1560. tree excess_fndecl = mathfn_built_in(excess_type,
  1561. DECL_FUNCTION_CODE(fndecl));
  1562. if (excess_fndecl == NULL_TREE)
  1563. excess_type = NULL_TREE;
  1564. else
  1565. {
  1566. fn = build_fold_addr_expr_loc(location.gcc_location(),
  1567. excess_fndecl);
  1568. for (size_t i = 0; i < nargs; ++i)
  1569. {
  1570. if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
  1571. || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
  1572. args[i] = ::convert(excess_type, args[i]);
  1573. }
  1574. }
  1575. }
  1576. }
  1577. tree ret =
  1578. build_call_array_loc(location.gcc_location(),
  1579. excess_type != NULL_TREE ? excess_type : rettype,
  1580. fn, nargs, args);
  1581. if (chain_expr)
  1582. CALL_EXPR_STATIC_CHAIN (ret) = chain_expr->get_tree();
  1583. if (excess_type != NULL_TREE)
  1584. {
  1585. // Calling convert here can undo our excess precision change.
  1586. // That may or may not be a bug in convert_to_real.
  1587. ret = build1_loc(location.gcc_location(), NOP_EXPR, rettype, ret);
  1588. }
  1589. delete[] args;
  1590. return this->make_expression(ret);
  1591. }
  1592. // An expression as a statement.
  1593. Bstatement*
  1594. Gcc_backend::expression_statement(Bexpression* expr)
  1595. {
  1596. return this->make_statement(expr->get_tree());
  1597. }
  1598. // Variable initialization.
  1599. Bstatement*
  1600. Gcc_backend::init_statement(Bvariable* var, Bexpression* init)
  1601. {
  1602. tree var_tree = var->get_tree();
  1603. tree init_tree = init->get_tree();
  1604. if (var_tree == error_mark_node || init_tree == error_mark_node)
  1605. return this->error_statement();
  1606. gcc_assert(TREE_CODE(var_tree) == VAR_DECL);
  1607. // To avoid problems with GNU ld, we don't make zero-sized
  1608. // externally visible variables. That might lead us to doing an
  1609. // initialization of a zero-sized expression to a non-zero sized
  1610. // variable, or vice-versa. Avoid crashes by omitting the
  1611. // initializer. Such initializations don't mean anything anyhow.
  1612. if (int_size_in_bytes(TREE_TYPE(var_tree)) != 0
  1613. && init_tree != NULL_TREE
  1614. && int_size_in_bytes(TREE_TYPE(init_tree)) != 0)
  1615. {
  1616. DECL_INITIAL(var_tree) = init_tree;
  1617. init_tree = NULL_TREE;
  1618. }
  1619. tree ret = build1_loc(DECL_SOURCE_LOCATION(var_tree), DECL_EXPR,
  1620. void_type_node, var_tree);
  1621. if (init_tree != NULL_TREE)
  1622. ret = build2_loc(DECL_SOURCE_LOCATION(var_tree), COMPOUND_EXPR,
  1623. void_type_node, init_tree, ret);
  1624. return this->make_statement(ret);
  1625. }
  1626. // Assignment.
  1627. Bstatement*
  1628. Gcc_backend::assignment_statement(Bexpression* lhs, Bexpression* rhs,
  1629. Location location)
  1630. {
  1631. tree lhs_tree = lhs->get_tree();
  1632. tree rhs_tree = rhs->get_tree();
  1633. if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
  1634. return this->error_statement();
  1635. // To avoid problems with GNU ld, we don't make zero-sized
  1636. // externally visible variables. That might lead us to doing an
  1637. // assignment of a zero-sized expression to a non-zero sized
  1638. // expression; avoid crashes here by avoiding assignments of
  1639. // zero-sized expressions. Such assignments don't really mean
  1640. // anything anyhow.
  1641. if (int_size_in_bytes(TREE_TYPE(lhs_tree)) == 0
  1642. || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
  1643. return this->compound_statement(this->expression_statement(lhs),
  1644. this->expression_statement(rhs));
  1645. // Sometimes the same unnamed Go type can be created multiple times
  1646. // and thus have multiple tree representations. Make sure this does
  1647. // not confuse the middle-end.
  1648. if (TREE_TYPE(lhs_tree) != TREE_TYPE(rhs_tree))
  1649. {
  1650. tree lhs_type_tree = TREE_TYPE(lhs_tree);
  1651. gcc_assert(TREE_CODE(lhs_type_tree) == TREE_CODE(TREE_TYPE(rhs_tree)));
  1652. if (POINTER_TYPE_P(lhs_type_tree)
  1653. || INTEGRAL_TYPE_P(lhs_type_tree)
  1654. || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
  1655. || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
  1656. rhs_tree = fold_convert_loc(location.gcc_location(), lhs_type_tree,
  1657. rhs_tree);
  1658. else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
  1659. || TREE_CODE(lhs_type_tree) == ARRAY_TYPE)
  1660. {
  1661. gcc_assert(int_size_in_bytes(lhs_type_tree)
  1662. == int_size_in_bytes(TREE_TYPE(rhs_tree)));
  1663. rhs_tree = fold_build1_loc(location.gcc_location(),
  1664. VIEW_CONVERT_EXPR,
  1665. lhs_type_tree, rhs_tree);
  1666. }
  1667. }
  1668. return this->make_statement(fold_build2_loc(location.gcc_location(),
  1669. MODIFY_EXPR,
  1670. void_type_node,
  1671. lhs_tree, rhs_tree));
  1672. }
  1673. // Return.
  1674. Bstatement*
  1675. Gcc_backend::return_statement(Bfunction* bfunction,
  1676. const std::vector<Bexpression*>& vals,
  1677. Location location)
  1678. {
  1679. tree fntree = bfunction->get_tree();
  1680. if (fntree == error_mark_node)
  1681. return this->error_statement();
  1682. tree result = DECL_RESULT(fntree);
  1683. if (result == error_mark_node)
  1684. return this->error_statement();
  1685. tree ret;
  1686. if (vals.empty())
  1687. ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR, void_type_node,
  1688. NULL_TREE);
  1689. else if (vals.size() == 1)
  1690. {
  1691. tree val = vals.front()->get_tree();
  1692. if (val == error_mark_node)
  1693. return this->error_statement();
  1694. tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
  1695. void_type_node, result,
  1696. vals.front()->get_tree());
  1697. ret = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
  1698. void_type_node, set);
  1699. }
  1700. else
  1701. {
  1702. // To return multiple values, copy the values into a temporary
  1703. // variable of the right structure type, and then assign the
  1704. // temporary variable to the DECL_RESULT in the return
  1705. // statement.
  1706. tree stmt_list = NULL_TREE;
  1707. tree rettype = TREE_TYPE(result);
  1708. if (DECL_STRUCT_FUNCTION(fntree) == NULL)
  1709. push_struct_function(fntree);
  1710. else
  1711. push_cfun(DECL_STRUCT_FUNCTION(fntree));
  1712. tree rettmp = create_tmp_var(rettype, "RESULT");
  1713. pop_cfun();
  1714. tree field = TYPE_FIELDS(rettype);
  1715. for (std::vector<Bexpression*>::const_iterator p = vals.begin();
  1716. p != vals.end();
  1717. p++, field = DECL_CHAIN(field))
  1718. {
  1719. gcc_assert(field != NULL_TREE);
  1720. tree ref = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
  1721. TREE_TYPE(field), rettmp, field,
  1722. NULL_TREE);
  1723. tree val = (*p)->get_tree();
  1724. if (val == error_mark_node)
  1725. return this->error_statement();
  1726. tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
  1727. void_type_node,
  1728. ref, (*p)->get_tree());
  1729. append_to_statement_list(set, &stmt_list);
  1730. }
  1731. gcc_assert(field == NULL_TREE);
  1732. tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
  1733. void_type_node,
  1734. result, rettmp);
  1735. tree ret_expr = fold_build1_loc(location.gcc_location(), RETURN_EXPR,
  1736. void_type_node, set);
  1737. append_to_statement_list(ret_expr, &stmt_list);
  1738. ret = stmt_list;
  1739. }
  1740. return this->make_statement(ret);
  1741. }
  1742. // Create a statement that attempts to execute BSTAT and calls EXCEPT_STMT if an
  1743. // error occurs. EXCEPT_STMT may be NULL. FINALLY_STMT may be NULL and if not
  1744. // NULL, it will always be executed. This is used for handling defers in Go
  1745. // functions. In C++, the resulting code is of this form:
  1746. // try { BSTAT; } catch { EXCEPT_STMT; } finally { FINALLY_STMT; }
  1747. Bstatement*
  1748. Gcc_backend::exception_handler_statement(Bstatement* bstat,
  1749. Bstatement* except_stmt,
  1750. Bstatement* finally_stmt,
  1751. Location location)
  1752. {
  1753. tree stat_tree = bstat->get_tree();
  1754. tree except_tree = except_stmt == NULL ? NULL_TREE : except_stmt->get_tree();
  1755. tree finally_tree = finally_stmt == NULL
  1756. ? NULL_TREE
  1757. : finally_stmt->get_tree();
  1758. if (stat_tree == error_mark_node
  1759. || except_tree == error_mark_node
  1760. || finally_tree == error_mark_node)
  1761. return this->error_statement();
  1762. if (except_tree != NULL_TREE)
  1763. stat_tree = build2_loc(location.gcc_location(), TRY_CATCH_EXPR,
  1764. void_type_node, stat_tree,
  1765. build2_loc(location.gcc_location(), CATCH_EXPR,
  1766. void_type_node, NULL, except_tree));
  1767. if (finally_tree != NULL_TREE)
  1768. stat_tree = build2_loc(location.gcc_location(), TRY_FINALLY_EXPR,
  1769. void_type_node, stat_tree, finally_tree);
  1770. return this->make_statement(stat_tree);
  1771. }
  1772. // If.
  1773. Bstatement*
  1774. Gcc_backend::if_statement(Bexpression* condition, Bblock* then_block,
  1775. Bblock* else_block, Location location)
  1776. {
  1777. tree cond_tree = condition->get_tree();
  1778. tree then_tree = then_block->get_tree();
  1779. tree else_tree = else_block == NULL ? NULL_TREE : else_block->get_tree();
  1780. if (cond_tree == error_mark_node
  1781. || then_tree == error_mark_node
  1782. || else_tree == error_mark_node)
  1783. return this->error_statement();
  1784. tree ret = build3_loc(location.gcc_location(), COND_EXPR, void_type_node,
  1785. cond_tree, then_tree, else_tree);
  1786. return this->make_statement(ret);
  1787. }
  1788. // Switch.
  1789. Bstatement*
  1790. Gcc_backend::switch_statement(
  1791. Bfunction* function,
  1792. Bexpression* value,
  1793. const std::vector<std::vector<Bexpression*> >& cases,
  1794. const std::vector<Bstatement*>& statements,
  1795. Location switch_location)
  1796. {
  1797. gcc_assert(cases.size() == statements.size());
  1798. tree decl = function->get_tree();
  1799. if (DECL_STRUCT_FUNCTION(decl) == NULL)
  1800. push_struct_function(decl);
  1801. else
  1802. push_cfun(DECL_STRUCT_FUNCTION(decl));
  1803. tree stmt_list = NULL_TREE;
  1804. std::vector<std::vector<Bexpression*> >::const_iterator pc = cases.begin();
  1805. for (std::vector<Bstatement*>::const_iterator ps = statements.begin();
  1806. ps != statements.end();
  1807. ++ps, ++pc)
  1808. {
  1809. if (pc->empty())
  1810. {
  1811. source_location loc = (*ps != NULL
  1812. ? EXPR_LOCATION((*ps)->get_tree())
  1813. : UNKNOWN_LOCATION);
  1814. tree label = create_artificial_label(loc);
  1815. tree c = build_case_label(NULL_TREE, NULL_TREE, label);
  1816. append_to_statement_list(c, &stmt_list);
  1817. }
  1818. else
  1819. {
  1820. for (std::vector<Bexpression*>::const_iterator pcv = pc->begin();
  1821. pcv != pc->end();
  1822. ++pcv)
  1823. {
  1824. tree t = (*pcv)->get_tree();
  1825. if (t == error_mark_node)
  1826. return this->error_statement();
  1827. source_location loc = EXPR_LOCATION(t);
  1828. tree label = create_artificial_label(loc);
  1829. tree c = build_case_label((*pcv)->get_tree(), NULL_TREE, label);
  1830. append_to_statement_list(c, &stmt_list);
  1831. }
  1832. }
  1833. if (*ps != NULL)
  1834. {
  1835. tree t = (*ps)->get_tree();
  1836. if (t == error_mark_node)
  1837. return this->error_statement();
  1838. append_to_statement_list(t, &stmt_list);
  1839. }
  1840. }
  1841. pop_cfun();
  1842. tree tv = value->get_tree();
  1843. if (tv == error_mark_node)
  1844. return this->error_statement();
  1845. tree t = build3_loc(switch_location.gcc_location(), SWITCH_EXPR,
  1846. NULL_TREE, tv, stmt_list, NULL_TREE);
  1847. return this->make_statement(t);
  1848. }
  1849. // Pair of statements.
  1850. Bstatement*
  1851. Gcc_backend::compound_statement(Bstatement* s1, Bstatement* s2)
  1852. {
  1853. tree stmt_list = NULL_TREE;
  1854. tree t = s1->get_tree();
  1855. if (t == error_mark_node)
  1856. return this->error_statement();
  1857. append_to_statement_list(t, &stmt_list);
  1858. t = s2->get_tree();
  1859. if (t == error_mark_node)
  1860. return this->error_statement();
  1861. append_to_statement_list(t, &stmt_list);
  1862. // If neither statement has any side effects, stmt_list can be NULL
  1863. // at this point.
  1864. if (stmt_list == NULL_TREE)
  1865. stmt_list = integer_zero_node;
  1866. return this->make_statement(stmt_list);
  1867. }
  1868. // List of statements.
  1869. Bstatement*
  1870. Gcc_backend::statement_list(const std::vector<Bstatement*>& statements)
  1871. {
  1872. tree stmt_list = NULL_TREE;
  1873. for (std::vector<Bstatement*>::const_iterator p = statements.begin();
  1874. p != statements.end();
  1875. ++p)
  1876. {
  1877. tree t = (*p)->get_tree();
  1878. if (t == error_mark_node)
  1879. return this->error_statement();
  1880. append_to_statement_list(t, &stmt_list);
  1881. }
  1882. return this->make_statement(stmt_list);
  1883. }
  1884. // Make a block. For some reason gcc uses a dual structure for
  1885. // blocks: BLOCK tree nodes and BIND_EXPR tree nodes. Since the
  1886. // BIND_EXPR node points to the BLOCK node, we store the BIND_EXPR in
  1887. // the Bblock.
  1888. Bblock*
  1889. Gcc_backend::block(Bfunction* function, Bblock* enclosing,
  1890. const std::vector<Bvariable*>& vars,
  1891. Location start_location,
  1892. Location)
  1893. {
  1894. tree block_tree = make_node(BLOCK);
  1895. if (enclosing == NULL)
  1896. {
  1897. tree fndecl = function->get_tree();
  1898. gcc_assert(fndecl != NULL_TREE);
  1899. // We may have already created a block for local variables when
  1900. // we take the address of a parameter.
  1901. if (DECL_INITIAL(fndecl) == NULL_TREE)
  1902. {
  1903. BLOCK_SUPERCONTEXT(block_tree) = fndecl;
  1904. DECL_INITIAL(fndecl) = block_tree;
  1905. }
  1906. else
  1907. {
  1908. tree superblock_tree = DECL_INITIAL(fndecl);
  1909. BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
  1910. tree* pp;
  1911. for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
  1912. *pp != NULL_TREE;
  1913. pp = &BLOCK_CHAIN(*pp))
  1914. ;
  1915. *pp = block_tree;
  1916. }
  1917. }
  1918. else
  1919. {
  1920. tree superbind_tree = enclosing->get_tree();
  1921. tree superblock_tree = BIND_EXPR_BLOCK(superbind_tree);
  1922. gcc_assert(TREE_CODE(superblock_tree) == BLOCK);
  1923. BLOCK_SUPERCONTEXT(block_tree) = superblock_tree;
  1924. tree* pp;
  1925. for (pp = &BLOCK_SUBBLOCKS(superblock_tree);
  1926. *pp != NULL_TREE;
  1927. pp = &BLOCK_CHAIN(*pp))
  1928. ;
  1929. *pp = block_tree;
  1930. }
  1931. tree* pp = &BLOCK_VARS(block_tree);
  1932. for (std::vector<Bvariable*>::const_iterator pv = vars.begin();
  1933. pv != vars.end();
  1934. ++pv)
  1935. {
  1936. *pp = (*pv)->get_tree();
  1937. if (*pp != error_mark_node)
  1938. pp = &DECL_CHAIN(*pp);
  1939. }
  1940. *pp = NULL_TREE;
  1941. TREE_USED(block_tree) = 1;
  1942. tree bind_tree = build3_loc(start_location.gcc_location(), BIND_EXPR,
  1943. void_type_node, BLOCK_VARS(block_tree),
  1944. NULL_TREE, block_tree);
  1945. TREE_SIDE_EFFECTS(bind_tree) = 1;
  1946. return new Bblock(bind_tree);
  1947. }
  1948. // Add statements to a block.
  1949. void
  1950. Gcc_backend::block_add_statements(Bblock* bblock,
  1951. const std::vector<Bstatement*>& statements)
  1952. {
  1953. tree stmt_list = NULL_TREE;
  1954. for (std::vector<Bstatement*>::const_iterator p = statements.begin();
  1955. p != statements.end();
  1956. ++p)
  1957. {
  1958. tree s = (*p)->get_tree();
  1959. if (s != error_mark_node)
  1960. append_to_statement_list(s, &stmt_list);
  1961. }
  1962. tree bind_tree = bblock->get_tree();
  1963. gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
  1964. BIND_EXPR_BODY(bind_tree) = stmt_list;
  1965. }
  1966. // Return a block as a statement.
  1967. Bstatement*
  1968. Gcc_backend::block_statement(Bblock* bblock)
  1969. {
  1970. tree bind_tree = bblock->get_tree();
  1971. gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
  1972. return this->make_statement(bind_tree);
  1973. }
  1974. // This is not static because we declare it with GTY(()) in go-c.h.
  1975. tree go_non_zero_struct;
  1976. // Return a type corresponding to TYPE with non-zero size.
  1977. tree
  1978. Gcc_backend::non_zero_size_type(tree type)
  1979. {
  1980. if (int_size_in_bytes(type) != 0)
  1981. return type;
  1982. switch (TREE_CODE(type))
  1983. {
  1984. case RECORD_TYPE:
  1985. if (TYPE_FIELDS(type) != NULL_TREE)
  1986. {
  1987. tree ns = make_node(RECORD_TYPE);
  1988. tree field_trees = NULL_TREE;
  1989. tree *pp = &field_trees;
  1990. for (tree field = TYPE_FIELDS(type);
  1991. field != NULL_TREE;
  1992. field = DECL_CHAIN(field))
  1993. {
  1994. tree ft = TREE_TYPE(field);
  1995. if (field == TYPE_FIELDS(type))
  1996. ft = non_zero_size_type(ft);
  1997. tree f = build_decl(DECL_SOURCE_LOCATION(field), FIELD_DECL,
  1998. DECL_NAME(field), ft);
  1999. DECL_CONTEXT(f) = ns;
  2000. *pp = f;
  2001. pp = &DECL_CHAIN(f);
  2002. }
  2003. TYPE_FIELDS(ns) = field_trees;
  2004. layout_type(ns);
  2005. return ns;
  2006. }
  2007. if (go_non_zero_struct == NULL_TREE)
  2008. {
  2009. type = make_node(RECORD_TYPE);
  2010. tree field = build_decl(UNKNOWN_LOCATION, FIELD_DECL,
  2011. get_identifier("dummy"),
  2012. boolean_type_node);
  2013. DECL_CONTEXT(field) = type;
  2014. TYPE_FIELDS(type) = field;
  2015. layout_type(type);
  2016. go_non_zero_struct = type;
  2017. }
  2018. return go_non_zero_struct;
  2019. case ARRAY_TYPE:
  2020. {
  2021. tree element_type = non_zero_size_type(TREE_TYPE(type));
  2022. return build_array_type_nelts(element_type, 1);
  2023. }
  2024. default:
  2025. gcc_unreachable();
  2026. }
  2027. gcc_unreachable();
  2028. }
  2029. // Make a global variable.
  2030. Bvariable*
  2031. Gcc_backend::global_variable(const std::string& package_name,
  2032. const std::string& pkgpath,
  2033. const std::string& name,
  2034. Btype* btype,
  2035. bool is_external,
  2036. bool is_hidden,
  2037. bool in_unique_section,
  2038. Location location)
  2039. {
  2040. tree type_tree = btype->get_tree();
  2041. if (type_tree == error_mark_node)
  2042. return this->error_variable();
  2043. // The GNU linker does not like dynamic variables with zero size.
  2044. if ((is_external || !is_hidden) && int_size_in_bytes(type_tree) == 0)
  2045. type_tree = this->non_zero_size_type(type_tree);
  2046. std::string var_name(package_name);
  2047. var_name.push_back('.');
  2048. var_name.append(name);
  2049. tree decl = build_decl(location.gcc_location(), VAR_DECL,
  2050. get_identifier_from_string(var_name),
  2051. type_tree);
  2052. if (is_external)
  2053. DECL_EXTERNAL(decl) = 1;
  2054. else
  2055. TREE_STATIC(decl) = 1;
  2056. if (!is_hidden)
  2057. {
  2058. TREE_PUBLIC(decl) = 1;
  2059. std::string asm_name(pkgpath);
  2060. asm_name.push_back('.');
  2061. asm_name.append(name);
  2062. SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
  2063. }
  2064. TREE_USED(decl) = 1;
  2065. if (in_unique_section)
  2066. resolve_unique_section (decl, 0, 1);
  2067. go_preserve_from_gc(decl);
  2068. return new Bvariable(decl);
  2069. }
  2070. // Set the initial value of a global variable.
  2071. void
  2072. Gcc_backend::global_variable_set_init(Bvariable* var, Bexpression* expr)
  2073. {
  2074. tree expr_tree = expr->get_tree();
  2075. if (expr_tree == error_mark_node)
  2076. return;
  2077. gcc_assert(TREE_CONSTANT(expr_tree));
  2078. tree var_decl = var->get_tree();
  2079. if (var_decl == error_mark_node)
  2080. return;
  2081. DECL_INITIAL(var_decl) = expr_tree;
  2082. // If this variable goes in a unique section, it may need to go into
  2083. // a different one now that DECL_INITIAL is set.
  2084. if (symtab_node::get(var_decl)
  2085. && symtab_node::get(var_decl)->implicit_section)
  2086. {
  2087. set_decl_section_name (var_decl, NULL);
  2088. resolve_unique_section (var_decl,
  2089. compute_reloc_for_constant (expr_tree),
  2090. 1);
  2091. }
  2092. }
  2093. // Make a local variable.
  2094. Bvariable*
  2095. Gcc_backend::local_variable(Bfunction* function, const std::string& name,
  2096. Btype* btype, bool is_address_taken,
  2097. Location location)
  2098. {
  2099. tree type_tree = btype->get_tree();
  2100. if (type_tree == error_mark_node)
  2101. return this->error_variable();
  2102. tree decl = build_decl(location.gcc_location(), VAR_DECL,
  2103. get_identifier_from_string(name),
  2104. type_tree);
  2105. DECL_CONTEXT(decl) = function->get_tree();
  2106. TREE_USED(decl) = 1;
  2107. if (is_address_taken)
  2108. TREE_ADDRESSABLE(decl) = 1;
  2109. go_preserve_from_gc(decl);
  2110. return new Bvariable(decl);
  2111. }
  2112. // Make a function parameter variable.
  2113. Bvariable*
  2114. Gcc_backend::parameter_variable(Bfunction* function, const std::string& name,
  2115. Btype* btype, bool is_address_taken,
  2116. Location location)
  2117. {
  2118. tree type_tree = btype->get_tree();
  2119. if (type_tree == error_mark_node)
  2120. return this->error_variable();
  2121. tree decl = build_decl(location.gcc_location(), PARM_DECL,
  2122. get_identifier_from_string(name),
  2123. type_tree);
  2124. DECL_CONTEXT(decl) = function->get_tree();
  2125. DECL_ARG_TYPE(decl) = type_tree;
  2126. TREE_USED(decl) = 1;
  2127. if (is_address_taken)
  2128. TREE_ADDRESSABLE(decl) = 1;
  2129. go_preserve_from_gc(decl);
  2130. return new Bvariable(decl);
  2131. }
  2132. // Make a static chain variable.
  2133. Bvariable*
  2134. Gcc_backend::static_chain_variable(Bfunction* function, const std::string& name,
  2135. Btype* btype, Location location)
  2136. {
  2137. tree type_tree = btype->get_tree();
  2138. if (type_tree == error_mark_node)
  2139. return this->error_variable();
  2140. tree decl = build_decl(location.gcc_location(), PARM_DECL,
  2141. get_identifier_from_string(name), type_tree);
  2142. tree fndecl = function->get_tree();
  2143. DECL_CONTEXT(decl) = fndecl;
  2144. DECL_ARG_TYPE(decl) = type_tree;
  2145. TREE_USED(decl) = 1;
  2146. DECL_ARTIFICIAL(decl) = 1;
  2147. DECL_IGNORED_P(decl) = 1;
  2148. TREE_READONLY(decl) = 1;
  2149. struct function *f = DECL_STRUCT_FUNCTION(fndecl);
  2150. if (f == NULL)
  2151. {
  2152. push_struct_function(fndecl);
  2153. pop_cfun();
  2154. f = DECL_STRUCT_FUNCTION(fndecl);
  2155. }
  2156. gcc_assert(f->static_chain_decl == NULL);
  2157. f->static_chain_decl = decl;
  2158. DECL_STATIC_CHAIN(fndecl) = 1;
  2159. go_preserve_from_gc(decl);
  2160. return new Bvariable(decl);
  2161. }
  2162. // Make a temporary variable.
  2163. Bvariable*
  2164. Gcc_backend::temporary_variable(Bfunction* function, Bblock* bblock,
  2165. Btype* btype, Bexpression* binit,
  2166. bool is_address_taken,
  2167. Location location,
  2168. Bstatement** pstatement)
  2169. {
  2170. gcc_assert(function != NULL);
  2171. tree decl = function->get_tree();
  2172. tree type_tree = btype->get_tree();
  2173. tree init_tree = binit == NULL ? NULL_TREE : binit->get_tree();
  2174. if (type_tree == error_mark_node
  2175. || init_tree == error_mark_node
  2176. || decl == error_mark_node)
  2177. {
  2178. *pstatement = this->error_statement();
  2179. return this->error_variable();
  2180. }
  2181. tree var;
  2182. // We can only use create_tmp_var if the type is not addressable.
  2183. if (!TREE_ADDRESSABLE(type_tree))
  2184. {
  2185. if (DECL_STRUCT_FUNCTION(decl) == NULL)
  2186. push_struct_function(decl);
  2187. else
  2188. push_cfun(DECL_STRUCT_FUNCTION(decl));
  2189. var = create_tmp_var(type_tree, "GOTMP");
  2190. pop_cfun();
  2191. }
  2192. else
  2193. {
  2194. gcc_assert(bblock != NULL);
  2195. var = build_decl(location.gcc_location(), VAR_DECL,
  2196. create_tmp_var_name("GOTMP"),
  2197. type_tree);
  2198. DECL_ARTIFICIAL(var) = 1;
  2199. DECL_IGNORED_P(var) = 1;
  2200. TREE_USED(var) = 1;
  2201. DECL_CONTEXT(var) = decl;
  2202. // We have to add this variable to the BLOCK and the BIND_EXPR.
  2203. tree bind_tree = bblock->get_tree();
  2204. gcc_assert(TREE_CODE(bind_tree) == BIND_EXPR);
  2205. tree block_tree = BIND_EXPR_BLOCK(bind_tree);
  2206. gcc_assert(TREE_CODE(block_tree) == BLOCK);
  2207. DECL_CHAIN(var) = BLOCK_VARS(block_tree);
  2208. BLOCK_VARS(block_tree) = var;
  2209. BIND_EXPR_VARS(bind_tree) = BLOCK_VARS(block_tree);
  2210. }
  2211. if (this->type_size(btype) != 0 && init_tree != NULL_TREE)
  2212. DECL_INITIAL(var) = fold_convert_loc(location.gcc_location(), type_tree,
  2213. init_tree);
  2214. if (is_address_taken)
  2215. TREE_ADDRESSABLE(var) = 1;
  2216. *pstatement = this->make_statement(build1_loc(location.gcc_location(),
  2217. DECL_EXPR,
  2218. void_type_node, var));
  2219. // Don't initialize VAR with BINIT, but still evaluate BINIT for
  2220. // its side effects.
  2221. if (this->type_size(btype) == 0 && init_tree != NULL_TREE)
  2222. *pstatement = this->compound_statement(this->expression_statement(binit),
  2223. *pstatement);
  2224. return new Bvariable(var);
  2225. }
  2226. // Create an implicit variable that is compiler-defined. This is used when
  2227. // generating GC root variables and storing the values of a slice initializer.
  2228. Bvariable*
  2229. Gcc_backend::implicit_variable(const std::string& name, Btype* type,
  2230. bool is_hidden, bool is_constant,
  2231. bool is_common, int64_t alignment)
  2232. {
  2233. tree type_tree = type->get_tree();
  2234. if (type_tree == error_mark_node)
  2235. return this->error_variable();
  2236. tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
  2237. get_identifier_from_string(name), type_tree);
  2238. DECL_EXTERNAL(decl) = 0;
  2239. TREE_PUBLIC(decl) = !is_hidden;
  2240. TREE_STATIC(decl) = 1;
  2241. TREE_USED(decl) = 1;
  2242. DECL_ARTIFICIAL(decl) = 1;
  2243. if (is_common)
  2244. {
  2245. DECL_COMMON(decl) = 1;
  2246. // When the initializer for one implicit_variable refers to another,
  2247. // it needs to know the visibility of the referenced struct so that
  2248. // compute_reloc_for_constant will return the right value. On many
  2249. // systems calling make_decl_one_only will mark the decl as weak,
  2250. // which will change the return value of compute_reloc_for_constant.
  2251. // We can't reliably call make_decl_one_only yet, because we don't
  2252. // yet know the initializer. This issue doesn't arise in C because
  2253. // Go initializers, unlike C initializers, can be indirectly
  2254. // recursive. To ensure that compute_reloc_for_constant computes
  2255. // the right value if some other initializer refers to this one, we
  2256. // mark this symbol as weak here. We undo that below in
  2257. // immutable_struct_set_init before calling mark_decl_one_only.
  2258. DECL_WEAK(decl) = 1;
  2259. }
  2260. if (is_constant)
  2261. {
  2262. TREE_READONLY(decl) = 1;
  2263. TREE_CONSTANT(decl) = 1;
  2264. }
  2265. if (alignment != 0)
  2266. {
  2267. DECL_ALIGN(decl) = alignment * BITS_PER_UNIT;
  2268. DECL_USER_ALIGN(decl) = 1;
  2269. }
  2270. go_preserve_from_gc(decl);
  2271. return new Bvariable(decl);
  2272. }
  2273. // Set the initalizer for a variable created by implicit_variable.
  2274. // This is where we finish compiling the variable.
  2275. void
  2276. Gcc_backend::implicit_variable_set_init(Bvariable* var, const std::string&,
  2277. Btype*, bool, bool, bool is_common,
  2278. Bexpression* init)
  2279. {
  2280. tree decl = var->get_tree();
  2281. tree init_tree;
  2282. if (init == NULL)
  2283. init_tree = NULL_TREE;
  2284. else
  2285. init_tree = init->get_tree();
  2286. if (decl == error_mark_node || init_tree == error_mark_node)
  2287. return;
  2288. DECL_INITIAL(decl) = init_tree;
  2289. // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
  2290. // See the comment where DECL_WEAK is set in implicit_variable.
  2291. if (is_common)
  2292. {
  2293. DECL_WEAK(decl) = 0;
  2294. make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
  2295. }
  2296. resolve_unique_section(decl, 2, 1);
  2297. rest_of_decl_compilation(decl, 1, 0);
  2298. }
  2299. // Return a reference to an implicit variable defined in another package.
  2300. Bvariable*
  2301. Gcc_backend::implicit_variable_reference(const std::string& name, Btype* btype)
  2302. {
  2303. tree type_tree = btype->get_tree();
  2304. if (type_tree == error_mark_node)
  2305. return this->error_variable();
  2306. tree decl = build_decl(BUILTINS_LOCATION, VAR_DECL,
  2307. get_identifier_from_string(name), type_tree);
  2308. DECL_EXTERNAL(decl) = 0;
  2309. TREE_PUBLIC(decl) = 1;
  2310. TREE_STATIC(decl) = 1;
  2311. DECL_ARTIFICIAL(decl) = 1;
  2312. go_preserve_from_gc(decl);
  2313. return new Bvariable(decl);
  2314. }
  2315. // Create a named immutable initialized data structure.
  2316. Bvariable*
  2317. Gcc_backend::immutable_struct(const std::string& name, bool is_hidden,
  2318. bool is_common, Btype* btype, Location location)
  2319. {
  2320. tree type_tree = btype->get_tree();
  2321. if (type_tree == error_mark_node)
  2322. return this->error_variable();
  2323. gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
  2324. tree decl = build_decl(location.gcc_location(), VAR_DECL,
  2325. get_identifier_from_string(name),
  2326. build_qualified_type(type_tree, TYPE_QUAL_CONST));
  2327. TREE_STATIC(decl) = 1;
  2328. TREE_USED(decl) = 1;
  2329. TREE_READONLY(decl) = 1;
  2330. TREE_CONSTANT(decl) = 1;
  2331. DECL_ARTIFICIAL(decl) = 1;
  2332. if (!is_hidden)
  2333. TREE_PUBLIC(decl) = 1;
  2334. // When the initializer for one immutable_struct refers to another,
  2335. // it needs to know the visibility of the referenced struct so that
  2336. // compute_reloc_for_constant will return the right value. On many
  2337. // systems calling make_decl_one_only will mark the decl as weak,
  2338. // which will change the return value of compute_reloc_for_constant.
  2339. // We can't reliably call make_decl_one_only yet, because we don't
  2340. // yet know the initializer. This issue doesn't arise in C because
  2341. // Go initializers, unlike C initializers, can be indirectly
  2342. // recursive. To ensure that compute_reloc_for_constant computes
  2343. // the right value if some other initializer refers to this one, we
  2344. // mark this symbol as weak here. We undo that below in
  2345. // immutable_struct_set_init before calling mark_decl_one_only.
  2346. if (is_common)
  2347. DECL_WEAK(decl) = 1;
  2348. // We don't call rest_of_decl_compilation until we have the
  2349. // initializer.
  2350. go_preserve_from_gc(decl);
  2351. return new Bvariable(decl);
  2352. }
  2353. // Set the initializer for a variable created by immutable_struct.
  2354. // This is where we finish compiling the variable.
  2355. void
  2356. Gcc_backend::immutable_struct_set_init(Bvariable* var, const std::string&,
  2357. bool, bool is_common, Btype*, Location,
  2358. Bexpression* initializer)
  2359. {
  2360. tree decl = var->get_tree();
  2361. tree init_tree = initializer->get_tree();
  2362. if (decl == error_mark_node || init_tree == error_mark_node)
  2363. return;
  2364. DECL_INITIAL(decl) = init_tree;
  2365. // Now that DECL_INITIAL is set, we can't call make_decl_one_only.
  2366. // See the comment where DECL_WEAK is set in immutable_struct.
  2367. if (is_common)
  2368. {
  2369. DECL_WEAK(decl) = 0;
  2370. make_decl_one_only(decl, DECL_ASSEMBLER_NAME(decl));
  2371. }
  2372. // These variables are often unneeded in the final program, so put
  2373. // them in their own section so that linker GC can discard them.
  2374. resolve_unique_section(decl,
  2375. compute_reloc_for_constant (init_tree),
  2376. 1);
  2377. rest_of_decl_compilation(decl, 1, 0);
  2378. }
  2379. // Return a reference to an immutable initialized data structure
  2380. // defined in another package.
  2381. Bvariable*
  2382. Gcc_backend::immutable_struct_reference(const std::string& name, Btype* btype,
  2383. Location location)
  2384. {
  2385. tree type_tree = btype->get_tree();
  2386. if (type_tree == error_mark_node)
  2387. return this->error_variable();
  2388. gcc_assert(TREE_CODE(type_tree) == RECORD_TYPE);
  2389. tree decl = build_decl(location.gcc_location(), VAR_DECL,
  2390. get_identifier_from_string(name),
  2391. build_qualified_type(type_tree, TYPE_QUAL_CONST));
  2392. TREE_READONLY(decl) = 1;
  2393. TREE_CONSTANT(decl) = 1;
  2394. DECL_ARTIFICIAL(decl) = 1;
  2395. TREE_PUBLIC(decl) = 1;
  2396. DECL_EXTERNAL(decl) = 1;
  2397. go_preserve_from_gc(decl);
  2398. return new Bvariable(decl);
  2399. }
  2400. // Make a label.
  2401. Blabel*
  2402. Gcc_backend::label(Bfunction* function, const std::string& name,
  2403. Location location)
  2404. {
  2405. tree decl;
  2406. if (name.empty())
  2407. {
  2408. tree func_tree = function->get_tree();
  2409. if (DECL_STRUCT_FUNCTION(func_tree) == NULL)
  2410. push_struct_function(func_tree);
  2411. else
  2412. push_cfun(DECL_STRUCT_FUNCTION(func_tree));
  2413. decl = create_artificial_label(location.gcc_location());
  2414. pop_cfun();
  2415. }
  2416. else
  2417. {
  2418. tree id = get_identifier_from_string(name);
  2419. decl = build_decl(location.gcc_location(), LABEL_DECL, id,
  2420. void_type_node);
  2421. DECL_CONTEXT(decl) = function->get_tree();
  2422. }
  2423. return new Blabel(decl);
  2424. }
  2425. // Make a statement which defines a label.
  2426. Bstatement*
  2427. Gcc_backend::label_definition_statement(Blabel* label)
  2428. {
  2429. tree lab = label->get_tree();
  2430. tree ret = fold_build1_loc(DECL_SOURCE_LOCATION(lab), LABEL_EXPR,
  2431. void_type_node, lab);
  2432. return this->make_statement(ret);
  2433. }
  2434. // Make a goto statement.
  2435. Bstatement*
  2436. Gcc_backend::goto_statement(Blabel* label, Location location)
  2437. {
  2438. tree lab = label->get_tree();
  2439. tree ret = fold_build1_loc(location.gcc_location(), GOTO_EXPR, void_type_node,
  2440. lab);
  2441. return this->make_statement(ret);
  2442. }
  2443. // Get the address of a label.
  2444. Bexpression*
  2445. Gcc_backend::label_address(Blabel* label, Location location)
  2446. {
  2447. tree lab = label->get_tree();
  2448. TREE_USED(lab) = 1;
  2449. TREE_ADDRESSABLE(lab) = 1;
  2450. tree ret = fold_convert_loc(location.gcc_location(), ptr_type_node,
  2451. build_fold_addr_expr_loc(location.gcc_location(),
  2452. lab));
  2453. return this->make_expression(ret);
  2454. }
  2455. // Declare or define a new function.
  2456. Bfunction*
  2457. Gcc_backend::function(Btype* fntype, const std::string& name,
  2458. const std::string& asm_name, bool is_visible,
  2459. bool is_declaration, bool is_inlinable,
  2460. bool disable_split_stack, bool in_unique_section,
  2461. Location location)
  2462. {
  2463. tree functype = fntype->get_tree();
  2464. if (functype != error_mark_node)
  2465. {
  2466. gcc_assert(FUNCTION_POINTER_TYPE_P(functype));
  2467. functype = TREE_TYPE(functype);
  2468. }
  2469. tree id = get_identifier_from_string(name);
  2470. if (functype == error_mark_node || id == error_mark_node)
  2471. return this->error_function();
  2472. tree decl = build_decl(location.gcc_location(), FUNCTION_DECL, id, functype);
  2473. if (!asm_name.empty())
  2474. SET_DECL_ASSEMBLER_NAME(decl, get_identifier_from_string(asm_name));
  2475. if (is_visible)
  2476. TREE_PUBLIC(decl) = 1;
  2477. if (is_declaration)
  2478. DECL_EXTERNAL(decl) = 1;
  2479. else
  2480. {
  2481. tree restype = TREE_TYPE(functype);
  2482. tree resdecl =
  2483. build_decl(location.gcc_location(), RESULT_DECL, NULL_TREE, restype);
  2484. DECL_ARTIFICIAL(resdecl) = 1;
  2485. DECL_IGNORED_P(resdecl) = 1;
  2486. DECL_CONTEXT(resdecl) = decl;
  2487. DECL_RESULT(decl) = resdecl;
  2488. }
  2489. if (!is_inlinable)
  2490. DECL_UNINLINABLE(decl) = 1;
  2491. if (disable_split_stack)
  2492. {
  2493. tree attr = get_identifier("__no_split_stack__");
  2494. DECL_ATTRIBUTES(decl) = tree_cons(attr, NULL_TREE, NULL_TREE);
  2495. }
  2496. if (in_unique_section)
  2497. resolve_unique_section(decl, 0, 1);
  2498. go_preserve_from_gc(decl);
  2499. return new Bfunction(decl);
  2500. }
  2501. // Create a statement that runs all deferred calls for FUNCTION. This should
  2502. // be a statement that looks like this in C++:
  2503. // finish:
  2504. // try { UNDEFER; } catch { CHECK_DEFER; goto finish; }
  2505. Bstatement*
  2506. Gcc_backend::function_defer_statement(Bfunction* function, Bexpression* undefer,
  2507. Bexpression* defer, Location location)
  2508. {
  2509. tree undefer_tree = undefer->get_tree();
  2510. tree defer_tree = defer->get_tree();
  2511. tree fntree = function->get_tree();
  2512. if (undefer_tree == error_mark_node
  2513. || defer_tree == error_mark_node
  2514. || fntree == error_mark_node)
  2515. return this->error_statement();
  2516. if (DECL_STRUCT_FUNCTION(fntree) == NULL)
  2517. push_struct_function(fntree);
  2518. else
  2519. push_cfun(DECL_STRUCT_FUNCTION(fntree));
  2520. tree stmt_list = NULL;
  2521. Blabel* blabel = this->label(function, "", location);
  2522. Bstatement* label_def = this->label_definition_statement(blabel);
  2523. append_to_statement_list(label_def->get_tree(), &stmt_list);
  2524. Bstatement* jump_stmt = this->goto_statement(blabel, location);
  2525. tree jump = jump_stmt->get_tree();
  2526. tree catch_body = build2(COMPOUND_EXPR, void_type_node, defer_tree, jump);
  2527. catch_body = build2(CATCH_EXPR, void_type_node, NULL, catch_body);
  2528. tree try_catch =
  2529. build2(TRY_CATCH_EXPR, void_type_node, undefer_tree, catch_body);
  2530. append_to_statement_list(try_catch, &stmt_list);
  2531. pop_cfun();
  2532. return this->make_statement(stmt_list);
  2533. }
  2534. // Record PARAM_VARS as the variables to use for the parameters of FUNCTION.
  2535. // This will only be called for a function definition.
  2536. bool
  2537. Gcc_backend::function_set_parameters(Bfunction* function,
  2538. const std::vector<Bvariable*>& param_vars)
  2539. {
  2540. tree func_tree = function->get_tree();
  2541. if (func_tree == error_mark_node)
  2542. return false;
  2543. tree params = NULL_TREE;
  2544. tree *pp = &params;
  2545. for (std::vector<Bvariable*>::const_iterator pv = param_vars.begin();
  2546. pv != param_vars.end();
  2547. ++pv)
  2548. {
  2549. *pp = (*pv)->get_tree();
  2550. gcc_assert(*pp != error_mark_node);
  2551. pp = &DECL_CHAIN(*pp);
  2552. }
  2553. *pp = NULL_TREE;
  2554. DECL_ARGUMENTS(func_tree) = params;
  2555. return true;
  2556. }
  2557. // Set the function body for FUNCTION using the code in CODE_BLOCK.
  2558. bool
  2559. Gcc_backend::function_set_body(Bfunction* function, Bstatement* code_stmt)
  2560. {
  2561. tree func_tree = function->get_tree();
  2562. tree code = code_stmt->get_tree();
  2563. if (func_tree == error_mark_node || code == error_mark_node)
  2564. return false;
  2565. DECL_SAVED_TREE(func_tree) = code;
  2566. return true;
  2567. }
  2568. // Look up a named built-in function in the current backend implementation.
  2569. // Returns NULL if no built-in function by that name exists.
  2570. Bfunction*
  2571. Gcc_backend::lookup_builtin(const std::string& name)
  2572. {
  2573. if (this->builtin_functions_.count(name) != 0)
  2574. return this->builtin_functions_[name];
  2575. return NULL;
  2576. }
  2577. // Write the definitions for all TYPE_DECLS, CONSTANT_DECLS,
  2578. // FUNCTION_DECLS, and VARIABLE_DECLS declared globally.
  2579. void
  2580. Gcc_backend::write_global_definitions(
  2581. const std::vector<Btype*>& type_decls,
  2582. const std::vector<Bexpression*>& constant_decls,
  2583. const std::vector<Bfunction*>& function_decls,
  2584. const std::vector<Bvariable*>& variable_decls)
  2585. {
  2586. size_t count_definitions = type_decls.size() + constant_decls.size()
  2587. + function_decls.size() + variable_decls.size();
  2588. tree* defs = new tree[count_definitions];
  2589. // Convert all non-erroneous declarations into Gimple form.
  2590. size_t i = 0;
  2591. for (std::vector<Bvariable*>::const_iterator p = variable_decls.begin();
  2592. p != variable_decls.end();
  2593. ++p)
  2594. {
  2595. if ((*p)->get_tree() != error_mark_node)
  2596. {
  2597. defs[i] = (*p)->get_tree();
  2598. go_preserve_from_gc(defs[i]);
  2599. ++i;
  2600. }
  2601. }
  2602. for (std::vector<Btype*>::const_iterator p = type_decls.begin();
  2603. p != type_decls.end();
  2604. ++p)
  2605. {
  2606. tree type_tree = (*p)->get_tree();
  2607. if (type_tree != error_mark_node
  2608. && IS_TYPE_OR_DECL_P(type_tree))
  2609. {
  2610. defs[i] = TYPE_NAME(type_tree);
  2611. gcc_assert(defs[i] != NULL);
  2612. go_preserve_from_gc(defs[i]);
  2613. ++i;
  2614. }
  2615. }
  2616. for (std::vector<Bexpression*>::const_iterator p = constant_decls.begin();
  2617. p != constant_decls.end();
  2618. ++p)
  2619. {
  2620. if ((*p)->get_tree() != error_mark_node)
  2621. {
  2622. defs[i] = (*p)->get_tree();
  2623. go_preserve_from_gc(defs[i]);
  2624. ++i;
  2625. }
  2626. }
  2627. for (std::vector<Bfunction*>::const_iterator p = function_decls.begin();
  2628. p != function_decls.end();
  2629. ++p)
  2630. {
  2631. tree decl = (*p)->get_tree();
  2632. if (decl != error_mark_node)
  2633. {
  2634. go_preserve_from_gc(decl);
  2635. gimplify_function_tree(decl);
  2636. cgraph_node::finalize_function(decl, true);
  2637. defs[i] = decl;
  2638. ++i;
  2639. }
  2640. }
  2641. // Pass everything back to the middle-end.
  2642. wrapup_global_declarations(defs, i);
  2643. symtab->finalize_compilation_unit();
  2644. check_global_declarations(defs, i);
  2645. emit_debug_global_declarations(defs, i);
  2646. delete[] defs;
  2647. }
  2648. // Define a builtin function. BCODE is the builtin function code
  2649. // defined by builtins.def. NAME is the name of the builtin function.
  2650. // LIBNAME is the name of the corresponding library function, and is
  2651. // NULL if there isn't one. FNTYPE is the type of the function.
  2652. // CONST_P is true if the function has the const attribute.
  2653. void
  2654. Gcc_backend::define_builtin(built_in_function bcode, const char* name,
  2655. const char* libname, tree fntype, bool const_p)
  2656. {
  2657. tree decl = add_builtin_function(name, fntype, bcode, BUILT_IN_NORMAL,
  2658. libname, NULL_TREE);
  2659. if (const_p)
  2660. TREE_READONLY(decl) = 1;
  2661. set_builtin_decl(bcode, decl, true);
  2662. this->builtin_functions_[name] = this->make_function(decl);
  2663. if (libname != NULL)
  2664. {
  2665. decl = add_builtin_function(libname, fntype, bcode, BUILT_IN_NORMAL,
  2666. NULL, NULL_TREE);
  2667. if (const_p)
  2668. TREE_READONLY(decl) = 1;
  2669. this->builtin_functions_[libname] = this->make_function(decl);
  2670. }
  2671. }
  2672. // Return the backend generator.
  2673. Backend*
  2674. go_get_backend()
  2675. {
  2676. return new Gcc_backend();
  2677. }