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