export.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. // export.cc -- Export declarations in Go frontend.
  2. // Copyright 2009 The Go Authors. All rights reserved.
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. #include "go-system.h"
  6. #include "sha1.h"
  7. #include "go-c.h"
  8. #include "gogo.h"
  9. #include "types.h"
  10. #include "statements.h"
  11. #include "export.h"
  12. // This file handles exporting global declarations.
  13. // Class Export.
  14. // Version 1 magic number.
  15. const int Export::v1_magic_len;
  16. const char Export::v1_magic[Export::v1_magic_len] =
  17. {
  18. 'v', '1', ';', '\n'
  19. };
  20. const int Export::v1_checksum_len;
  21. // Constructor.
  22. Export::Export(Stream* stream)
  23. : stream_(stream), type_refs_(), type_index_(1), packages_()
  24. {
  25. }
  26. // A functor to sort Named_object pointers by name.
  27. struct Sort_bindings
  28. {
  29. bool
  30. operator()(const Named_object* n1, const Named_object* n2) const
  31. { return n1->name() < n2->name(); }
  32. };
  33. // Return true if we should export NO.
  34. static bool
  35. should_export(Named_object* no)
  36. {
  37. // We only export objects which are locally defined.
  38. if (no->package() != NULL)
  39. return false;
  40. // We don't export packages.
  41. if (no->is_package())
  42. return false;
  43. // We don't export hidden names.
  44. if (Gogo::is_hidden_name(no->name()))
  45. return false;
  46. // We don't export nested functions.
  47. if (no->is_function() && no->func_value()->enclosing() != NULL)
  48. return false;
  49. // We don't export thunks.
  50. if (no->is_function() && Gogo::is_thunk(no))
  51. return false;
  52. // Methods are exported with the type, not here.
  53. if (no->is_function()
  54. && no->func_value()->type()->is_method())
  55. return false;
  56. if (no->is_function_declaration()
  57. && no->func_declaration_value()->type()->is_method())
  58. return false;
  59. // Don't export dummy global variables created for initializers when
  60. // used with sinks.
  61. if (no->is_variable() && no->name()[0] == '_' && no->name()[1] == '.')
  62. return false;
  63. return true;
  64. }
  65. // Export those identifiers marked for exporting.
  66. void
  67. Export::export_globals(const std::string& package_name,
  68. const std::string& prefix,
  69. const std::string& pkgpath,
  70. int package_priority,
  71. const std::map<std::string, Package*>& packages,
  72. const std::map<std::string, Package*>& imports,
  73. const std::string& import_init_fn,
  74. const std::set<Import_init>& imported_init_fns,
  75. const Bindings* bindings)
  76. {
  77. // If there have been any errors so far, don't try to export
  78. // anything. That way the export code doesn't have to worry about
  79. // mismatched types or other confusions.
  80. if (saw_errors())
  81. return;
  82. // Export the symbols in sorted order. That will reduce cases where
  83. // irrelevant changes to the source code affect the exported
  84. // interface.
  85. std::vector<Named_object*> exports;
  86. exports.reserve(bindings->size_definitions());
  87. for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
  88. p != bindings->end_definitions();
  89. ++p)
  90. if (should_export(*p))
  91. exports.push_back(*p);
  92. for (Bindings::const_declarations_iterator p =
  93. bindings->begin_declarations();
  94. p != bindings->end_declarations();
  95. ++p)
  96. {
  97. // We export a function declaration as it may be implemented in
  98. // supporting C code. We do not export type declarations.
  99. if (p->second->is_function_declaration()
  100. && should_export(p->second))
  101. exports.push_back(p->second);
  102. }
  103. std::sort(exports.begin(), exports.end(), Sort_bindings());
  104. // Although the export data is readable, at least this version is,
  105. // it is conceptually a binary format. Start with a four byte
  106. // verison number.
  107. this->write_bytes(Export::v1_magic, Export::v1_magic_len);
  108. // The package name.
  109. this->write_c_string("package ");
  110. this->write_string(package_name);
  111. this->write_c_string(";\n");
  112. // The prefix or package path, used for all global symbols.
  113. if (prefix.empty())
  114. {
  115. go_assert(!pkgpath.empty());
  116. this->write_c_string("pkgpath ");
  117. this->write_string(pkgpath);
  118. }
  119. else
  120. {
  121. this->write_c_string("prefix ");
  122. this->write_string(prefix);
  123. }
  124. this->write_c_string(";\n");
  125. // The package priority.
  126. char buf[100];
  127. snprintf(buf, sizeof buf, "priority %d;\n", package_priority);
  128. this->write_c_string(buf);
  129. this->write_packages(packages);
  130. this->write_imports(imports);
  131. this->write_imported_init_fns(package_name, package_priority, import_init_fn,
  132. imported_init_fns);
  133. // FIXME: It might be clever to add something about the processor
  134. // and ABI being used, although ideally any problems in that area
  135. // would be caught by the linker.
  136. for (std::vector<Named_object*>::const_iterator p = exports.begin();
  137. p != exports.end();
  138. ++p)
  139. (*p)->export_named_object(this);
  140. std::string checksum = this->stream_->checksum();
  141. std::string s = "checksum ";
  142. for (std::string::const_iterator p = checksum.begin();
  143. p != checksum.end();
  144. ++p)
  145. {
  146. unsigned char c = *p;
  147. unsigned int dig = c >> 4;
  148. s += dig < 10 ? '0' + dig : 'A' + dig - 10;
  149. dig = c & 0xf;
  150. s += dig < 10 ? '0' + dig : 'A' + dig - 10;
  151. }
  152. s += ";\n";
  153. this->stream_->write_checksum(s);
  154. }
  155. // Sort packages.
  156. static bool
  157. packages_compare(const Package* a, const Package* b)
  158. {
  159. return a->package_name() < b->package_name();
  160. }
  161. // Write out all the known packages whose pkgpath symbol is not a
  162. // simple transformation of the pkgpath, so that the importing code
  163. // can reliably know it.
  164. void
  165. Export::write_packages(const std::map<std::string, Package*>& packages)
  166. {
  167. // Sort for consistent output.
  168. std::vector<Package*> out;
  169. for (std::map<std::string, Package*>::const_iterator p = packages.begin();
  170. p != packages.end();
  171. ++p)
  172. {
  173. if (p->second->pkgpath_symbol()
  174. != Gogo::pkgpath_for_symbol(p->second->pkgpath()))
  175. out.push_back(p->second);
  176. }
  177. std::sort(out.begin(), out.end(), packages_compare);
  178. for (std::vector<Package*>::const_iterator p = out.begin();
  179. p != out.end();
  180. ++p)
  181. {
  182. this->write_c_string("package ");
  183. this->write_string((*p)->package_name());
  184. this->write_c_string(" ");
  185. this->write_string((*p)->pkgpath());
  186. this->write_c_string(" ");
  187. this->write_string((*p)->pkgpath_symbol());
  188. this->write_c_string(";\n");
  189. }
  190. }
  191. // Sort imported packages.
  192. static bool
  193. import_compare(const std::pair<std::string, Package*>& a,
  194. const std::pair<std::string, Package*>& b)
  195. {
  196. return a.first < b.first;
  197. }
  198. // Write out the imported packages.
  199. void
  200. Export::write_imports(const std::map<std::string, Package*>& imports)
  201. {
  202. // Sort the imports for more consistent output.
  203. std::vector<std::pair<std::string, Package*> > imp;
  204. for (std::map<std::string, Package*>::const_iterator p = imports.begin();
  205. p != imports.end();
  206. ++p)
  207. imp.push_back(std::make_pair(p->first, p->second));
  208. std::sort(imp.begin(), imp.end(), import_compare);
  209. for (std::vector<std::pair<std::string, Package*> >::const_iterator p =
  210. imp.begin();
  211. p != imp.end();
  212. ++p)
  213. {
  214. this->write_c_string("import ");
  215. this->write_string(p->second->package_name());
  216. this->write_c_string(" ");
  217. this->write_string(p->second->pkgpath());
  218. this->write_c_string(" \"");
  219. this->write_string(p->first);
  220. this->write_c_string("\";\n");
  221. this->packages_.insert(p->second);
  222. }
  223. }
  224. // Write out the initialization functions which need to run for this
  225. // package.
  226. void
  227. Export::write_imported_init_fns(
  228. const std::string& package_name,
  229. int priority,
  230. const std::string& import_init_fn,
  231. const std::set<Import_init>& imported_init_fns)
  232. {
  233. if (import_init_fn.empty() && imported_init_fns.empty())
  234. return;
  235. this->write_c_string("init");
  236. if (!import_init_fn.empty())
  237. {
  238. this->write_c_string(" ");
  239. this->write_string(package_name);
  240. this->write_c_string(" ");
  241. this->write_string(import_init_fn);
  242. char buf[100];
  243. snprintf(buf, sizeof buf, " %d", priority);
  244. this->write_c_string(buf);
  245. }
  246. if (!imported_init_fns.empty())
  247. {
  248. // Sort the list of functions for more consistent output.
  249. std::vector<Import_init> v;
  250. for (std::set<Import_init>::const_iterator p = imported_init_fns.begin();
  251. p != imported_init_fns.end();
  252. ++p)
  253. v.push_back(*p);
  254. std::sort(v.begin(), v.end());
  255. for (std::vector<Import_init>::const_iterator p = v.begin();
  256. p != v.end();
  257. ++p)
  258. {
  259. this->write_c_string(" ");
  260. this->write_string(p->package_name());
  261. this->write_c_string(" ");
  262. this->write_string(p->init_name());
  263. char buf[100];
  264. snprintf(buf, sizeof buf, " %d", p->priority());
  265. this->write_c_string(buf);
  266. }
  267. }
  268. this->write_c_string(";\n");
  269. }
  270. // Write a name to the export stream.
  271. void
  272. Export::write_name(const std::string& name)
  273. {
  274. if (name.empty())
  275. this->write_c_string("?");
  276. else
  277. this->write_string(Gogo::message_name(name));
  278. }
  279. // Export a type. We have to ensure that on import we create a single
  280. // Named_type node for each named type. We do this by keeping a hash
  281. // table mapping named types to reference numbers. The first time we
  282. // see a named type we assign it a reference number by making an entry
  283. // in the hash table. If we see it again, we just refer to the
  284. // reference number.
  285. // Named types are, of course, associated with packages. Note that we
  286. // may see a named type when importing one package, and then later see
  287. // the same named type when importing a different package. The home
  288. // package may or may not be imported during this compilation. The
  289. // reference number scheme has to get this all right. Basic approach
  290. // taken from "On the Linearization of Graphs and Writing Symbol
  291. // Files" by Robert Griesemer.
  292. void
  293. Export::write_type(const Type* type)
  294. {
  295. // We don't want to assign a reference number to a forward
  296. // declaration to a type which was defined later.
  297. type = type->forwarded();
  298. Type_refs::const_iterator p = this->type_refs_.find(type);
  299. if (p != this->type_refs_.end())
  300. {
  301. // This type was already in the table.
  302. int index = p->second;
  303. go_assert(index != 0);
  304. char buf[30];
  305. snprintf(buf, sizeof buf, "<type %d>", index);
  306. this->write_c_string(buf);
  307. return;
  308. }
  309. const Named_type* named_type = type->named_type();
  310. const Forward_declaration_type* forward = type->forward_declaration_type();
  311. int index = this->type_index_;
  312. ++this->type_index_;
  313. char buf[30];
  314. snprintf(buf, sizeof buf, "<type %d ", index);
  315. this->write_c_string(buf);
  316. if (named_type != NULL || forward != NULL)
  317. {
  318. const Named_object* named_object;
  319. if (named_type != NULL)
  320. {
  321. // The builtin types should have been predefined.
  322. go_assert(!Linemap::is_predeclared_location(named_type->location())
  323. || (named_type->named_object()->package()->package_name()
  324. == "unsafe"));
  325. named_object = named_type->named_object();
  326. }
  327. else
  328. named_object = forward->named_object();
  329. const Package* package = named_object->package();
  330. std::string s = "\"";
  331. if (package != NULL && !Gogo::is_hidden_name(named_object->name()))
  332. {
  333. s += package->pkgpath();
  334. s += '.';
  335. }
  336. s += named_object->name();
  337. s += "\" ";
  338. this->write_string(s);
  339. // It is possible that this type was imported indirectly, and is
  340. // not in a package in the import list. If we have not
  341. // mentioned this package before, write out the package name
  342. // here so that any package importing this one will know it.
  343. if (package != NULL
  344. && this->packages_.find(package) == this->packages_.end())
  345. {
  346. this->write_c_string("\"");
  347. this->write_string(package->package_name());
  348. this->packages_.insert(package);
  349. this->write_c_string("\" ");
  350. }
  351. // We must add a named type to the table now, since the
  352. // definition of the type may refer to the named type via a
  353. // pointer.
  354. this->type_refs_[type] = index;
  355. }
  356. type->export_type(this);
  357. this->write_c_string(">");
  358. if (named_type == NULL)
  359. this->type_refs_[type] = index;
  360. }
  361. // Add the builtin types to the export table.
  362. void
  363. Export::register_builtin_types(Gogo* gogo)
  364. {
  365. this->register_builtin_type(gogo, "int8", BUILTIN_INT8);
  366. this->register_builtin_type(gogo, "int16", BUILTIN_INT16);
  367. this->register_builtin_type(gogo, "int32", BUILTIN_INT32);
  368. this->register_builtin_type(gogo, "int64", BUILTIN_INT64);
  369. this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8);
  370. this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16);
  371. this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32);
  372. this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64);
  373. this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32);
  374. this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64);
  375. this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64);
  376. this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128);
  377. this->register_builtin_type(gogo, "int", BUILTIN_INT);
  378. this->register_builtin_type(gogo, "uint", BUILTIN_UINT);
  379. this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR);
  380. this->register_builtin_type(gogo, "bool", BUILTIN_BOOL);
  381. this->register_builtin_type(gogo, "string", BUILTIN_STRING);
  382. this->register_builtin_type(gogo, "error", BUILTIN_ERROR);
  383. this->register_builtin_type(gogo, "byte", BUILTIN_BYTE);
  384. this->register_builtin_type(gogo, "rune", BUILTIN_RUNE);
  385. }
  386. // Register one builtin type in the export table.
  387. void
  388. Export::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
  389. {
  390. Named_object* named_object = gogo->lookup_global(name);
  391. go_assert(named_object != NULL && named_object->is_type());
  392. std::pair<Type_refs::iterator, bool> ins =
  393. this->type_refs_.insert(std::make_pair(named_object->type_value(), code));
  394. go_assert(ins.second);
  395. // We also insert the underlying type. We can see the underlying
  396. // type at least for string and bool. We skip the type aliases byte
  397. // and rune here.
  398. if (code != BUILTIN_BYTE && code != BUILTIN_RUNE)
  399. {
  400. Type* real_type = named_object->type_value()->real_type();
  401. ins = this->type_refs_.insert(std::make_pair(real_type, code));
  402. go_assert(ins.second);
  403. }
  404. }
  405. // Class Export::Stream.
  406. Export::Stream::Stream()
  407. {
  408. this->checksum_ = new sha1_ctx;
  409. memset(this->checksum_, 0, sizeof(sha1_ctx));
  410. sha1_init_ctx(this->checksum_);
  411. }
  412. Export::Stream::~Stream()
  413. {
  414. }
  415. // Write bytes to the stream. This keeps a checksum of bytes as they
  416. // go by.
  417. void
  418. Export::Stream::write_and_sum_bytes(const char* bytes, size_t length)
  419. {
  420. sha1_process_bytes(bytes, length, this->checksum_);
  421. this->do_write(bytes, length);
  422. }
  423. // Get the checksum.
  424. std::string
  425. Export::Stream::checksum()
  426. {
  427. // Use a union to provide the required alignment.
  428. union
  429. {
  430. char checksum[Export::v1_checksum_len];
  431. long align;
  432. } u;
  433. sha1_finish_ctx(this->checksum_, u.checksum);
  434. return std::string(u.checksum, Export::v1_checksum_len);
  435. }
  436. // Write the checksum string to the export data.
  437. void
  438. Export::Stream::write_checksum(const std::string& s)
  439. {
  440. this->do_write(s.data(), s.length());
  441. }
  442. // Class Stream_to_section.
  443. Stream_to_section::Stream_to_section()
  444. {
  445. }
  446. // Write data to a section.
  447. void
  448. Stream_to_section::do_write(const char* bytes, size_t length)
  449. {
  450. go_write_export_data (bytes, length);
  451. }