cresstr.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. {
  2. Copyright (c) 1998-2002 by Michael van Canneyt
  3. Handles resourcestrings
  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 cresstr;
  18. {$i fpcdefs.inc}
  19. interface
  20. Procedure GenerateResourceStrings;
  21. implementation
  22. uses
  23. SysUtils,
  24. cclasses,
  25. cutils,globtype,globals,systems,
  26. symconst,symtype,symdef,symsym,
  27. verbose,fmodule,ppu,
  28. aasmbase,aasmtai,aasmdata,
  29. aasmcpu;
  30. Type
  31. { These are used to form a singly-linked list, ordered by hash value }
  32. TResourceStringItem = class(TLinkedListItem)
  33. Sym : TConstSym;
  34. Name : String;
  35. Value : Pchar;
  36. Len : Longint;
  37. hash : Cardinal;
  38. constructor Create(asym:TConstsym);
  39. destructor Destroy;override;
  40. procedure CalcHash;
  41. end;
  42. Tresourcestrings=class
  43. private
  44. List : TLinkedList;
  45. procedure ConstSym_Register(p:TObject;arg:pointer);
  46. public
  47. constructor Create;
  48. destructor Destroy;override;
  49. procedure CreateResourceStringData;
  50. Procedure WriteResourceFile;
  51. procedure RegisterResourceStrings;
  52. end;
  53. { ---------------------------------------------------------------------
  54. TRESOURCESTRING_ITEM
  55. ---------------------------------------------------------------------}
  56. constructor TResourceStringItem.Create(asym:TConstsym);
  57. begin
  58. inherited Create;
  59. Sym:=Asym;
  60. Name:=lower(asym.owner.name^+'.'+asym.Name);
  61. Len:=asym.value.len;
  62. GetMem(Value,Len);
  63. Move(asym.value.valueptr^,Value^,Len);
  64. CalcHash;
  65. end;
  66. destructor TResourceStringItem.Destroy;
  67. begin
  68. FreeMem(Value);
  69. end;
  70. procedure TResourceStringItem.CalcHash;
  71. Var
  72. g : Cardinal;
  73. I : longint;
  74. begin
  75. hash:=0;
  76. For I:=0 to Len-1 do { 0 terminated }
  77. begin
  78. hash:=hash shl 4;
  79. inc(Hash,Ord(Value[i]));
  80. g:=hash and ($f shl 28);
  81. if g<>0 then
  82. begin
  83. hash:=hash xor (g shr 24);
  84. hash:=hash xor g;
  85. end;
  86. end;
  87. If Hash=0 then
  88. Hash:=$ffffffff;
  89. end;
  90. { ---------------------------------------------------------------------
  91. Tresourcestrings
  92. ---------------------------------------------------------------------}
  93. Constructor Tresourcestrings.Create;
  94. begin
  95. List:=TLinkedList.Create;
  96. end;
  97. Destructor Tresourcestrings.Destroy;
  98. begin
  99. List.Free;
  100. end;
  101. procedure Tresourcestrings.CreateResourceStringData;
  102. function WriteValueString(p:pchar;len:longint):TasmLabel;
  103. var
  104. s : pchar;
  105. referencelab: TAsmLabel;
  106. begin
  107. if (target_info.system in systems_darwin) then
  108. begin
  109. current_asmdata.getdatalabel(referencelab);
  110. current_asmdata.asmlists[al_const].concat(tai_label.create(referencelab));
  111. end;
  112. current_asmdata.getdatalabel(result);
  113. current_asmdata.asmlists[al_const].concat(tai_align.create(const_align(sizeof(pint))));
  114. current_asmdata.asmlists[al_const].concat(tai_const.create_pint(-1));
  115. current_asmdata.asmlists[al_const].concat(tai_const.create_pint(len));
  116. current_asmdata.asmlists[al_const].concat(tai_label.create(result));
  117. if (target_info.system in systems_darwin) then
  118. current_asmdata.asmlists[al_const].concat(tai_directive.create(asd_reference,referencelab.name));
  119. getmem(s,len+1);
  120. move(p^,s^,len);
  121. s[len]:=#0;
  122. current_asmdata.asmlists[al_const].concat(tai_string.create_pchar(s,len));
  123. current_asmdata.asmlists[al_const].concat(tai_const.create_8bit(0));
  124. end;
  125. Var
  126. namelab,
  127. valuelab : tasmlabel;
  128. resstrlab : tasmsymbol;
  129. R : TResourceStringItem;
  130. begin
  131. { Put resourcestrings in a new objectfile. Putting it in multiple files
  132. makes the linking too dependent on the linker script requiring a SORT(*) for
  133. the data sections }
  134. maybe_new_object_file(current_asmdata.asmlists[al_const]);
  135. maybe_new_object_file(current_asmdata.asmlists[al_resourcestrings]);
  136. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'1_START'),sizeof(pint));
  137. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.createname_global(
  138. make_mangledname('RESSTR',current_module.localsymtable,'START'),AT_DATA,0));
  139. { Write unitname entry }
  140. namelab:=WriteValueString(@current_module.localsymtable.name^[1],length(current_module.localsymtable.name^));
  141. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(namelab));
  142. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(nil));
  143. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(nil));
  144. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  145. {$ifdef cpu64bitaddr}
  146. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  147. {$endif cpu64bitaddr}
  148. { Add entries }
  149. R:=TResourceStringItem(List.First);
  150. while assigned(R) do
  151. begin
  152. new_section(current_asmdata.asmlists[al_const],sec_rodata,make_mangledname('RESSTR',current_module.localsymtable,'d_'+r.name),sizeof(pint));
  153. { Write default value }
  154. if assigned(R.value) and (R.len<>0) then
  155. valuelab:=WriteValueString(R.Value,R.Len)
  156. else
  157. valuelab:=nil;
  158. { Append the name as a ansistring. }
  159. namelab:=WriteValueString(@R.Name[1],length(R.name));
  160. {
  161. Resourcestring index:
  162. TResourceStringRecord = Packed Record
  163. Name,
  164. CurrentValue,
  165. DefaultValue : AnsiString;
  166. HashValue : LongWord;
  167. end;
  168. }
  169. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'2_'+r.name),sizeof(pint));
  170. resstrlab:=current_asmdata.DefineAsmSymbol(make_mangledname('RESSTR',R.Sym.owner,R.Sym.name),AB_GLOBAL,AT_DATA);
  171. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol.Create_global(resstrlab,0));
  172. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(namelab));
  173. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(valuelab));
  174. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_sym(valuelab));
  175. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(longint(R.Hash)));
  176. {$ifdef cpu64bitaddr}
  177. current_asmdata.asmlists[al_resourcestrings].concat(tai_const.create_32bit(0));
  178. {$endif cpu64bitaddr}
  179. current_asmdata.asmlists[al_resourcestrings].concat(tai_symbol_end.create(resstrlab));
  180. R:=TResourceStringItem(R.Next);
  181. end;
  182. new_section(current_asmdata.asmlists[al_resourcestrings],sec_data,make_mangledname('RESSTR',current_module.localsymtable,'3_END'),sizeof(pint));
  183. current_asmdata.AsmLists[al_resourcestrings].concat(tai_symbol.createname_global(
  184. make_mangledname('RESSTR',current_module.localsymtable,'END'),AT_DATA,0));
  185. { The darwin/ppc64 assembler or linker seems to have trouble }
  186. { if a section ends with a global label without any data after it. }
  187. { So for safety, just put a dummy value here. }
  188. { Further, the regular linker also kills this symbol when turning }
  189. { on smart linking in case no value appears after it, so put the }
  190. { dummy byte there always }
  191. if (target_info.system in systems_darwin) then
  192. current_asmdata.asmlists[al_resourcestrings].concat(Tai_const.create_8bit(0));
  193. end;
  194. Procedure Tresourcestrings.WriteResourceFile;
  195. Type
  196. TMode = (quoted,unquoted);
  197. Var
  198. F : Text;
  199. Mode : TMode;
  200. R : TResourceStringItem;
  201. C : char;
  202. Col,i : longint;
  203. ResFileName : string;
  204. Procedure Add(Const S : String);
  205. begin
  206. Write(F,S);
  207. inc(Col,length(s));
  208. end;
  209. begin
  210. ResFileName:=ChangeFileExt(current_module.ppufilename^,'.rst');
  211. message1 (general_i_writingresourcefile,ExtractFileName(ResFileName));
  212. Assign(F,ResFileName);
  213. {$i-}
  214. Rewrite(f);
  215. {$i+}
  216. If IOresult<>0 then
  217. begin
  218. message1(general_e_errorwritingresourcefile,ResFileName);
  219. exit;
  220. end;
  221. R:=TResourceStringItem(List.First);
  222. while assigned(R) do
  223. begin
  224. writeln(f);
  225. Writeln(f,'# hash value = ',R.Hash);
  226. col:=0;
  227. Add(R.Name+'=');
  228. Mode:=unquoted;
  229. For I:=0 to R.Len-1 do
  230. begin
  231. C:=R.Value[i];
  232. If (ord(C)>31) and (Ord(c)<=128) and (c<>'''') then
  233. begin
  234. If mode=Quoted then
  235. Add(c)
  236. else
  237. begin
  238. Add(''''+c);
  239. mode:=quoted
  240. end;
  241. end
  242. else
  243. begin
  244. If Mode=quoted then
  245. begin
  246. Add('''');
  247. mode:=unquoted;
  248. end;
  249. Add('#'+tostr(ord(c)));
  250. end;
  251. If Col>72 then
  252. begin
  253. if mode=quoted then
  254. Write (F,'''');
  255. Writeln(F,'+');
  256. Col:=0;
  257. Mode:=unQuoted;
  258. end;
  259. end;
  260. if mode=quoted then
  261. writeln (f,'''');
  262. Writeln(f);
  263. R:=TResourceStringItem(R.Next);
  264. end;
  265. close(f);
  266. end;
  267. procedure Tresourcestrings.ConstSym_Register(p:TObject;arg:pointer);
  268. begin
  269. if (tsym(p).typ=constsym) and
  270. (tconstsym(p).consttyp=constresourcestring) then
  271. List.Concat(tResourceStringItem.Create(TConstsym(p)));
  272. end;
  273. procedure Tresourcestrings.RegisterResourceStrings;
  274. begin
  275. if assigned(current_module.globalsymtable) then
  276. current_module.globalsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  277. current_module.localsymtable.SymList.ForEachCall(@ConstSym_Register,nil);
  278. end;
  279. Procedure GenerateResourceStrings;
  280. var
  281. resstrs : Tresourcestrings;
  282. begin
  283. resstrs:=Tresourcestrings.Create;
  284. resstrs.RegisterResourceStrings;
  285. if not resstrs.List.Empty then
  286. begin
  287. current_module.flags:=current_module.flags or uf_has_resourcestrings;
  288. resstrs.CreateResourceStringData;
  289. resstrs.WriteResourceFile;
  290. end;
  291. resstrs.Free;
  292. end;
  293. end.