]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/MigrationTools/org/tianocore/migration/SourceFileReplacer.java
1.Support to drop Pei Services Table Pointer at best.
[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
107 boolean addr8 = false;\r
27e0221a 108 // remove EFI_DRIVER_ENTRY_POINT\r
d47b9900 109 wholeline = wholeline.replaceAll("(EFI_[A-Z]+_ENTRY_POINT\\s*\\(\\s*" + mi.entrypoint + "\\s*\\)\\s*;)", MigrationTool.MIGRATIONCOMMENT + " $1");\r
110 // redefine module entry point for some self-relocated modules\r
111 wholeline = wholeline.replaceAll (mi.entrypoint + "([^{]*?})", "_ModuleEntryPoint" + "$1");\r
06d1ff5b 112 // remove R8 library contractor\r
113 wholeline = wholeline.replaceAll ("(\\b(?:Efi|Dxe)InitializeDriverLib\\b)", MigrationTool.MIGRATIONCOMMENT + " $1");\r
114 // Add Library Class for potential reference of gBS, gRT & gDS.\r
115 if (Common.find (wholeline, "\\bg?BS\\b")) {\r
116 mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
117 }\r
118 if (Common.find (wholeline, "\\bg?RT\\b")) {\r
119 mi.hashrequiredr9libs.add ("UefiRuntimeServicesTableLib");\r
120 }\r
121 if (Common.find (wholeline, "\\bgDS\\b")) {\r
122 mi.hashrequiredr9libs.add ("DxeServicesTableLib");\r
123 }\r
9f98dbb7 124\r
125 wholeline = replaceLibrary (wholeline, mi.hashnonlocalfunc);\r
126 wholeline = replaceLibrary (wholeline, mi.hashmacro);\r
27e0221a 127 // Converting macro\r
9f98dbb7 128 wholeline = replaceMacro (wholeline, mi.hashnonlocalmacro);\r
0dc8c589 129\r
27e0221a 130 // Converting guid\r
131 replaceGuid(wholeline, mi.guid, "guid", fileguid);\r
132 replaceGuid(wholeline, mi.ppi, "ppi", fileppi);\r
133 replaceGuid(wholeline, mi.protocol, "protocol", fileprotocol);\r
2887f99b 134\r
27e0221a 135 // Converting Pei\r
27e0221a 136 if (mi.getModuleType().matches("PEIM")) {\r
10e4f990 137 //\r
138 // Try to remove PeiServicesTablePointer;\r
139 // \r
140 wholeline = dropPeiServicesPointer (wholeline);\r
141 //\r
142 // Drop the possible return Status of Hob building function.\r
143 // \r
144 wholeline = drophobLibReturnStatus (wholeline);\r
27e0221a 145 }\r
10e4f990 146 //\r
147 // Expand obsolete R8 macro.\r
148 // \r
149 wholeline = replaceObsoleteMacro (wholeline);\r
0dc8c589 150\r
27e0221a 151 show(filefunc, "function");\r
152 show(filemacro, "macro");\r
153 show(fileguid, "guid");\r
154 show(fileppi, "ppi");\r
155 show(fileprotocol, "protocol");\r
156 if (!filer8only.isEmpty()) {\r
157 MigrationTool.ui.println("Converting r8only : " + filer8only);\r
158 }\r
2887f99b 159\r
27e0221a 160 filefunc.clear();\r
161 filemacro.clear();\r
162 fileguid.clear();\r
163 fileppi.clear();\r
164 fileprotocol.clear();\r
165 filer8only.clear();\r
2887f99b 166\r
27e0221a 167 return wholeline;\r
168 }\r
169 \r
170 public boolean recognize(String filename) {\r
171 return filename.contains(".c") || filename.contains(".C");\r
172 }\r
0dc8c589 173\r
27e0221a 174 public String namechange(String oldname) {\r
175 if (oldname.contains(".C")) {\r
176 return oldname.replaceFirst(".C", ".c");\r
177 } else {\r
178 return oldname;\r
179 }\r
180 }\r
181 }\r
182 //---------------------------------------inner classes---------------------------------------//\r
99b0f0e6 183\r
27e0221a 184 //-------------------------------------process functions-------------------------------------//\r
185 private static final String addincludefile(String wholeline, String hfile) {\r
186 return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n");\r
187 }\r
188 \r
189 private static final void show(Set<r8tor9> hash, String sh) {\r
190 Iterator<r8tor9> it = hash.iterator();\r
191 r8tor9 temp;\r
192 if (!hash.isEmpty()) {\r
193 MigrationTool.ui.print("Converting " + sh + " : ");\r
194 while (it.hasNext()) {\r
195 temp = it.next();\r
196 MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
197 }\r
198 MigrationTool.ui.println("");\r
199 }\r
200 }\r
71874f93 201\r
27e0221a 202 private static final void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
203 Iterator<String> it;\r
204 String r8thing;\r
205 String r9thing;\r
206 it = hash.iterator();\r
207 while (it.hasNext()) {\r
208 r8thing = it.next();\r
209 if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {\r
210 if (!r8thing.equals(r9thing)) {\r
211 if (line.contains(r8thing)) {\r
212 line = line.replaceAll(r8thing, r9thing);\r
213 filehash.add(new r8tor9(r8thing, r9thing));\r
214 }\r
215 }\r
216 }\r
217 }\r
218 }\r
71874f93 219\r
10e4f990 220 private final String dropPeiServicesPointer (String wholeline) {\r
221 String peiServicesTablePointer;\r
222 String peiServicesTableCaller;\r
223 String regPeiServices;\r
224 Pattern ptnPei;\r
225 Matcher mtrPei;\r
226\r
227 peiServicesTablePointer = "\\w(?:\\w|[0-9]|->)*";\r
228 peiServicesTableCaller = "\\(\\*\\*?\\s*(" + peiServicesTablePointer + ")\\s*\\)[.-]>?\\s*";\r
229 for (int i = 0; i < peiserviceslibfunc.length; i++) {\r
230 regPeiServices = peiServicesTableCaller + peiserviceslibfunc[i] + "\\s*\\(\\s*\\1\\s*,(\\t| )*"; \r
231 ptnPei = Pattern.compile (regPeiServices);\r
232 mtrPei = ptnPei.matcher (wholeline);\r
233 if (mtrPei.find()) {\r
234 wholeline = mtrPei.replaceAll("PeiServices" + peiserviceslibfunc[i] + " (");\r
235 mi.hashrequiredr9libs.add("PeiServicesLib");\r
236 }\r
237 }\r
238 regPeiServices = peiServicesTableCaller + "(CopyMem|SetMem)" + "\\s*\\((\\t| )*";\r
239 ptnPei = Pattern.compile (regPeiServices);\r
240 mtrPei = ptnPei.matcher (wholeline);\r
241 if (mtrPei.find()) {\r
242 wholeline = mtrPei.replaceAll("$2 (");\r
243 mi.hashrequiredr9libs.add("BaseMemoryLib");\r
244 }\r
245\r
246 ptnPei = Pattern.compile("#%+(\\s*\\(+\\s*)" + peiServicesTablePointer + "\\s*,\\s*", Pattern.MULTILINE);\r
247 mtrPei = ptnPei.matcher(wholeline);\r
248 while (mtrPei.find()) {\r
249 wholeline = mtrPei.replaceAll("$1");\r
250 }\r
251\r
252 return wholeline;\r
253 }\r
254\r
255 private final String drophobLibReturnStatus (String wholeline) { // or use regex to find pattern "Status = ..."\r
27e0221a 256 Pattern ptnhobstatus;\r
257 Matcher mtrhobstatus;\r
258 String templine = wholeline;\r
259 for (int i = 0; i < specialhoblibfunc.length; i++) {\r
260 ptnhobstatus = Pattern.compile("(Status\\s*=\\s*)?" + specialhoblibfunc[i] + "(.*?\\)\\s*;)", Pattern.DOTALL);\r
261 mtrhobstatus = ptnhobstatus.matcher(templine);\r
262 if (mtrhobstatus.find()) {\r
10e4f990 263 templine = mtrhobstatus.replaceAll(specialhoblibfunc[i] + mtrhobstatus.group(2) + "\n " + \r
264 MigrationTool.MIGRATIONCOMMENT + "R9 Hob-building library functions will assert if build failure.\n Status = EFI_SUCCESS;");\r
27e0221a 265 }\r
266 }\r
267 return templine;\r
268 }\r
9f98dbb7 269\r
270 private final String replaceMacro (String wholeline, Set<String> symbolSet) {\r
271 String r8thing;\r
272 String r9thing;\r
273 Iterator<String> it;\r
274\r
275 it = symbolSet.iterator();\r
276 while (it.hasNext()) { //macros are all assumed MdePkg currently\r
277 r8thing = it.next();\r
9f98dbb7 278 //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); \r
279 if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {\r
280 if (wholeline.contains(r8thing)) {\r
281 wholeline = wholeline.replaceAll(r8thing, r9thing);\r
282 filemacro.add(new r8tor9(r8thing, r9thing));\r
283 }\r
284 }\r
285 }\r
286 return wholeline;\r
287 }\r
288\r
289 private final String replaceLibrary (String wholeline, Set<String> symbolSet) {\r
290 boolean addr8 = false;\r
291 // start replacing names\r
292 String r8thing;\r
293 String r9thing;\r
294 Iterator<String> it;\r
295 // Converting non-locla function\r
296 it = symbolSet.iterator();\r
297 while (it.hasNext()) {\r
298 r8thing = it.next();\r
299 mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here\r
300 \r
301 r8tor9 temp;\r
302 if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {\r
303 if (!r8thing.equals(r9thing)) {\r
304 if (wholeline.contains(r8thing)) {\r
305 wholeline = wholeline.replaceAll(r8thing, r9thing);\r
306 filefunc.add(new r8tor9(r8thing, r9thing));\r
307 Iterator<r8tor9> rt = filefunc.iterator();\r
308 while (rt.hasNext()) {\r
309 temp = rt.next();\r
310 if (MigrationTool.db.r8only.contains(temp.r8thing)) {\r
311 filer8only.add(r8thing);\r
312 mi.hashr8only.add(r8thing);\r
313 addr8 = true;\r
314 }\r
315 }\r
316 }\r
317 }\r
318 }\r
319 } //is any of the guids changed?\r
320 if (addr8 == true) {\r
321 wholeline = addincludefile(wholeline, "\"R8Lib.h\"");\r
322 }\r
323 return wholeline;\r
324 }\r
325\r
10e4f990 326 private final String replaceObsoleteMacro (String wholeline) {\r
327 Matcher mtrmac;\r
328 mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline);\r
329 if (mtrmac.find()) {\r
330 wholeline = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
331 }\r
332 mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline);\r
333 if (mtrmac.find()) {\r
334 wholeline = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
335 }\r
336 mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline);\r
337 if (mtrmac.find()) {\r
338 wholeline = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
339 }\r
340 mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline);\r
341 if (mtrmac.find()) {\r
342 wholeline = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
343 }\r
344 if (wholeline.contains("EFI_UINTN_ALIGN_MASK")) {\r
345 wholeline = wholeline.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
346 }\r
347 return wholeline;\r
348 }\r
349\r
27e0221a 350 private final void addr8only() throws Exception {\r
351 String paragraph = null;\r
352 String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c");\r
353 PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));\r
354 PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));\r
355 Pattern ptnr8only = Pattern.compile("////#?(\\w*)?(.*?R8_(\\w*).*?)////~", Pattern.DOTALL);\r
356 Matcher mtrr8only = ptnr8only.matcher(line);\r
357 Matcher mtrr8onlyhead;\r
358 \r
359 //add head comment\r
360 Matcher mtrr8onlyheadcomment = Critic.PTN_NEW_HEAD_COMMENT.matcher(line);\r
361 if (mtrr8onlyheadcomment.find()) {\r
362 outfile1.append(mtrr8onlyheadcomment.group() + "\n\n");\r
363 outfile2.append(mtrr8onlyheadcomment.group() + "\n\n");\r
364 }\r
365 \r
366 //add functions body\r
367 while (mtrr8only.find()) {\r
368 if (mi.hashr8only.contains(mtrr8only.group(3))) {\r
369 paragraph = mtrr8only.group(2);\r
370 outfile1.append(paragraph + "\n\n");\r
371 if (mtrr8only.group(1).length() != 0) {\r
372 mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
373 }\r
374 //generate R8lib.h\r
375 while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
376 paragraph = mtrr8onlyhead.replaceAll(";");\r
377 }\r
378 outfile2.append(paragraph + "\n\n");\r
379 }\r
380 }\r
381 outfile1.flush();\r
382 outfile1.close();\r
383 outfile2.flush();\r
384 outfile2.close();\r
385 \r
386 mi.localmodulesources.add("R8Lib.h");\r
387 mi.localmodulesources.add("R8Lib.c");\r
388 }\r
389 //-------------------------------------process functions-------------------------------------//\r
390 \r
391 //-----------------------------------ForDoAll-----------------------------------//\r
392 public void run(String filepath) throws Exception {\r
393 String inname = filepath.replace(mi.modulepath + File.separator, "");\r
394 String tempinpath = mi.modulepath + File.separator + "temp" + File.separator;\r
395 String tempoutpath = MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator;\r
16b7eeef 396\r
27e0221a 397 Iterator<Common.Laplace> itLaplace = Laplaces.iterator();\r
398 while (itLaplace.hasNext()) {\r
399 Common.Laplace lap = itLaplace.next();\r
400 if (lap.recognize(inname)) {\r
401 MigrationTool.ui.println("\nHandling file: " + inname);\r
402 lap.transform(tempinpath + inname, tempoutpath + lap.namechange(inname));\r
403 }\r
404 }\r
405 }\r
406 \r
407 public boolean filter(File dir) {\r
408 return true;\r
409 }\r
410 //-----------------------------------ForDoAll-----------------------------------//\r
411 \r
412 private final void setModuleInfo(ModuleInfo moduleinfo) {\r
413 mi = moduleinfo;\r
414 }\r
415 \r
416 private final void start() throws Exception {\r
417 Laplaces.add(new DxsLaplace());\r
418 Laplaces.add(new CLaplace());\r
419 Laplaces.add(new IdleLaplace());\r
420 \r
421 Common.toDoAll(mi.localmodulesources, this);\r
422 \r
423 if (!mi.hashr8only.isEmpty()) {\r
424 addr8only();\r
425 }\r
426 \r
427 Laplaces.clear();\r
428 }\r
429 \r
430 public static final void fireAt(ModuleInfo moduleinfo) throws Exception {\r
431 SFReplacer.setModuleInfo(moduleinfo);\r
432 SFReplacer.start();\r
433 }\r
0dc8c589 434}\r