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