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. cutils,cclasses,
  23. systems,
  24. export,
  25. symtype,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. globtype,globals,
  47. aasmdata,aasmtai,aasmcpu,
  48. fmodule,
  49. {$ifdef cpuhighleveltarget}
  50. symcreat,
  51. {$endif}
  52. cgbase,cgutils,cpubase,cgobj,
  53. cgcpu,hlcgobj,hlcgcpu,
  54. ncgutil,
  55. verbose;
  56. constructor texportlibunix.create;
  57. begin
  58. inherited create;
  59. fexportedsymnames:=tcmdstrlist.create_no_double;
  60. end;
  61. destructor texportlibunix.destroy;
  62. begin
  63. fexportedsymnames.free;
  64. inherited destroy;
  65. end;
  66. procedure texportlibunix.preparelib(const s:string);
  67. begin
  68. end;
  69. procedure texportlibunix.exportprocedure(hp : texported_item);
  70. var
  71. hp2 : texported_item;
  72. {$ifdef cpuhighleveltarget}
  73. pd,
  74. wrapperpd: tprocdef;
  75. i: longint;
  76. anyhasalias: boolean;
  77. {$endif cpuhighleveltarget}
  78. begin
  79. { first test the index value }
  80. if eo_index in hp.options then
  81. begin
  82. Message1(parser_e_no_export_with_index_for_target,target_info.shortname);
  83. exit;
  84. end;
  85. { now place in correct order }
  86. hp2:=texported_item(current_module._exports.first);
  87. while assigned(hp2) and
  88. (hp.name^>hp2.name^) do
  89. hp2:=texported_item(hp2.next);
  90. { insert hp there !! }
  91. if assigned(hp2) and (hp2.name^=hp.name^) then
  92. begin
  93. { this is not allowed !! }
  94. duplicatesymbol(hp.name^);
  95. exit;
  96. end;
  97. if hp2=texported_item(current_module._exports.first) then
  98. current_module._exports.concat(hp)
  99. else if assigned(hp2) then
  100. begin
  101. hp.next:=hp2;
  102. hp.previous:=hp2.previous;
  103. if assigned(hp2.previous) then
  104. hp2.previous.next:=hp;
  105. hp2.previous:=hp;
  106. end
  107. else
  108. current_module._exports.concat(hp);
  109. {$ifdef cpuhighleveltarget}
  110. { in case of a high level target create a stub procedure at the node/def
  111. level instead of via hlcg.g_external_wrapper() later on, because it's
  112. hard to manually create a fake procedure there (and it requires a def
  113. anyway) }
  114. { in case of eo_name there is no sym, and this routine is also called from
  115. exportvar() so the sym doesn't have to be a procsym }
  116. if assigned(hp.sym) and
  117. (hp.sym.typ=procsym) then
  118. begin
  119. anyhasalias:=false;
  120. { if the procedure has the exported name as one of its aliases, we don't
  121. need a separate stub }
  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.