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