]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
re
[mirror_edk2.git] / Tools / Source / MigrationTools / org / tianocore / migration / SourceFileReplacer.java
CommitLineData
b0282412 1/** @file\r
2 \r
3 Copyright (c) 2006, Intel Corporation\r
4 All rights reserved. This program and the accompanying materials\r
5 are licensed and made available under the terms and conditions of the BSD License\r
6 which accompanies this distribution. The full text of the license may be found at\r
7 http://opensource.org/licenses/bsd-license.php\r
8 \r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11 \r
12 **/\r
0dc8c589 13package org.tianocore.migration;\r
14\r
15import java.io.*;\r
16import java.util.*;\r
17import java.util.regex.Matcher;\r
18import java.util.regex.Pattern;\r
19\r
16b7eeef 20public final class SourceFileReplacer implements Common.ForDoAll {\r
27e0221a 21 private static final SourceFileReplacer SFReplacer = new SourceFileReplacer();\r
22 private ModuleInfo mi;\r
23 private static final Set<Common.Laplace> Laplaces = new HashSet<Common.Laplace>();\r
2887f99b 24\r
27e0221a 25 // these sets are used only for printing log of the changes in current file\r
26 private static final Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
27 private static final Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
28 private static final Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
29 private static final Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
30 private static final Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
31 private static final Set<String> filer8only = new HashSet<String>();\r
32 \r
33 private static final String[] specialhoblibfunc = {\r
34 "BuildModuleHob",\r
35 "BuildResourceDescriptorHob",\r
36 "BuildFvHob",\r
37 "BuildCpuHob",\r
733d48fe 38 "BuildGuidDataHob",\r
27e0221a 39 "BuildStackHob",\r
40 "BuildBspStoreHob",\r
41 "BuildMemoryAllocationHob"\r
42 };\r
10e4f990 43 private static final String[] peiserviceslibfunc = {\r
44 "InstallPpi",\r
45 "ReInstallPpi",\r
46 "LocatePpi",\r
47 "NotifyPpi",\r
48 "GetBootMode",\r
49 "SetBootMode",\r
50 "GetHobList",\r
51 "CreateHob",\r
52 "FfsFindNextVolume",\r
53 "FfsFindNextFile",\r
54 "FfsFindSectionData",\r
55 "InstallPeiMemory",\r
56 "AllocatePages",\r
57 "AllocatePool",\r
58 "PeiResetSystem"\r
59 };\r
27e0221a 60 //---------------------------------------inner classes---------------------------------------//\r
61 private static class r8tor9 {\r
62 r8tor9(String r8, String r9) {\r
63 r8thing = r8;\r
64 r9thing = r9;\r
65 }\r
66 public String r8thing;\r
67 public String r9thing;\r
68 }\r
69 \r
70 private class IdleLaplace extends Common.Laplace {\r
71 public String operation(String wholeline) {\r
9f98dbb7 72 return replaceLibrary (wholeline, mi.hashmacro);\r
27e0221a 73 }\r
74 \r
75 public boolean recognize(String filename) {\r
76 return filename.contains(".h") || filename.contains(".H") || filename.contains(".uni");\r
77 }\r
2887f99b 78\r
27e0221a 79 public String namechange(String oldname) {\r
80 if (oldname.contains(".H")) {\r
81 return oldname.replaceFirst(".H", ".h");\r
82 } else {\r
83 return oldname;\r
84 }\r
85 }\r
86 }\r
87 private class DxsLaplace extends Common.Laplace {\r
88 public String operation(String wholeline) {\r
89 if (mi.getModuleType().equals("PEIM")) {\r
90 return addincludefile(wholeline, "\\<PeimDepex.h\\>");\r
91 } else {\r
92 return addincludefile(wholeline, "\\<DxeDepex.h\\>");\r
93 }\r
94 }\r
95 \r
96 public boolean recognize(String filename) {\r
97 return filename.contains(".dxs");\r
98 }\r
2887f99b 99\r
27e0221a 100 public String namechange(String oldname) {\r
101 return oldname;\r
102 }\r
103 }\r
104 \r
105 private class CLaplace extends Common.Laplace {\r
106 public String operation(String wholeline) {\r
27e0221a 107 // remove EFI_DRIVER_ENTRY_POINT\r
d47b9900 108 wholeline = wholeline.replaceAll("(EFI_[A-Z]+_ENTRY_POINT\\s*\\(\\s*" + mi.entrypoint + "\\s*\\)\\s*;)", MigrationTool.MIGRATIONCOMMENT + " $1");\r
109 // redefine module entry point for some self-relocated modules\r
110 wholeline = wholeline.replaceAll (mi.entrypoint + "([^{]*?})", "_ModuleEntryPoint" + "$1");\r
06d1ff5b 111 // remove R8 library contractor\r
112 wholeline = wholeline.replaceAll ("(\\b(?:Efi|Dxe)InitializeDriverLib\\b)", MigrationTool.MIGRATIONCOMMENT + " $1");\r
113 // Add Library Class for potential reference of gBS, gRT & gDS.\r
114 if (Common.find (wholeline, "\\bg?BS\\b")) {\r
115 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
116 }\r
117 if (Common.find (wholeline, "\\bg?RT\\b")) {\r
118 mi.hashrequiredr9libs.add ("UefiRuntimeServicesTableLib");\r
119 }\r
120 if (Common.find (wholeline, "\\bgDS\\b")) {\r
121 mi.hashrequiredr9libs.add ("DxeServicesTableLib");\r
122 }\r
9f98dbb7 123\r
124 wholeline = replaceLibrary (wholeline, mi.hashnonlocalfunc);\r
125 wholeline = replaceLibrary (wholeline, mi.hashmacro);\r
27e0221a 126 // Converting macro\r
9f98dbb7 127 wholeline = replaceMacro (wholeline, mi.hashnonlocalmacro);\r
0dc8c589 128\r
27e0221a 129 // Converting guid\r
130 replaceGuid(wholeline, mi.guid, "guid", fileguid);\r
131 replaceGuid(wholeline, mi.ppi, "ppi", fileppi);\r
132 replaceGuid(wholeline, mi.protocol, "protocol", fileprotocol);\r
2887f99b 133\r
27e0221a 134 // Converting Pei\r
27e0221a 135 if (mi.getModuleType().matches("PEIM")) {\r
10e4f990 136 //\r
137 // Try to remove PeiServicesTablePointer;\r
138 // \r
139 wholeline = dropPeiServicesPointer (wholeline);\r
140 //\r
141 // Drop the possible return Status of Hob building function.\r
142 // \r
143 wholeline = drophobLibReturnStatus (wholeline);\r
27e0221a 144 }\r
10e4f990 145 //\r
146 // Expand obsolete R8 macro.\r
147 // \r
148 wholeline = replaceObsoleteMacro (wholeline);\r
0dc8c589 149\r
27e0221a 150 show(filefunc, "function");\r
151 show(filemacro, "macro");\r
152 show(fileguid, "guid");\r
153 show(fileppi, "ppi");\r
154 show(fileprotocol, "protocol");\r
155 if (!filer8only.isEmpty()) {\r
156 MigrationTool.ui.println("Converting r8only : " + filer8only);\r
157 }\r
2887f99b 158\r
27e0221a 159 filefunc.clear();\r
160 filemacro.clear();\r
161 fileguid.clear();\r
162 fileppi.clear();\r
163 fileprotocol.clear();\r
164 filer8only.clear();\r
2887f99b 165\r
27e0221a 166 return wholeline;\r
167 }\r
168 \r
169 public boolean recognize(String filename) {\r
170 return filename.contains(".c") || filename.contains(".C");\r
171 }\r
0dc8c589 172\r
27e0221a 173 public String namechange(String oldname) {\r
174 if (oldname.contains(".C")) {\r
175 return oldname.replaceFirst(".C", ".c");\r
176 } else {\r
177 return oldname;\r
178 }\r
179 }\r
180 }\r
181 //---------------------------------------inner classes---------------------------------------//\r
99b0f0e6 182\r
27e0221a 183 //-------------------------------------process functions-------------------------------------//\r
184 private static final String addincludefile(String wholeline, String hfile) {\r
185 return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n");\r
186 }\r
187 \r
188 private static final void show(Set<r8tor9> hash, String sh) {\r
189 Iterator<r8tor9> it = hash.iterator();\r
190 r8tor9 temp;\r
191 if (!hash.isEmpty()) {\r
192 MigrationTool.ui.print("Converting " + sh + " : ");\r
193 while (it.hasNext()) {\r
194 temp = it.next();\r
195 MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
196 }\r
197 MigrationTool.ui.println("");\r
198 }\r
199 }\r
71874f93 200\r
27e0221a 201 private static final void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
202 Iterator<String> it;\r
203 String r8thing;\r
204 String r9thing;\r
205 it = hash.iterator();\r
206 while (it.hasNext()) {\r
207 r8thing = it.next();\r
208 if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {\r
209 if (!r8thing.equals(r9thing)) {\r
210 if (line.contains(r8thing)) {\r
211 line = line.replaceAll(r8thing, r9thing);\r
212 filehash.add(new r8tor9(r8thing, r9thing));\r
213 }\r
214 }\r
215 }\r
216 }\r
217 }\r
71874f93 218\r
10e4f990 219 private final String dropPeiServicesPointer (String wholeline) {\r
220 String peiServicesTablePointer;\r
221 String peiServicesTableCaller;\r
222 String regPeiServices;\r
223 Pattern ptnPei;\r
224 Matcher mtrPei;\r
225\r
226 peiServicesTablePointer = "\\w(?:\\w|[0-9]|->)*";\r
227 peiServicesTableCaller = "\\(\\*\\*?\\s*(" + peiServicesTablePointer + ")\\s*\\)[.-]>?\\s*";\r
228 for (int i = 0; i < peiserviceslibfunc.length; i++) {\r
229 regPeiServices = peiServicesTableCaller + peiserviceslibfunc[i] + "\\s*\\(\\s*\\1\\s*,(\\t| )*"; \r
230 ptnPei = Pattern.compile (regPeiServices);\r
231 mtrPei = ptnPei.matcher (wholeline);\r
232 if (mtrPei.find()) {\r
233 wholeline = mtrPei.replaceAll("PeiServices" + peiserviceslibfunc[i] + " (");\r
234 mi.hashrequiredr9libs.add("PeiServicesLib");\r
235 }\r
236 }\r
237 regPeiServices = peiServicesTableCaller + "(CopyMem|SetMem)" + "\\s*\\((\\t| )*";\r
238 ptnPei = Pattern.compile (regPeiServices);\r
239 mtrPei = ptnPei.matcher (wholeline);\r
240 if (mtrPei.find()) {\r
241 wholeline = mtrPei.replaceAll("$2 (");\r
242 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
243 }\r
244\r
245 ptnPei = Pattern.compile("#%+(\\s*\\(+\\s*)" + peiServicesTablePointer + "\\s*,\\s*", Pattern.MULTILINE);\r
246 mtrPei = ptnPei.matcher(wholeline);\r
247 while (mtrPei.find()) {\r
248 wholeline = mtrPei.replaceAll("$1");\r
249 }\r
250\r
251 return wholeline;\r
252 }\r
253\r
254 private final String drophobLibReturnStatus (String wholeline) { // or use regex to find pattern "Status = ..."\r
27e0221a 255 Pattern ptnhobstatus;\r
256 Matcher mtrhobstatus;\r
257 String templine = wholeline;\r
258 for (int i = 0; i < specialhoblibfunc.length; i++) {\r
259 ptnhobstatus = Pattern.compile("(Status\\s*=\\s*)?" + specialhoblibfunc[i] + "(.*?\\)\\s*;)", Pattern.DOTALL);\r
260 mtrhobstatus = ptnhobstatus.matcher(templine);\r
261 if (mtrhobstatus.find()) {\r
10e4f990 262 templine = mtrhobstatus.replaceAll(specialhoblibfunc[i] + mtrhobstatus.group(2) + "\n " + \r
263 MigrationTool.MIGRATIONCOMMENT + "R9 Hob-building library functions will assert if build failure.\n Status = EFI_SUCCESS;");\r
27e0221a 264 }\r
265 }\r
266 return templine;\r
267 }\r
9f98dbb7 268\r
269 private final String replaceMacro (String wholeline, Set<String> symbolSet) {\r
270 String r8thing;\r
271 String r9thing;\r
272 Iterator<String> it;\r
273\r
274 it = symbolSet.iterator();\r
275 while (it.hasNext()) { //macros are all assumed MdePkg currently\r
276 r8thing = it.next();\r
9f98dbb7 277 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); \r
278 if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {\r
279 if (wholeline.contains(r8thing)) {\r
280 wholeline = wholeline.replaceAll(r8thing, r9thing);\r
281 filemacro.add(new r8tor9(r8thing, r9thing));\r
282 }\r
283 }\r
284 }\r
285 return wholeline;\r
286 }\r
287\r
288 private final String replaceLibrary (String wholeline, Set<String> symbolSet) {\r
289 boolean addr8 = false;\r
290 // start replacing names\r
291 String r8thing;\r
292 String r9thing;\r
293 Iterator<String> it;\r
294 // Converting non-locla function\r
295 it = symbolSet.iterator();\r
296 while (it.hasNext()) {\r
297 r8thing = it.next();\r
298 mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here\r
299 \r
300 r8tor9 temp;\r
301 if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {\r
302 if (!r8thing.equals(r9thing)) {\r
303 if (wholeline.contains(r8thing)) {\r
304 wholeline = wholeline.replaceAll(r8thing, r9thing);\r
305 filefunc.add(new r8tor9(r8thing, r9thing));\r
306 Iterator<r8tor9> rt = filefunc.iterator();\r
307 while (rt.hasNext()) {\r
308 temp = rt.next();\r
309 if (MigrationTool.db.r8only.contains(temp.r8thing)) {\r
310 filer8only.add(r8thing);\r
311 mi.hashr8only.add(r8thing);\r
312 addr8 = true;\r
313 }\r
314 }\r
315 }\r
316 }\r
317 }\r
318 } //is any of the guids changed?\r
319 if (addr8 == true) {\r
320 wholeline = addincludefile(wholeline, "\"R8Lib.h\"");\r
321 }\r
322 return wholeline;\r
323 }\r
324\r
10e4f990 325 private final String replaceObsoleteMacro (String wholeline) {\r
326 Matcher mtrmac;\r
327 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline);\r
328 if (mtrmac.find()) {\r
329 wholeline = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
330 }\r
331 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline);\r
332 if (mtrmac.find()) {\r
333 wholeline = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
334 }\r
335 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline);\r
336 if (mtrmac.find()) {\r
337 wholeline = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
338 }\r
339 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline);\r
340 if (mtrmac.find()) {\r
341 wholeline = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
342 }\r
343 if (wholeline.contains("EFI_UINTN_ALIGN_MASK")) {\r
344 wholeline = wholeline.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
345 }\r
346 return wholeline;\r
347 }\r
348\r
27e0221a 349 private final void addr8only() throws Exception {\r
350 String paragraph = null;\r
351 String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c");\r
352 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));\r
353 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));\r
354 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?(.*?R8_(\\w*).*?)////~", Pattern.DOTALL);\r
355 Matcher mtrr8only = ptnr8only.matcher(line);\r
356 Matcher mtrr8onlyhead;\r
357 \r
358 //add head comment\r
359 Matcher mtrr8onlyheadcomment = Critic.PTN_NEW_HEAD_COMMENT.matcher(line);\r
360 if (mtrr8onlyheadcomment.find()) {\r
361 outfile1.append(mtrr8onlyheadcomment.group() + "\n\n");\r
362 outfile2.append(mtrr8onlyheadcomment.group() + "\n\n");\r
363 }\r
364 \r
365 //add functions body\r
366 while (mtrr8only.find()) {\r
367 if (mi.hashr8only.contains(mtrr8only.group(3))) {\r
368 paragraph = mtrr8only.group(2);\r
369 outfile1.append(paragraph + "\n\n");\r
370 if (mtrr8only.group(1).length() != 0) {\r
371 mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
372 }\r
373 //generate R8lib.h\r
374 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
375 paragraph = mtrr8onlyhead.replaceAll(";");\r
376 }\r
377 outfile2.append(paragraph + "\n\n");\r
378 }\r
379 }\r
380 outfile1.flush();\r
381 outfile1.close();\r
382 outfile2.flush();\r
383 outfile2.close();\r
384 \r
385 mi.localmodulesources.add("R8Lib.h");\r
386 mi.localmodulesources.add("R8Lib.c");\r
387 }\r
388 //-------------------------------------process functions-------------------------------------//\r
389 \r
390 //-----------------------------------ForDoAll-----------------------------------//\r
391 public void run(String filepath) throws Exception {\r
df87de9b 392 String inname = filepath.replace(mi.temppath + File.separator, "");\r
393 String tempinpath = mi.temppath + File.separator;\r
27e0221a 394 String tempoutpath = MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator;\r
16b7eeef 395\r
27e0221a 396 Iterator<Common.Laplace> itLaplace = Laplaces.iterator();\r
397 while (itLaplace.hasNext()) {\r
398 Common.Laplace lap = itLaplace.next();\r
399 if (lap.recognize(inname)) {\r
400 MigrationTool.ui.println("\nHandling file: " + inname);\r
401 lap.transform(tempinpath + inname, tempoutpath + lap.namechange(inname));\r
402 }\r
403 }\r
404 }\r
405 \r
406 public boolean filter(File dir) {\r
407 return true;\r
408 }\r
409 //-----------------------------------ForDoAll-----------------------------------//\r
410 \r
411 private final void setModuleInfo(ModuleInfo moduleinfo) {\r
412 mi = moduleinfo;\r
413 }\r
414 \r
415 private final void start() throws Exception {\r
416 Laplaces.add(new DxsLaplace());\r
417 Laplaces.add(new CLaplace());\r
418 Laplaces.add(new IdleLaplace());\r
419 \r
df87de9b 420 Common.toDoAll(mi.temppath, this, Common.FILE);\r
27e0221a 421 \r
422 if (!mi.hashr8only.isEmpty()) {\r
423 addr8only();\r
424 }\r
425 \r
426 Laplaces.clear();\r
427 }\r
428 \r
429 public static final void fireAt(ModuleInfo moduleinfo) throws Exception {\r
430 SFReplacer.setModuleInfo(moduleinfo);\r
431 SFReplacer.start();\r
432 }\r
0dc8c589 433}\r