import java.util.regex.Matcher;\r
import java.util.regex.Pattern;\r
\r
-public class SourceFileReplacer {\r
- SourceFileReplacer(String path, ModuleInfo moduleinfo, Database database, UI fp) {\r
- modulepath = path;\r
- mi = moduleinfo;\r
- db = database;\r
- ui = fp;\r
- }\r
- private String modulepath;\r
- private ModuleInfo mi;\r
- private Database db;\r
- private UI ui;\r
- private boolean showdetails = false;\r
- \r
- private class r8tor9 {\r
- r8tor9(String r8, String r9) {\r
- r8thing = r8;\r
- r9thing = r9;\r
- }\r
- public String r8thing;\r
- public String r9thing;\r
- }\r
- \r
- // these sets are used only for printing log of the changes in current file\r
- private Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
- private Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
- private Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
- private Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
- private Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
- private Set<String> filer8only = new HashSet<String>();\r
- \r
- private String r8only = "EfiLibInstallDriverBinding " +\r
- "EfiLibInstallAllDriverProtocols " +\r
- "EfiLibCompareLanguage " +\r
- "BufToHexString " +\r
- "EfiStrTrim " + //is the r8only lib going to be enlarged???? Caution !!!!\r
- "EfiValueToHexStr " +\r
- "HexStringToBuf " +\r
- "IsHexDigit " +\r
- "NibbleToHexChar " +\r
- "GetHob " +\r
- "GetHobListSize " +\r
- "GetHobVersion " +\r
- "GetHobBootMode " +\r
- "GetCpuHobInfo " +\r
- "GetDxeCoreHobInfo " +\r
- "GetNextFirmwareVolumeHob " +\r
- "GetNextGuidHob " +\r
- "GetPalEntryHobInfo " +\r
- "GetIoPortSpaceAddressHobInfo ";\r
- \r
- public void flush() throws Exception {\r
- PrintWriter outfile;\r
- String outname = null;\r
- String inname = null;\r
- if (ui.yesOrNo("Changes will be made to the Source Code. View details?")) {\r
- showdetails = true;\r
- }\r
- \r
- Iterator<String> di = mi.localmodulesources.iterator();\r
- while (di.hasNext()) {\r
- inname = di.next();\r
- if (inname.contains(".c") || inname.contains(".C")) {\r
- if (inname.contains(".C")) {\r
- outname = inname.replaceFirst(".C", ".c");\r
- } else {\r
- outname = inname;\r
- }\r
- ui.println("\nModifying file: " + inname);\r
- mi.ensureDir(modulepath + File.separator + "result" + File.separator + outname);\r
- outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + outname)));\r
- outfile.append(sourcefilereplace(modulepath + File.separator + "temp" + File.separator + inname));\r
- outfile.flush();\r
- outfile.close();\r
- } else if (inname.contains(".h") || inname.contains(".H") || inname.contains(".dxs") || inname.contains(".uni")) {\r
- if (inname.contains(".H")) {\r
- outname = inname.replaceFirst(".H", ".h");\r
- } else {\r
- outname = inname;\r
- }\r
- ui.println("\nCopying file: " + inname);\r
- mi.ensureDir(modulepath + File.separator + "result" + File.separator + outname);\r
- outfile = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + outname)));\r
- outfile.append(sourcefiletostring(modulepath + File.separator + "temp" + File.separator + inname));\r
- outfile.flush();\r
- outfile.close();\r
- }\r
- }\r
+public final class SourceFileReplacer implements Common.ForDoAll {\r
+ private static final SourceFileReplacer SFReplacer = new SourceFileReplacer();\r
+ private ModuleInfo mi;\r
+ private static final Set<Common.Laplace> Laplaces = new HashSet<Common.Laplace>();\r
\r
- if (!mi.hashr8only.isEmpty()) {\r
- addr8only();\r
- }\r
- }\r
- \r
- private void addr8only() throws Exception {\r
- String paragraph = null;\r
- String line = sourcefiletostring(Database.defaultpath + File.separator + "R8Lib.c");\r
- mi.ensureDir(modulepath + File.separator + "result" + File.separator + "R8Lib.c");\r
- PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.c")));\r
- PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(modulepath + File.separator + "result" + File.separator + "R8Lib.h")));\r
- Pattern ptnr8only = Pattern.compile("////#?(\\w*)?.*?R8_(\\w*).*?////~", Pattern.DOTALL);\r
- Matcher mtrr8only = ptnr8only.matcher(line);\r
- Matcher mtrr8onlyhead;\r
- while (mtrr8only.find()) {\r
- if (mi.hashr8only.contains(mtrr8only.group(2))) {\r
- paragraph = mtrr8only.group();\r
- outfile1.append(paragraph + "\n\n");\r
- if (mtrr8only.group(1).length() != 0) {\r
- mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
- }\r
- //generate R8lib.h\r
- while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
- paragraph = mtrr8onlyhead.replaceAll(";");\r
- }\r
- outfile2.append(paragraph + "\n\n");\r
- }\r
- }\r
- outfile1.flush();\r
- outfile1.close();\r
- outfile2.flush();\r
- outfile2.close();\r
- \r
- mi.localmodulesources.add("R8Lib.h");\r
- mi.localmodulesources.add("R8Lib.c");\r
- }\r
- \r
- private String sourcefiletostring(String filename) throws Exception {\r
- BufferedReader rd = new BufferedReader(new FileReader(filename));\r
- StringBuffer wholefile = new StringBuffer();\r
- String line;\r
- while ((line = rd.readLine()) != null) {\r
- wholefile.append(line + "\n");\r
- }\r
- return wholefile.toString();\r
- }\r
- \r
- // Caution : if there is @ in file , it will be replaced with \n , so is you use Doxygen ... God Bless you!\r
- private String sourcefilereplace(String filename) throws Exception {\r
- BufferedReader rd = new BufferedReader(new FileReader(filename));\r
- StringBuffer wholefile = new StringBuffer();\r
- String line;\r
- String r8thing;\r
- String r9thing;\r
- r8tor9 temp;\r
- boolean addr8 = false;\r
+ // these sets are used only for printing log of the changes in current file\r
+ private static final Set<r8tor9> filefunc = new HashSet<r8tor9>();\r
+ private static final Set<r8tor9> filemacro = new HashSet<r8tor9>();\r
+ private static final Set<r8tor9> fileguid = new HashSet<r8tor9>();\r
+ private static final Set<r8tor9> fileppi = new HashSet<r8tor9>();\r
+ private static final Set<r8tor9> fileprotocol = new HashSet<r8tor9>();\r
+ private static final Set<String> filer8only = new HashSet<String>();\r
+ \r
+ private static final String[] specialhoblibfunc = {\r
+ "BuildModuleHob",\r
+ "BuildResourceDescriptorHob",\r
+ "BuildFvHob",\r
+ "BuildCpuHob",\r
+ "BuildGuidDataHob",\r
+ "BuildStackHob",\r
+ "BuildBspStoreHob",\r
+ "BuildMemoryAllocationHob"\r
+ };\r
+ private static final String[] peiserviceslibfunc = {\r
+ "InstallPpi",\r
+ "ReInstallPpi",\r
+ "LocatePpi",\r
+ "NotifyPpi",\r
+ "GetBootMode",\r
+ "SetBootMode",\r
+ "GetHobList",\r
+ "CreateHob",\r
+ "FfsFindNextVolume",\r
+ "FfsFindNextFile",\r
+ "FfsFindSectionData",\r
+ "InstallPeiMemory",\r
+ "AllocatePages",\r
+ "AllocatePool",\r
+ "PeiResetSystem"\r
+ };\r
+ //---------------------------------------inner classes---------------------------------------//\r
+ private static class r8tor9 {\r
+ r8tor9(String r8, String r9) {\r
+ r8thing = r8;\r
+ r9thing = r9;\r
+ }\r
+ public String r8thing;\r
+ public String r9thing;\r
+ }\r
+ \r
+ private class IdleLaplace extends Common.Laplace {\r
+ public String operation(String wholeline) {\r
+ return replaceLibrary (wholeline, mi.hashmacro);\r
+ }\r
+ \r
+ public boolean recognize(String filename) {\r
+ return filename.contains(".h") || filename.contains(".H") || filename.contains(".uni");\r
+ }\r
\r
- Pattern pat = Pattern.compile("g?(BS|RT)(\\s*->\\s*)([a-zA-Z_]\\w*)", Pattern.MULTILINE); // ! only two level () bracket allowed !\r
- //Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*[#$]*)(\\s*\\(([^\\(\\)]*(\\([^\\(\\)]*\\))?[^\\(\\)]*)*\\))", Pattern.MULTILINE);\r
+ public String namechange(String oldname) {\r
+ if (oldname.contains(".H")) {\r
+ return oldname.replaceFirst(".H", ".h");\r
+ } else {\r
+ return oldname;\r
+ }\r
+ }\r
+ }\r
+ private class DxsLaplace extends Common.Laplace {\r
+ public String operation(String wholeline) {\r
+ if (mi.getModuleType().equals("PEIM")) {\r
+ return addincludefile(wholeline, "\\<PeimDepex.h\\>");\r
+ } else {\r
+ return addincludefile(wholeline, "\\<DxeDepex.h\\>");\r
+ }\r
+ }\r
+ \r
+ public boolean recognize(String filename) {\r
+ return filename.contains(".dxs");\r
+ }\r
\r
- while ((line = rd.readLine()) != null) {\r
- wholefile.append(line + "\n");\r
- }\r
- line = wholefile.toString();\r
- \r
- // replace BS -> gBS , RT -> gRT\r
- Matcher mat = pat.matcher(line);\r
- if (mat.find()) { // add a library here\r
- ui.println("Converting all BS->gBS, RT->gRT");\r
- line = mat.replaceAll("g$1$2$3"); //unknown correctiveness\r
- }\r
- mat.reset();\r
- while (mat.find()) {\r
- if (mat.group(1).matches("BS")) {\r
- mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
- }\r
- if (mat.group(1).matches("RT")) {\r
- mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib");\r
- }\r
- }\r
- /*\r
- // remove EFI_DRIVER_ENTRY_POINT\r
- Pattern patentrypoint = Pattern.compile("EFI_DRIVER_ENTRY_POINT[^\\}]*\\}");\r
- Matcher matentrypoint = patentrypoint.matcher(line);\r
- if (matentrypoint.find()) {\r
- ui.println("Deleting Entry_Point");\r
- line = matentrypoint.replaceAll("");\r
- }\r
- */\r
- // start replacing names\r
- Iterator<String> it;\r
- // Converting non-locla function\r
- it = mi.hashnonlocalfunc.iterator();\r
- while (it.hasNext()) {\r
- r8thing = it.next();\r
- if (r8thing.matches("EfiInitializeDriverLib")) { //s\r
- mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //p\r
- mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //e\r
- } else if (r8thing.matches("DxeInitializeDriverLib")) { //c\r
- mi.hashrequiredr9libs.add("UefiBootServicesTableLib"); //i\r
- mi.hashrequiredr9libs.add("UefiRuntimeServicesTableLib"); //a\r
- mi.hashrequiredr9libs.add("DxeServicesTableLib"); //l\r
- } else { //\r
- mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); // add a library here\r
- }\r
- \r
- if ((r9thing = db.getR9Func(r8thing)) != null) {\r
- if (!r8thing.equals(r9thing)) {\r
- if (line.contains(r8thing)) {\r
- line = line.replaceAll(r8thing, r9thing);\r
- filefunc.add(new r8tor9(r8thing, r9thing));\r
- Iterator<r8tor9> rt = filefunc.iterator();\r
- while (rt.hasNext()) {\r
- temp = rt.next();\r
- if (r8only.contains(temp.r8thing)) {\r
- filer8only.add(r8thing);\r
- mi.hashr8only.add(r8thing);\r
- addr8 = true;\r
- }\r
- }\r
- }\r
- }\r
- }\r
- } //is any of the guids changed?\r
- if (addr8 == true) {\r
- line = line.replaceFirst("\\*/\n", "\\*/\n#include \"R8Lib.h\"\n");\r
- }\r
- \r
- // Converting macro\r
- it = mi.hashnonlocalmacro.iterator();\r
- while (it.hasNext()) { //macros are all assumed MdePkg currently\r
- r8thing = it.next();\r
- //mi.hashrequiredr9libs.add(db.getR9Lib(r8thing)); \r
- if ((r9thing = db.getR9Macro(r8thing)) != null) {\r
- if (line.contains(r8thing)) {\r
- line = line.replaceAll(r8thing, r9thing);\r
- filemacro.add(new r8tor9(r8thing, r9thing));\r
- }\r
- }\r
- }\r
+ public String namechange(String oldname) {\r
+ return oldname;\r
+ }\r
+ }\r
+ \r
+ private class CLaplace extends Common.Laplace {\r
+ public String operation(String wholeline) {\r
+ // remove EFI_DRIVER_ENTRY_POINT\r
+ wholeline = wholeline.replaceAll("(EFI_[A-Z]+_ENTRY_POINT\\s*\\(\\s*" + mi.entrypoint + "\\s*\\)\\s*;)", MigrationTool.MIGRATIONCOMMENT + " $1");\r
+ // redefine module entry point for some self-relocated modules\r
+ wholeline = wholeline.replaceAll (mi.entrypoint + "([^{]*?})", "_ModuleEntryPoint" + "$1");\r
+ // remove R8 library contractor\r
+ wholeline = wholeline.replaceAll ("(\\b(?:Efi|Dxe)InitializeDriverLib\\b)", MigrationTool.MIGRATIONCOMMENT + " $1");\r
+ // Add Library Class for potential reference of gBS, gRT & gDS.\r
+ if (Common.find (wholeline, "\\bg?BS\\b")) {\r
+ mi.hashrequiredr9libs.add("UefiBootServicesTableLib");\r
+ }\r
+ if (Common.find (wholeline, "\\bg?RT\\b")) {\r
+ mi.hashrequiredr9libs.add ("UefiRuntimeServicesTableLib");\r
+ }\r
+ if (Common.find (wholeline, "\\bgDS\\b")) {\r
+ mi.hashrequiredr9libs.add ("DxeServicesTableLib");\r
+ }\r
\r
- // Converting guid\r
- replaceGuid(line, mi.guid, "guid", fileguid);\r
- replaceGuid(line, mi.ppi, "ppi", fileppi);\r
- replaceGuid(line, mi.protocol, "protocol", fileprotocol);\r
+ wholeline = replaceLibrary (wholeline, mi.hashnonlocalfunc);\r
+ wholeline = replaceLibrary (wholeline, mi.hashmacro);\r
+ // Converting macro\r
+ wholeline = replaceMacro (wholeline, mi.hashnonlocalmacro);\r
\r
- // Converting Pei\r
- // First , find all (**PeiServices)-> or (*PeiServices). with arg "PeiServices" , change name and add #%\r
- Pattern ptnpei = Pattern.compile("\\(\\*\\*?PeiServices\\)[.-][>]?\\s*(\\w*)(\\s*\\(\\s*PeiServices\\s*,\\s*)", Pattern.MULTILINE);\r
- if (mi.moduletype.contains("PEIM")) {\r
- Matcher mtrpei = ptnpei.matcher(line);\r
- while (mtrpei.find()) { // ! add a library here !\r
- line = mtrpei.replaceAll("PeiServices$1#%$2");\r
- mi.hashrequiredr9libs.add("PeiServicesLib");\r
- }\r
- mtrpei.reset();\r
- if (line.contains("PeiServicesCopyMem")) {\r
- line = line.replaceAll("PeiServicesCopyMem#%", "CopyMem");\r
- mi.hashrequiredr9libs.add("BaseMemoryLib");\r
- }\r
- if (line.contains("PeiServicesSetMem")) {\r
- line = line.replaceAll("PeiServicesSetMem#%", "SetMem");\r
- mi.hashrequiredr9libs.add("BaseMemoryLib");\r
- }\r
+ // Converting guid\r
+ replaceGuid(wholeline, mi.guid, "guid", fileguid);\r
+ replaceGuid(wholeline, mi.ppi, "ppi", fileppi);\r
+ replaceGuid(wholeline, mi.protocol, "protocol", fileprotocol);\r
\r
- // Second , find all #% to drop the arg "PeiServices"\r
- Pattern ptnpeiarg = Pattern.compile("#%+(\\s*\\(+\\s*)PeiServices\\s*,\\s*", Pattern.MULTILINE);\r
- Matcher mtrpeiarg = ptnpeiarg.matcher(line);\r
- while (mtrpeiarg.find()) {\r
- line = mtrpeiarg.replaceAll("$1");\r
- }\r
- }\r
- \r
- Matcher mtrmac;\r
- mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(line);\r
- if (mtrmac.find()) {\r
- line = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
- }\r
- mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(line);\r
- if (mtrmac.find()) {\r
- line = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
- }\r
- mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(line);\r
- if (mtrmac.find()) {\r
- line = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
- }\r
- mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(line);\r
- if (mtrmac.find()) {\r
- line = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
- }\r
- if (line.contains("EFI_UINTN_ALIGN_MASK")) {\r
- line = line.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
- }\r
+ // Converting Pei\r
+ if (mi.getModuleType().matches("PEIM")) {\r
+ //\r
+ // Try to remove PeiServicesTablePointer;\r
+ // \r
+ wholeline = dropPeiServicesPointer (wholeline);\r
+ //\r
+ // Drop the possible return Status of Hob building function.\r
+ // \r
+ wholeline = drophobLibReturnStatus (wholeline);\r
+ }\r
+ //\r
+ // Expand obsolete R8 macro.\r
+ // \r
+ wholeline = replaceObsoleteMacro (wholeline);\r
\r
- show(filefunc, "function");\r
- show(filemacro, "macro");\r
- show(fileguid, "guid");\r
- show(fileppi, "ppi");\r
- show(fileprotocol, "protocol");\r
- if (!filer8only.isEmpty()) {\r
- ui.println("Converting r8only : " + filer8only);\r
- }\r
+ show(filefunc, "function");\r
+ show(filemacro, "macro");\r
+ show(fileguid, "guid");\r
+ show(fileppi, "ppi");\r
+ show(fileprotocol, "protocol");\r
+ if (!filer8only.isEmpty()) {\r
+ MigrationTool.ui.println("Converting r8only : " + filer8only);\r
+ }\r
\r
- filefunc.clear();\r
- filemacro.clear();\r
- fileguid.clear();\r
- fileppi.clear();\r
- fileprotocol.clear();\r
- filer8only.clear();\r
+ filefunc.clear();\r
+ filemacro.clear();\r
+ fileguid.clear();\r
+ fileppi.clear();\r
+ fileprotocol.clear();\r
+ filer8only.clear();\r
\r
- return line;\r
- }\r
- \r
- private void show(Set<r8tor9> hash, String sh) {\r
- Iterator<r8tor9> it = hash.iterator();\r
- r8tor9 temp;\r
- if (showdetails) {\r
- if (!hash.isEmpty()) {\r
- ui.print("Converting " + sh + " : ");\r
- while (it.hasNext()) {\r
- temp = it.next();\r
- ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
- }\r
- ui.println("");\r
- }\r
- }\r
- }\r
- \r
- private void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
- Iterator<String> it;\r
- String r8thing;\r
- String r9thing;\r
- it = hash.iterator();\r
- while (it.hasNext()) {\r
- r8thing = it.next();\r
- if ((r9thing = db.getR9Guidname(r8thing)) != null) {\r
- if (!r8thing.equals(r9thing)) {\r
- if (line.contains(r8thing)) {\r
- line = line.replaceAll(r8thing, r9thing);\r
- filehash.add(new r8tor9(r8thing, r9thing));\r
- }\r
- }\r
- }\r
- }\r
- }\r
+ return wholeline;\r
+ }\r
+ \r
+ public boolean recognize(String filename) {\r
+ return filename.contains(".c") || filename.contains(".C");\r
+ }\r
+\r
+ public String namechange(String oldname) {\r
+ if (oldname.contains(".C")) {\r
+ return oldname.replaceFirst(".C", ".c");\r
+ } else {\r
+ return oldname;\r
+ }\r
+ }\r
+ }\r
+ //---------------------------------------inner classes---------------------------------------//\r
+\r
+ //-------------------------------------process functions-------------------------------------//\r
+ private static final String addincludefile(String wholeline, String hfile) {\r
+ return wholeline.replaceFirst("(\\*/\\s)", "$1\n#include " + hfile + "\n");\r
+ }\r
+ \r
+ private static final void show(Set<r8tor9> hash, String sh) {\r
+ Iterator<r8tor9> it = hash.iterator();\r
+ r8tor9 temp;\r
+ if (!hash.isEmpty()) {\r
+ MigrationTool.ui.print("Converting " + sh + " : ");\r
+ while (it.hasNext()) {\r
+ temp = it.next();\r
+ MigrationTool.ui.print("[" + temp.r8thing + "->" + temp.r9thing + "] ");\r
+ }\r
+ MigrationTool.ui.println("");\r
+ }\r
+ }\r
+\r
+ private static final void replaceGuid(String line, Set<String> hash, String kind, Set<r8tor9> filehash) {\r
+ Iterator<String> it;\r
+ String r8thing;\r
+ String r9thing;\r
+ it = hash.iterator();\r
+ while (it.hasNext()) {\r
+ r8thing = it.next();\r
+ if ((r9thing = MigrationTool.db.getR9Guidname(r8thing)) != null) {\r
+ if (!r8thing.equals(r9thing)) {\r
+ if (line.contains(r8thing)) {\r
+ line = line.replaceAll(r8thing, r9thing);\r
+ filehash.add(new r8tor9(r8thing, r9thing));\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ private final String dropPeiServicesPointer (String wholeline) {\r
+ String peiServicesTablePointer;\r
+ String peiServicesTableCaller;\r
+ String regPeiServices;\r
+ Pattern ptnPei;\r
+ Matcher mtrPei;\r
+\r
+ peiServicesTablePointer = "\\w(?:\\w|[0-9]|->)*";\r
+ peiServicesTableCaller = "\\(\\*\\*?\\s*(" + peiServicesTablePointer + ")\\s*\\)[.-]>?\\s*";\r
+ for (int i = 0; i < peiserviceslibfunc.length; i++) {\r
+ regPeiServices = peiServicesTableCaller + peiserviceslibfunc[i] + "\\s*\\(\\s*\\1\\s*,(\\t| )*"; \r
+ ptnPei = Pattern.compile (regPeiServices);\r
+ mtrPei = ptnPei.matcher (wholeline);\r
+ if (mtrPei.find()) {\r
+ wholeline = mtrPei.replaceAll("PeiServices" + peiserviceslibfunc[i] + " (");\r
+ mi.hashrequiredr9libs.add("PeiServicesLib");\r
+ }\r
+ }\r
+ regPeiServices = peiServicesTableCaller + "(CopyMem|SetMem)" + "\\s*\\((\\t| )*";\r
+ ptnPei = Pattern.compile (regPeiServices);\r
+ mtrPei = ptnPei.matcher (wholeline);\r
+ if (mtrPei.find()) {\r
+ wholeline = mtrPei.replaceAll("$2 (");\r
+ mi.hashrequiredr9libs.add("BaseMemoryLib");\r
+ }\r
+\r
+ ptnPei = Pattern.compile("#%+(\\s*\\(+\\s*)" + peiServicesTablePointer + "\\s*,\\s*", Pattern.MULTILINE);\r
+ mtrPei = ptnPei.matcher(wholeline);\r
+ while (mtrPei.find()) {\r
+ wholeline = mtrPei.replaceAll("$1");\r
+ }\r
+\r
+ return wholeline;\r
+ }\r
+\r
+ private final String drophobLibReturnStatus (String wholeline) { // or use regex to find pattern "Status = ..."\r
+ Pattern ptnhobstatus;\r
+ Matcher mtrhobstatus;\r
+ String templine = wholeline;\r
+ for (int i = 0; i < specialhoblibfunc.length; i++) {\r
+ ptnhobstatus = Pattern.compile("(Status\\s*=\\s*)?" + specialhoblibfunc[i] + "(.*?\\)\\s*;)", Pattern.DOTALL);\r
+ mtrhobstatus = ptnhobstatus.matcher(templine);\r
+ if (mtrhobstatus.find()) {\r
+ templine = mtrhobstatus.replaceAll(specialhoblibfunc[i] + mtrhobstatus.group(2) + "\n " + \r
+ MigrationTool.MIGRATIONCOMMENT + "R9 Hob-building library functions will assert if build failure.\n Status = EFI_SUCCESS;");\r
+ }\r
+ }\r
+ return templine;\r
+ }\r
+\r
+ private final String replaceMacro (String wholeline, Set<String> symbolSet) {\r
+ String r8thing;\r
+ String r9thing;\r
+ Iterator<String> it;\r
+\r
+ it = symbolSet.iterator();\r
+ while (it.hasNext()) { //macros are all assumed MdePkg currently\r
+ r8thing = it.next();\r
+ //mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); \r
+ if ((r9thing = MigrationTool.db.getR9Macro(r8thing)) != null) {\r
+ if (wholeline.contains(r8thing)) {\r
+ wholeline = wholeline.replaceAll(r8thing, r9thing);\r
+ filemacro.add(new r8tor9(r8thing, r9thing));\r
+ }\r
+ }\r
+ }\r
+ return wholeline;\r
+ }\r
+\r
+ private final String replaceLibrary (String wholeline, Set<String> symbolSet) {\r
+ boolean addr8 = false;\r
+ // start replacing names\r
+ String r8thing;\r
+ String r9thing;\r
+ Iterator<String> it;\r
+ // Converting non-locla function\r
+ it = symbolSet.iterator();\r
+ while (it.hasNext()) {\r
+ r8thing = it.next();\r
+ mi.hashrequiredr9libs.add(MigrationTool.db.getR9Lib(r8thing)); // add a library here\r
+ \r
+ r8tor9 temp;\r
+ if ((r9thing = MigrationTool.db.getR9Func(r8thing)) != null) {\r
+ if (!r8thing.equals(r9thing)) {\r
+ if (wholeline.contains(r8thing)) {\r
+ wholeline = wholeline.replaceAll(r8thing, r9thing);\r
+ filefunc.add(new r8tor9(r8thing, r9thing));\r
+ Iterator<r8tor9> rt = filefunc.iterator();\r
+ while (rt.hasNext()) {\r
+ temp = rt.next();\r
+ if (MigrationTool.db.r8only.contains(temp.r8thing)) {\r
+ filer8only.add(r8thing);\r
+ mi.hashr8only.add(r8thing);\r
+ addr8 = true;\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+ } //is any of the guids changed?\r
+ if (addr8 == true) {\r
+ wholeline = addincludefile(wholeline, "\"R8Lib.h\"");\r
+ }\r
+ return wholeline;\r
+ }\r
+\r
+ private final String replaceObsoleteMacro (String wholeline) {\r
+ Matcher mtrmac;\r
+ mtrmac = Pattern.compile("EFI_IDIV_ROUND\\((.*), (.*)\\)").matcher(wholeline);\r
+ if (mtrmac.find()) {\r
+ wholeline = mtrmac.replaceAll("\\($1 \\/ $2 \\+ \\(\\(\\(2 \\* \\($1 \\% $2\\)\\) \\< $2\\) \\? 0 \\: 1\\)\\)");\r
+ }\r
+ mtrmac = Pattern.compile("EFI_MIN\\((.*), (.*)\\)").matcher(wholeline);\r
+ if (mtrmac.find()) {\r
+ wholeline = mtrmac.replaceAll("\\(\\($1 \\< $2\\) \\? $1 \\: $2\\)");\r
+ }\r
+ mtrmac = Pattern.compile("EFI_MAX\\((.*), (.*)\\)").matcher(wholeline);\r
+ if (mtrmac.find()) {\r
+ wholeline = mtrmac.replaceAll("\\(\\($1 \\> $2\\) \\? $1 \\: $2\\)");\r
+ }\r
+ mtrmac = Pattern.compile("EFI_UINTN_ALIGNED\\((.*)\\)").matcher(wholeline);\r
+ if (mtrmac.find()) {\r
+ wholeline = mtrmac.replaceAll("\\(\\(\\(UINTN\\) $1\\) \\& \\(sizeof \\(UINTN\\) \\- 1\\)\\)");\r
+ }\r
+ if (wholeline.contains("EFI_UINTN_ALIGN_MASK")) {\r
+ wholeline = wholeline.replaceAll("EFI_UINTN_ALIGN_MASK", "(sizeof (UINTN) - 1)");\r
+ }\r
+ return wholeline;\r
+ }\r
+\r
+ private final void addr8only() throws Exception {\r
+ String paragraph = null;\r
+ String line = Common.file2string(MigrationTool.db.DatabasePath + File.separator + "R8Lib.c");\r
+ PrintWriter outfile1 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.c")));\r
+ PrintWriter outfile2 = new PrintWriter(new BufferedWriter(new FileWriter(MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator + "R8Lib.h")));\r
+ Pattern ptnr8only = Pattern.compile("////#?(\\w*)?(.*?R8_(\\w*).*?)////~", Pattern.DOTALL);\r
+ Matcher mtrr8only = ptnr8only.matcher(line);\r
+ Matcher mtrr8onlyhead;\r
+ \r
+ //add head comment\r
+ Matcher mtrr8onlyheadcomment = Critic.PTN_NEW_HEAD_COMMENT.matcher(line);\r
+ if (mtrr8onlyheadcomment.find()) {\r
+ outfile1.append(mtrr8onlyheadcomment.group() + "\n\n");\r
+ outfile2.append(mtrr8onlyheadcomment.group() + "\n\n");\r
+ }\r
+ \r
+ //add functions body\r
+ while (mtrr8only.find()) {\r
+ if (mi.hashr8only.contains(mtrr8only.group(3))) {\r
+ paragraph = mtrr8only.group(2);\r
+ outfile1.append(paragraph + "\n\n");\r
+ if (mtrr8only.group(1).length() != 0) {\r
+ mi.hashrequiredr9libs.add(mtrr8only.group(1));\r
+ }\r
+ //generate R8lib.h\r
+ while ((mtrr8onlyhead = Func.ptnbrace.matcher(paragraph)).find()) {\r
+ paragraph = mtrr8onlyhead.replaceAll(";");\r
+ }\r
+ outfile2.append(paragraph + "\n\n");\r
+ }\r
+ }\r
+ outfile1.flush();\r
+ outfile1.close();\r
+ outfile2.flush();\r
+ outfile2.close();\r
+ \r
+ mi.localmodulesources.add("R8Lib.h");\r
+ mi.localmodulesources.add("R8Lib.c");\r
+ }\r
+ //-------------------------------------process functions-------------------------------------//\r
+ \r
+ //-----------------------------------ForDoAll-----------------------------------//\r
+ public void run(String filepath) throws Exception {\r
+ String inname = filepath.replace(mi.modulepath + File.separator + "temp" + File.separator, "");\r
+ String tempinpath = mi.modulepath + File.separator + "temp" + File.separator;\r
+ String tempoutpath = MigrationTool.ModuleInfoMap.get(mi) + File.separator + "Migration_" + mi.modulename + File.separator;\r
+\r
+ Iterator<Common.Laplace> itLaplace = Laplaces.iterator();\r
+ while (itLaplace.hasNext()) {\r
+ Common.Laplace lap = itLaplace.next();\r
+ if (lap.recognize(inname)) {\r
+ MigrationTool.ui.println("\nHandling file: " + inname);\r
+ lap.transform(tempinpath + inname, tempoutpath + lap.namechange(inname));\r
+ }\r
+ }\r
+ }\r
+ \r
+ public boolean filter(File dir) {\r
+ return true;\r
+ }\r
+ //-----------------------------------ForDoAll-----------------------------------//\r
+ \r
+ private final void setModuleInfo(ModuleInfo moduleinfo) {\r
+ mi = moduleinfo;\r
+ }\r
+ \r
+ private final void start() throws Exception {\r
+ Laplaces.add(new DxsLaplace());\r
+ Laplaces.add(new CLaplace());\r
+ Laplaces.add(new IdleLaplace());\r
+ \r
+ Common.toDoAll(mi.modulepath + File.separator + "temp", this, Common.FILE);\r
+ \r
+ if (!mi.hashr8only.isEmpty()) {\r
+ addr8only();\r
+ }\r
+ \r
+ Laplaces.clear();\r
+ }\r
+ \r
+ public static final void fireAt(ModuleInfo moduleinfo) throws Exception {\r
+ SFReplacer.setModuleInfo(moduleinfo);\r
+ SFReplacer.start();\r
+ }\r
}\r