Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsify.pl
1 #!/usr/local/bin/perl
2
3 # This script modifies C code to use the hijacked NSPR routines that are
4 # now baked into the JavaScript engine rather than using the NSPR
5 # routines that they were based on, i.e. types like PRArenaPool are changed
6 # to JSArenaPool.
7 #
8 # This script was used in 9/98 to facilitate the incorporation of some NSPR
9 # code into the JS engine so as to minimize dependency on NSPR.
10 #
11
12 # Command-line: jsify.pl [options] [filename]*
13 #
14 # Options:
15 #  -r         Reverse direction of transformation, i.e. JS ==> NSPR2
16 #  -outdir    Directory in which to place output files
17
18
19 # NSPR2 symbols that will be modified to JS symbols, e.g.
20 # PRArena <==> JSArena
21
22 @NSPR_symbols = (
23 "PRArena",
24 "PRArenaPool",
25 "PRArenaStats",
26 "PR_ARENAMETER",
27 "PR_ARENA_",
28 "PR_ARENA_ALIGN",
29 "PR_ARENA_ALLOCATE",
30 "PR_ARENA_CONST_ALIGN_MASK",
31 "PR_ARENA_DEFAULT_ALIGN",
32 "PR_ARENA_DESTROY",
33 "PR_ARENA_GROW",
34 "PR_ARENA_MARK",
35 "PR_ARENA_RELEASE",
36
37 "PR_smprintf",
38 "PR_smprintf_free",
39 "PR_snprintf",
40 "PR_sprintf_append",
41 "PR_sscanf",
42 "PR_sxprintf",
43 "PR_vsmprintf",
44 "PR_vsnprintf",
45 "PR_vsprintf_append",
46 "PR_vsxprintf",
47
48 "PRCList",
49 "PRCListStr",
50 "PRCLists",
51
52 "PRDestroyEventProc",
53 "PREvent",
54 "PREventFunProc",
55 "PREventQueue",
56 "PRHandleEventProc",
57 "PR_PostEvent",
58 "PR_PostSynchronousEvent",
59 "PR_ProcessPendingEvents",
60 "PR_CreateEventQueue",
61 "PR_DequeueEvent",
62 "PR_DestroyEvent",
63 "PR_DestroyEventQueue",
64 "PR_EventAvailable",
65 "PR_EventLoop",
66 "PR_GetEvent",
67 "PR_GetEventOwner",
68 "PR_GetEventQueueMonitor",
69 "PR_GetEventQueueSelectFD",
70 "PR_GetMainEventQueue",
71 "PR_HandleEvent",
72 "PR_InitEvent",
73 "PR_ENTER_EVENT_QUEUE_MONITOR",
74 "PR_EXIT_EVENT_QUEUE_MONITOR",
75 "PR_MapEvents",
76 "PR_RevokeEvents",
77
78 "PR_cnvtf",
79 "PR_dtoa",
80 "PR_strtod",
81
82 "PRFileDesc",
83
84 "PR_HASH_BITS",
85 "PR_GOLDEN_RATIO",
86 "PRHashAllocOps",
87 "PRHashComparator",
88 "PRHashEntry",
89 "PRHashEnumerator",
90 "PRHashFunction",
91 "PRHashNumber",
92 "PRHashTable",
93 "PR_HashString",
94 "PR_HashTableAdd",
95 "PR_HashTableDestroy",
96 "PR_HashTableDump",
97 "PR_HashTableEnumerateEntries",
98 "PR_HashTableLookup",
99 "PR_HashTableRawAdd",
100 "PR_HashTableRawLookup",
101 "PR_HashTableRawRemove",
102 "PR_HashTableRemove",
103
104 "PRBool",
105 "PRFloat64",
106 "PRInt16",
107 "PRInt32",
108 "PRInt64",
109 "PRInt8",
110 "PRIntn",
111 "PRUint16",
112 "PRUint32",
113 "PRUint64",
114 "PRUint8",
115 "PRUintn",
116 "PRPtrDiff",
117 "PRPtrdiff",
118 "PRUptrdiff",
119 "PRUword",
120 "PRWord",
121 "PRPackedBool",
122 "PRSize",
123 "PRStatus",
124 "pruword",
125 "prword",
126 "prword_t",
127
128 "PR_ALIGN_OF_DOUBLE",
129 "PR_ALIGN_OF_FLOAT",
130 "PR_ALIGN_OF_INT",
131 "PR_ALIGN_OF_INT64",
132 "PR_ALIGN_OF_LONG",
133 "PR_ALIGN_OF_POINTER",
134 "PR_ALIGN_OF_SHORT",
135 "PR_ALIGN_OF_WORD",
136 "PR_BITS_PER_BYTE",
137 "PR_BITS_PER_BYTE_LOG2",
138 "PR_BITS_PER_DOUBLE",
139 "PR_BITS_PER_DOUBLE_LOG2",
140 "PR_BITS_PER_FLOAT",
141 "PR_BITS_PER_FLOAT_LOG2",
142 "PR_BITS_PER_INT",
143 "PR_BITS_PER_INT64",
144 "PR_BITS_PER_INT64_LOG2",
145 "PR_BITS_PER_INT_LOG2",
146 "PR_BITS_PER_LONG",
147 "PR_BITS_PER_LONG_LOG2",
148 "PR_BITS_PER_SHORT",
149 "PR_BITS_PER_SHORT_LOG2",
150 "PR_BITS_PER_WORD",
151 "PR_BITS_PER_WORD_LOG2",
152 "PR_BYTES_PER_BYTE",
153 "PR_BYTES_PER_DOUBLE",
154 "PR_BYTES_PER_DWORD",
155 "PR_BYTES_PER_DWORD_LOG2",
156 "PR_BYTES_PER_FLOAT",
157 "PR_BYTES_PER_INT",
158 "PR_BYTES_PER_INT64",
159 "PR_BYTES_PER_LONG",
160 "PR_BYTES_PER_SHORT",
161 "PR_BYTES_PER_WORD",
162 "PR_BYTES_PER_WORD_LOG2",
163
164 "PRSegment",
165 "PRSegmentAccess",
166 "PRStuffFunc",
167 "PRThread",
168
169 "PR_APPEND_LINK",
170
171 "PR_ASSERT",
172
173 "PR_ATOMIC_DWORD_LOAD",
174 "PR_ATOMIC_DWORD_STORE",
175
176 "PR_Abort",
177
178 "PR_ArenaAllocate",
179 "PR_ArenaCountAllocation",
180 "PR_ArenaCountGrowth",
181 "PR_ArenaCountInplaceGrowth",
182 "PR_ArenaCountRelease",
183 "PR_ArenaCountRetract",
184 "PR_ArenaFinish",
185 "PR_ArenaGrow",
186 "PR_ArenaRelease",
187 "PR_CompactArenaPool",
188 "PR_DumpArenaStats",
189 "PR_FinishArenaPool",
190 "PR_FreeArenaPool",
191 "PR_InitArenaPool",
192
193 "PR_Assert",
194
195 "PR_AttachThread",
196
197 "PR_BEGIN_EXTERN_C",
198 "PR_BEGIN_MACRO",
199
200 "PR_BIT",
201 "PR_BITMASK",
202
203 "PR_BUFFER_OVERFLOW_ERROR",
204
205 "PR_CALLBACK",
206 "PR_CALLBACK_DECL",
207 "PR_CALLOC",
208 "PR_CEILING_LOG2",
209 "PR_CLEAR_ARENA",
210 "PR_CLEAR_BIT",
211 "PR_CLEAR_UNUSED",
212 "PR_CLIST_IS_EMPTY",
213 "PR_COUNT_ARENA",
214 "PR_CURRENT_THREAD",
215
216 "PR_GetSegmentAccess",
217 "PR_GetSegmentSize",
218 "PR_GetSegmentVaddr",
219 "PR_GrowSegment",
220 "PR_DestroySegment",
221 "PR_MapSegment",
222 "PR_NewSegment",
223 "PR_Segment",
224 "PR_Seg",
225 "PR_SEGMENT_NONE",
226 "PR_SEGMENT_RDONLY",
227 "PR_SEGMENT_RDWR",
228
229 "PR_Calloc",
230 "PR_CeilingLog2",
231 "PR_CompareStrings",
232 "PR_CompareValues",
233 "PR_DELETE",
234 "PR_END_EXTERN_C",
235 "PR_END_MACRO",
236 "PR_ENUMERATE_STOP",
237 "PR_FAILURE",
238 "PR_FALSE",
239 "PR_FLOOR_LOG2",
240 "PR_FREEIF",
241 "PR_FREE_PATTERN",
242 "PR_FloorLog2",
243 "PR_FormatTime",
244 "PR_Free",
245
246 "PR_GetEnv",
247 "PR_GetError",
248 "PR_INIT_ARENA_POOL",
249 "PR_INIT_CLIST",
250 "PR_INIT_STATIC_CLIST",
251 "PR_INLINE",
252 "PR_INSERT_AFTER",
253 "PR_INSERT_BEFORE",
254 "PR_INSERT_LINK",
255 "PR_INT32",
256 "PR_INTERVAL_NO_TIMEOUT",
257 "PR_INTERVAL_NO_WAIT",
258 "PR_Init",
259 "PR_LIST_HEAD",
260 "PR_LIST_TAIL",
261 "PR_LOG",
262 "PR_LOGGING",
263 "PR_LOG_ALWAYS",
264 "PR_LOG_BEGIN",
265 "PR_LOG_DEBUG",
266 "PR_LOG_DEFINE",
267 "PR_LOG_END",
268 "PR_LOG_ERROR",
269 "PR_LOG_MAX",
270 "PR_LOG_MIN",
271 "PR_LOG_NONE",
272 "PR_LOG_NOTICE",
273 "PR_LOG_TEST",
274 "PR_LOG_WARN",
275 "PR_LOG_WARNING",
276 "PR_LogFlush",
277 "PR_LogPrint",
278 "PR_MALLOC",
279 "PR_MAX",
280 "PR_MD_calloc",
281 "PR_MD_free",
282 "PR_MD_malloc",
283 "PR_MD_realloc",
284 "PR_MIN",
285 "PR_Malloc",
286 "PR_NEW",
287 "PR_NEWZAP",
288 "PR_NEXT_LINK",
289 "PR_NOT_REACHED",
290 "PR_NewCondVar",
291 "PR_NewHashTable",
292 "PR_NewLogModule",
293 "PR_PREV_LINK",
294 "PR_PUBLIC_API",
295 "PR_PUBLIC_DATA",
296 "PR_RANGE_ERROR",
297 "PR_REALLOC",
298 "PR_REMOVE_AND_INIT_LINK",
299 "PR_REMOVE_LINK",
300 "PR_ROUNDUP",
301 "PR_Realloc",
302
303 "PR_SET_BIT",
304 "PR_STATIC_CALLBACK",
305 "PR_SUCCESS",
306 "PR_SetError",
307 "PR_SetLogBuffering",
308 "PR_SetLogFile",
309
310 "PR_TEST_BIT",
311 "PR_TRUE",
312 "PR_UINT32",
313 "PR_UPTRDIFF",
314
315 "prarena_h___",
316 "prbit_h___",
317 "prclist_h___",
318 "prdtoa_h___",
319 "prlog_h___",
320 "prlong_h___",
321 "prmacos_h___",
322 "prmem_h___",
323 "prprf_h___",
324 "prtypes_h___",
325
326 "prarena",
327 "prbit",
328 "prbitmap_t",
329 "prclist",
330 "prcpucfg",
331 "prdtoa",
332 "prhash",
333 "plhash",
334 "prlong",
335 "prmacos",
336 "prmem",
337 "prosdep",
338 "protypes",
339 "prprf",
340 "prtypes"
341 );
342
343 while ($ARGV[0] =~ /^-/) {
344     if ($ARGV[0] eq "-r") {
345         shift;
346         $reverse_conversion = 1;
347     } elsif ($ARGV[0] eq "-outdir") {
348         shift;
349         $outdir = shift;
350     }
351 }
352
353 # Given an NSPR symbol compute the JS equivalent or
354 # vice-versa
355 sub subst {
356     local ($replacement);
357     local ($sym) = @_;
358     
359     $replacement = substr($sym,0,2) eq "pr" ? "js" : "JS";
360     $replacement .= substr($sym, 2);
361     return $replacement;
362 }
363
364 # Build the regular expression that will convert between the NSPR
365 # types and the JS types
366 if ($reverse_conversion) {
367     die "Not implemented yet";
368 } else {
369     foreach $sym (@NSPR_symbols) {
370         $regexp .= $sym . "|"
371     }
372     # Get rid of the last "!"
373     chop $regexp;
374
375     # Replace PR* with JS* and replace pr* with js*
376     $regexp = 's/(^|\\W)(' . $regexp . ')/$1 . &subst($2)/eg';
377 #    print $regexp;
378 }
379
380 # Pre-compile a little subroutine to perform the regexp substitution
381 # between NSPR types and JS types
382 eval('sub convert_from_NSPR {($line) = @_; $line =~ ' . $regexp . ';}');
383
384 sub convert_mallocs {
385     ($line) = @_;
386     $line =~ s/PR_MALLOC/malloc/g;
387     $line =~ s/PR_REALLOC/realloc/g;
388     $line =~ s/PR_FREE/free/g;
389     return $line;
390 }
391
392 sub convert_includes {
393     ($line) = @_;
394     if ($line !~ /include/) {
395         return $line;
396     }
397
398     if ($line =~ /prlog\.h/) {
399         $line = '#include "jsutil.h"'. " /* Added by JSIFY */\n";
400     } elsif ($line =~ /plhash\.h/) {
401         $line = '#include "jshash.h"'. " /* Added by JSIFY */\n";
402     } elsif ($line =~ /plarena\.h/) {
403         $line = '#include "jsarena.h"'. " /* Added by JSIFY */\n";
404     } elsif ($line =~ /prmem\.h/) {
405         $line  = "";
406     } elsif ($line =~ /jsmsg\.def/) {
407         $line  = '#include "js.msg"' . "\n";
408     } elsif ($line =~ /shellmsg\.def/) {
409         $line  = '#include "jsshell.msg"' . "\n";
410     } elsif ($line =~ /jsopcode\.def/) {
411         $line  = '#include "jsopcode.tbl"' . "\n";
412     }
413     return $line;
414 }
415
416 sub convert_declarations {
417     ($line) = @_;
418     $line =~ s/PR_EXTERN/JS_EXTERN_API/g;
419     $line =~ s/PR_IMPLEMENT_DATA/JS_EXPORT_DATA/g;
420     $line =~ s/PR_IMPLEMENT/JS_EXPORT_API/g;
421     $line =~ s/PR_IMPORT/JS_IMPORT/g;
422     $line =~ s/PR_PUBLIC_API/JS_EXPORT_API/g;
423     $line =~ s/PR_PUBLIC_DATA/JS_EXPORT_DATA/g;
424     return $line;
425 }    
426
427 sub convert_long_long_macros {
428     ($line) = @_;
429     $line =~ s/\b(LL_)/JSLL_/g;
430     return $line;
431 }    
432
433 sub convert_asserts {
434     ($line) = @_;
435     $line =~ s/\bPR_ASSERT/JS_ASSERT/g;
436     return $line;
437 }
438
439 while ($#ARGV >= 0) {
440     $infile = shift;
441
442     # Change filename, e.g. prtime.h to jsprtime.h, except for legacy
443     # files that start with 'prmj', like prmjtime.h.
444     $outfile = $infile;
445     if ($infile !~ /^prmj/) {
446         $outfile =~ s/^pr/js/;
447         $outfile =~ s/^pl/js/;
448     }
449     
450     if ($outdir) {
451         $outfile = $outdir . '/' . $outfile;
452     }   
453
454     if ($infile eq $outfile) {
455         die "Error: refuse to overwrite $outfile, use -outdir option."
456     }
457     die "Can't open $infile" if !open(INFILE, "<$infile");
458     die "Can't open $outfile for writing" if !open(OUTFILE, ">$outfile");
459
460     while (<INFILE>) {
461         $line = $_;
462         
463         #Get rid of #include "prlog.h"
464         &convert_includes($line);
465
466         # Rename PR_EXTERN, PR_IMPORT, etc.
467         &convert_declarations($line);
468
469         # Convert from PR_MALLOC to malloc, etc.
470         &convert_mallocs($line);
471         
472         # Convert from PR_ASSERT to JS_ASSERT
473 #       &convert_asserts($line);
474
475         # Convert from, e.g. PRArena to JSPRArena
476         &convert_from_NSPR($line);
477
478         # Change LL_* macros to JSLL_*
479         &convert_long_long_macros($line);
480         
481         print OUTFILE $line;
482     }
483 }