+ 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.temppath + File.separator, "");\r
+ String tempinpath = mi.temppath + 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.temppath, 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