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