expunix.pas 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. {
  2. Copyright (c) 2008 by the Free Pascal Compiler team
  3. This unit implements common support for import,export,link routines
  4. for unix target
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit expunix;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. systems,
  24. export,
  25. symdef,symsym,
  26. aasmbase;
  27. type
  28. texportlibunix=class(texportlib)
  29. private
  30. fexportedsymnames: TCmdStrList;
  31. public
  32. constructor Create; override;
  33. destructor destroy; override;
  34. procedure preparelib(const s : string);override;
  35. procedure exportprocedure(hp : texported_item);override;
  36. procedure exportvar(hp : texported_item);override;
  37. procedure generatelib;override;
  38. property exportedsymnames: TCmdStrList read fexportedsymnames;
  39. end;
  40. implementation
  41. {****************************************************************************
  42. TExportLibUnix
  43. ****************************************************************************}
  44. uses
  45. symconst,
  46. globals,
  47. aasmdata,aasmtai,
  48. fmodule,
  49. {$ifdef cpuhighleveltarget}
  50. symcreat,
  51. {$endif}
  52. cgbase,
  53. hlcgobj,hlcgcpu,
  54. verbose;
  55. constructor texportlibunix.create;
  56. begin
  57. inherited create;
  58. fexportedsymnames:=tcmdstrlist.create_no_double;
  59. end;
  60. destructor texportlibunix.destroy;
  61. begin
  62. fexportedsymnames.free;
  63. inherited destroy;
  64. end;
  65. procedure texportlibunix.preparelib(const s:string);
  66. begin
  67. end;
  68. procedure texportlibunix.exportprocedure(hp : texported_item);
  69. var
  70. hp2 : texported_item;
  71. {$ifdef cpuhighleveltarget}
  72. pd,
  73. wrapperpd: tprocdef;
  74. i: longint;
  75. anyhasalias: boolean;
  76. {$endif cpuhighleveltarget}
  77. begin
  78. { first test the index value }
  79. if eo_index in hp.options then
  80. begin
  81. Message1(parser_e_no_export_with_index_for_target,target_info.shortname);
  82. exit;
  83. end;
  84. { now place in correct order }
  85. hp2:=texported_item(current_module._exports.first);
  86. while assigned(hp2) and
  87. (hp.name^>hp2.name^) do
  88. hp2:=texported_item(hp2.next);
  89. { insert hp there !! }
  90. if assigned(hp2) and (hp2.name^=hp.name^) then
  91. begin
  92. { this is not allowed !! }
  93. duplicatesymbol(hp.name^);
  94. exit;
  95. end;
  96. if hp2=texported_item(current_module._exports.first) then
  97. current_module._exports.concat(hp)
  98. else if assigned(hp2) then
  99. begin
  100. hp.next:=hp2;
  101. hp.previous:=hp2.previous;
  102. if assigned(hp2.previous) then
  103. hp2.previous.next:=hp;
  104. hp2.previous:=hp;
  105. end
  106. else
  107. current_module._exports.concat(hp);
  108. {$ifdef cpuhighleveltarget}
  109. { in case of a high level target create a stub procedure at the node/def
  110. level instead of via hlcg.g_external_wrapper() later on, because it's
  111. hard to manually create a fake procedure there (and it requires a def
  112. anyway) }
  113. { in case of eo_name there is no sym, and this routine is also called from
  114. exportvar() so the sym doesn't have to be a procsym }
  115. if assigned(hp.sym) and
  116. (hp.sym.typ=procsym) then
  117. begin
  118. anyhasalias:=false;
  119. { if the procedure has the exported name as one of its aliases, we don't
  120. need a separate stub }
  121. pd:=nil;
  122. for i:=0 to tprocsym(hp.sym).procdeflist.count-1 do
  123. begin
  124. pd:=tprocdef(tprocsym(hp.sym).procdeflist[i]);
  125. anyhasalias:=pd.has_alias_name(hp.name^);
  126. if anyhasalias then
  127. break;
  128. end;
  129. if not anyhasalias then
  130. begin
  131. { avoid name clashes for the identifier }
  132. wrapperpd:=create_procdef_alias(pd,'$fpc_exported$'+hp.name^,hp.name^,
  133. current_module.localsymtable,nil,
  134. tsk_callthrough,pd);
  135. end;
  136. end;
  137. {$endif cpuhighleveltarget}
  138. end;
  139. procedure texportlibunix.exportvar(hp : texported_item);
  140. begin
  141. hp.is_var:=true;
  142. exportprocedure(hp);
  143. end;
  144. procedure texportlibunix.generatelib; // straight t_linux copy for now.
  145. var
  146. hp2 : texported_item;
  147. pd : tprocdef;
  148. anyhasalias : boolean;
  149. i : longint;
  150. begin
  151. pd:=nil;
  152. create_hlcodegen;
  153. new_section(current_asmdata.asmlists[al_procedures],sec_code,'',0);
  154. hp2:=texported_item(current_module._exports.first);
  155. while assigned(hp2) do
  156. begin
  157. if (not hp2.is_var) and
  158. assigned(hp2.sym) and
  159. (hp2.sym.typ=procsym) then
  160. begin
  161. {$ifndef cpuhighleveltarget}
  162. { the manglednames can already be the same when the procedure
  163. is declared with cdecl }
  164. { note: for "exports" sections we only allow non overloaded procsyms,
  165. so checking all symbols only matters for packages }
  166. anyhasalias:=false;
  167. for i:=0 to tprocsym(hp2.sym).procdeflist.count-1 do
  168. begin
  169. pd:=tprocdef(tprocsym(hp2.sym).procdeflist[i]);
  170. anyhasalias:=pd.has_alias_name(hp2.name^);
  171. if anyhasalias then
  172. break;
  173. end;
  174. if not anyhasalias then
  175. hlcg.g_external_wrapper(current_asmdata.asmlists[al_procedures],pd,hp2.name^,pd.mangledname,true);
  176. {$endif cpuhighleveltarget}
  177. exportedsymnames.insert(hp2.name^);
  178. end
  179. else
  180. begin
  181. if assigned(hp2.sym) and
  182. (hp2.name^<>hp2.sym.mangledname) then
  183. Message2(parser_e_cant_export_var_different_name,hp2.sym.realname,hp2.sym.mangledname)
  184. else
  185. exportedsymnames.insert(hp2.name^);
  186. end;
  187. hp2:=texported_item(hp2.next);
  188. end;
  189. destroy_hlcodegen;
  190. end;
  191. end.