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