ptconst.pas 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Reads typed constants
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit ptconst;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses symtype,symsym,aasmdata;
  21. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym;in_structure:boolean);
  22. implementation
  23. uses
  24. globtype,systems,globals,verbose,cutils,tokens,
  25. aasmbase,aasmtai,
  26. procinfo,fmodule,
  27. scanner,pbase,pdecvar,
  28. node,nbas,ngtcon,
  29. symconst,symbase,symdef
  30. ;
  31. procedure read_typed_const(list:tasmlist;sym:tstaticvarsym;in_structure:boolean);
  32. var
  33. storefilepos : tfileposinfo;
  34. cursectype : TAsmSectionType;
  35. section : ansistring;
  36. tcbuilder : ttypedconstbuilder;
  37. datalist,
  38. reslist : tasmlist;
  39. restree,
  40. previnit : tnode;
  41. begin
  42. { mark the staticvarsym as typedconst }
  43. include(sym.varoptions,vo_is_typed_const);
  44. { The variable has a value assigned }
  45. sym.varstate:=vs_initialised;
  46. { the variable can't be placed in a register }
  47. sym.varregable:=vr_none;
  48. { generate data for typed const }
  49. storefilepos:=current_filepos;
  50. current_filepos:=sym.fileinfo;
  51. if not(target_info.system in systems_typed_constants_node_init) then
  52. begin
  53. maybe_new_object_file(list);
  54. tcbuilder:=tasmlisttypedconstbuilderclass(ctypedconstbuilder).create(sym);
  55. tasmlisttypedconstbuilder(tcbuilder).parse_into_asmlist(reslist,datalist);
  56. { Certain types like windows WideString are initialized at runtime and cannot
  57. be placed into readonly memory }
  58. if (sym.varspez=vs_const) and not (vo_force_finalize in sym.varoptions) then
  59. cursectype:=sec_rodata
  60. else
  61. cursectype:=sec_data;
  62. tcbuilder.free;
  63. end
  64. else
  65. begin
  66. if assigned(current_structdef) then
  67. previnit:=current_structdef.tcinitcode
  68. else
  69. previnit:=tnode(current_module.tcinitcode);
  70. tcbuilder:=tnodetreetypedconstbuilderclass(ctypedconstbuilder).create(sym,previnit);
  71. restree:=tnodetreetypedconstbuilder(tcbuilder).parse_into_nodetree;
  72. if assigned(current_structdef) then
  73. current_structdef.tcinitcode:=restree
  74. else
  75. current_module.tcinitcode:=restree;
  76. tcbuilder.free;
  77. reslist:=nil;
  78. datalist:=nil;
  79. cursectype:=sec_none;
  80. end;
  81. { Parse hints }
  82. try_consume_hintdirective(sym.symoptions,sym.deprecatedmsg);
  83. consume(_SEMICOLON);
  84. { parse public/external/export/... }
  85. if not in_structure and
  86. (
  87. (
  88. (token = _ID) and
  89. (idtoken in [_EXPORT,_EXTERNAL,_WEAKEXTERNAL,_PUBLIC,_CVAR]) and
  90. (m_cvar_support in current_settings.modeswitches)
  91. ) or
  92. (
  93. (m_mac in current_settings.modeswitches) and
  94. (
  95. (cs_external_var in current_settings.localswitches) or
  96. (cs_externally_visible in current_settings.localswitches)
  97. )
  98. )
  99. ) then
  100. read_public_and_external(sym);
  101. { try to parse a section directive }
  102. if not in_structure and (target_info.system in systems_allow_section) and
  103. (symtablestack.top.symtabletype in [staticsymtable,globalsymtable]) and
  104. (idtoken=_SECTION) then
  105. begin
  106. try_consume_sectiondirective(section);
  107. if section<>'' then
  108. begin
  109. if (sym.varoptions *[vo_is_external,vo_is_weak_external])<>[] then
  110. Message(parser_e_externals_no_section);
  111. if sym.typ<>staticvarsym then
  112. Message(parser_e_section_no_locals);
  113. tstaticvarsym(sym).section:=section;
  114. include(sym.varoptions, vo_has_section);
  115. end;
  116. end;
  117. if not(target_info.system in systems_typed_constants_node_init) then
  118. begin
  119. { only now add items based on the symbolname, because it may }
  120. { have been modified by the directives parsed above }
  121. if vo_has_section in sym.varoptions then
  122. new_section(list,sec_user,sym.section,const_align(sym.vardef.alignment))
  123. else
  124. new_section(list,cursectype,lower(sym.mangledname),const_align(sym.vardef.alignment));
  125. if sym.globalasmsym then
  126. begin
  127. { see same code in ncgutil.insertbssdata }
  128. if (target_dbg.id=dbg_stabx) and
  129. (cs_debuginfo in current_settings.moduleswitches) and
  130. not assigned(current_asmdata.GetAsmSymbol(sym.name)) then
  131. begin
  132. list.concat(tai_symbol.Create(current_asmdata.DefineAsmSymbol(sym.name,AB_LOCAL,AT_DATA),0));
  133. list.concat(tai_directive.Create(asd_reference,sym.name));
  134. end;
  135. list.concat(Tai_symbol.Createname_global(sym.mangledname,AT_DATA,0))
  136. end
  137. else
  138. list.concat(Tai_symbol.Createname(sym.mangledname,AT_DATA,0));
  139. { add the parsed value }
  140. list.concatlist(reslist);
  141. reslist.free;
  142. list.concat(tai_symbol_end.Createname(sym.mangledname));
  143. { and pointed data, if any }
  144. current_asmdata.asmlists[al_const].concatlist(datalist);
  145. datalist.free;
  146. end
  147. else
  148. begin
  149. { nothing to do }
  150. end;
  151. current_filepos:=storefilepos;
  152. end;
  153. end.