[svn:parrot] r46681 - in branches/ops_pct: compilers/opsc compilers/opsc/src/Ops compilers/opsc/src/Ops/Trans include/parrot/oplib src/ops

cotto at svn.parrot.org cotto at svn.parrot.org
Sat May 15 19:21:46 UTC 2010


Author: cotto
Date: Sat May 15 19:21:45 2010
New Revision: 46681
URL: https://trac.parrot.org/parrot/changeset/46681

Log:
[opsc] move ops.h generation into Ops::Emitter, regenerate ops

Modified:
   branches/ops_pct/compilers/opsc/ops2c.nqp
   branches/ops_pct/compilers/opsc/src/Ops/Emitter.pm
   branches/ops_pct/compilers/opsc/src/Ops/Trans.pm
   branches/ops_pct/compilers/opsc/src/Ops/Trans/C.pm
   branches/ops_pct/include/parrot/oplib/ops.h
   branches/ops_pct/src/ops/core_ops.c

Modified: branches/ops_pct/compilers/opsc/ops2c.nqp
==============================================================================
--- branches/ops_pct/compilers/opsc/ops2c.nqp	Sat May 15 18:31:28 2010	(r46680)
+++ branches/ops_pct/compilers/opsc/ops2c.nqp	Sat May 15 19:21:45 2010	(r46681)
@@ -86,58 +86,7 @@
     :flags( hash( core => $core ) ),
 );
 
-if $core {
-    # Emit oplib/ops.h
-    # TODO Move it somewhere else and test it properly.
-    my $ops_h := pir::open__Ps("include/parrot/oplib/ops.h", "w")
-                      || die("Can't open ops.h");
-
-    $ops_h.print(q|
-/* ex: set ro:
- * !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
- *
- * This file is generated automatically from 'src/ops/core.ops'
- * by ops2c.
- *
- * Any changes made here will be lost!
- */
-
-#ifndef PARROT_OPS_H_GUARD
-#define PARROT_OPS_H_GUARD
-
-typedef enum {
-|);
-
-    my $sb := pir::new__Ps('StringBuilder');
-    my $last_op_code := +$f.ops - 1;
-    say("# Last $last_op_code\n");
-    for $f.ops -> $op {
-        $sb.append_format("    PARROT_OP_%0%1 %2 /* %3 */\n",
-            $op.full_name,
-            ($op.code == $last_op_code ?? ' ' !! ','),
-            pir::repeat__SsI(' ', 30 - pir::length__Is($op.full_name)),
-            $op.code);
-    }
-
-    $ops_h.print(~$sb);
-
-    $ops_h.print(q|
-} parrot_opcode_enums;
-
-#endif /* PARROT_OPS_H_GUARD */
-
-/*
- * Local variables:
- *   c-file-style: "parrot"
- * End:
- * vim: expandtab shiftwidth=4:
- */
-    |);
-
-    $ops_h.close;
-}
-
-$emitter.print_c_header_file();
+$emitter.print_c_header_files();
 $emitter.print_c_source_file();
 
 # vim: expandtab shiftwidth=4 ft=perl6:

Modified: branches/ops_pct/compilers/opsc/src/Ops/Emitter.pm
==============================================================================
--- branches/ops_pct/compilers/opsc/src/Ops/Emitter.pm	Sat May 15 18:31:28 2010	(r46680)
+++ branches/ops_pct/compilers/opsc/src/Ops/Emitter.pm	Sat May 15 19:21:45 2010	(r46681)
@@ -35,13 +35,14 @@
 
     if %flags<core> {
         self<include> := "parrot/oplib/$base_ops_h";
-        self<header>  := (~%flags<dir>) ~ "include/" ~ self<include>;
+        self<func_header> := (~%flags<dir>) ~ "include/" ~ self<include>;
+        self<enum_header> := (~%flags<dir>) ~ "include/parrot/oplib/ops.h";
         self<source>  := (~%flags<dir>) ~ "src/ops/$base_ops_stub.c";
     }
     else {
         my $dynops_dir := subst( $file, /\w+\.ops$$/, '');
         self<include> := $base ~ "_ops.h";
-        self<header>  := $dynops_dir ~ self<include>;
+        self<func_header>  := $dynops_dir ~ self<include>;
         self<source>  := $dynops_dir ~ $base ~ "_ops.c";
     }
 
@@ -70,23 +71,44 @@
 method suffix()     { self<suffix> };
 method bs()         { self<bs> };
 
-method print_c_header_file() {
-    my $fh := pir::open__PSs(self<header>, 'w') || die("Can't open filehandle");
-    self.emit_c_header_file($fh);
+method print_c_header_files() {
+    my $fh := pir::open__PSs(self<func_header>, 'w')
+        || die("Can't open "~ self<func_header>);
+    self.emit_c_op_funcs_header($fh);
     $fh.close();
-    return self<header>;
+    if self.ops_file<core> {
+        $fh := pir::open__PSs(self<enum_header>, 'w')
+            || die("Can't open "~ self<enum_header>);
+        self.emit_c_op_enum_header($fh);
+        $fh.close();
+    }
 }
 
-method emit_c_header_file($fh) {
+method emit_c_op_funcs_header($fh) {
 
-    self._emit_guard_prefix($fh);
+    self._emit_guard_prefix($fh, self<func_header>);
 
     self._emit_preamble($fh);
 
+    self._emit_includes($fh);
+
     # Emit runcore specific part.
-    self.trans.emit_c_header_part($fh);
+    self.trans.emit_c_op_funcs_header_part($fh);
 
-    self._emit_guard_suffix($fh);
+    self._emit_guard_suffix($fh, self<func_header>);
+
+    self._emit_coda($fh);
+}
+
+method emit_c_op_enum_header($fh) {
+
+    self._emit_guard_prefix($fh, self<enum_header>);
+
+    self._emit_preamble($fh);
+
+    self._emit_c_op_enum_header_part($fh);
+
+    self._emit_guard_suffix($fh, self<enum_header>);
 
     self._emit_coda($fh);
 }
@@ -119,6 +141,25 @@
     self._emit_coda($fh);
 }
 
+method _emit_c_op_enum_header_part($fh) {
+    my $sb := pir::new__Ps('StringBuilder');
+    my $last_op_code := +self.ops_file.ops - 1;
+    for self.ops_file.ops -> $op {
+        $sb.append_format("    PARROT_OP_%0%1 %2 /* %3 */\n",
+            $op.full_name,
+            ($op.code == $last_op_code ?? ' ' !! ','),
+            pir::repeat__SsI(' ', 30 - pir::length__Is($op.full_name)),
+            $op.code);
+    }
+    $fh.print(q|
+typedef enum {
+|);
+    $fh.print(~$sb);
+    $fh.print(q|
+} parrot_opcode_enums;
+|);
+}
+
 method _emit_source_preamble($fh) {
 
     self._emit_preamble($fh);
@@ -215,11 +256,10 @@
 
 # given a headerfile name like "include/parrot/oplib/core_ops.h", this
 # returns a string like "PARROT_OPLIB_CORE_OPS_H_GUARD"
-method _generate_guard_macro_name() {
-    my $fn   := self<header>;
-    $fn := subst($fn, /.h$/, '');
-    #my @path = File::Spec->splitdir($fn);
-    my @path := split('/', $fn);
+method _generate_guard_macro_name($filename) {
+    $filename := subst($filename, /.h$/, '');
+    #my @path = File::Spec->splitdir($filename);
+    my @path := split('/', $filename);
     @path.shift if @path[0]~'/' eq self<flags><dir>;
     @path.shift if @path[0] eq 'include';
     @path.shift if @path[0] eq 'parrot';
@@ -227,8 +267,8 @@
 }
 
 
-method _emit_guard_prefix($fh) {
-    my $guardname := self._generate_guard_macro_name();
+method _emit_guard_prefix($fh, $filename) {
+    my $guardname := self._generate_guard_macro_name($filename);
     $fh.print(qq/
 #ifndef $guardname
 #define $guardname
@@ -236,8 +276,8 @@
 /);
 }
 
-method _emit_guard_suffix($fh) {
-    my $guardname := self._generate_guard_macro_name();
+method _emit_guard_suffix($fh, $filename) {
+    my $guardname := self._generate_guard_macro_name($filename);
     $fh.print(qq|
 
 #endif /* $guardname */
@@ -258,6 +298,18 @@
 |);
 }
 
+method _emit_includes($fh) {
+
+    $fh.print(qq|
+#include "parrot/parrot.h"
+#include "parrot/oplib.h"
+#include "parrot/runcore_api.h"
+
+{self.sym_export} op_lib_t *{self.init_func}(PARROT_INTERP, long init);
+
+|);
+}
+
 method _emit_preamble($fh) {
 
     $fh.print(qq|
@@ -276,14 +328,6 @@
         $fh.print("#define PARROT_IN_EXTENSION\n");
     }
 
-    $fh.print(qq|
-#include "parrot/parrot.h"
-#include "parrot/oplib.h"
-#include "parrot/runcore_api.h"
-
-{self.sym_export} op_lib_t *{self.init_func}(PARROT_INTERP, long init);
-
-|);
 }
 
 # vim: expandtab shiftwidth=4 ft=perl6:

Modified: branches/ops_pct/compilers/opsc/src/Ops/Trans.pm
==============================================================================
--- branches/ops_pct/compilers/opsc/src/Ops/Trans.pm	Sat May 15 18:31:28 2010	(r46680)
+++ branches/ops_pct/compilers/opsc/src/Ops/Trans.pm	Sat May 15 19:21:45 2010	(r46681)
@@ -21,7 +21,7 @@
 method prepare_ops($emitter, $ops_file) { die('...') }
 
 #
-method emit_c_header_part($fh) { die('...') }
+method emit_c_op_funcs_header_part($fh) { die('...') }
 
 # Called from Ops::Op.
 method body_prelude() { '' }

Modified: branches/ops_pct/compilers/opsc/src/Ops/Trans/C.pm
==============================================================================
--- branches/ops_pct/compilers/opsc/src/Ops/Trans/C.pm	Sat May 15 18:31:28 2010	(r46680)
+++ branches/ops_pct/compilers/opsc/src/Ops/Trans/C.pm	Sat May 15 19:21:45 2010	(r46681)
@@ -69,7 +69,7 @@
     self<num_entries>   := + at op_funcs + 1;
 }
 
-method emit_c_header_part($fh) {
+method emit_c_op_funcs_header_part($fh) {
     for self<op_protos> -> $proto {
         $fh.print($proto);
     }

Modified: branches/ops_pct/include/parrot/oplib/ops.h
==============================================================================
--- branches/ops_pct/include/parrot/oplib/ops.h	Sat May 15 18:31:28 2010	(r46680)
+++ branches/ops_pct/include/parrot/oplib/ops.h	Sat May 15 19:21:45 2010	(r46681)
@@ -1,16 +1,18 @@
 
+#ifndef PARROT_OPLIB_OPS_H_GUARD
+#define PARROT_OPLIB_OPS_H_GUARD
+
+
 /* ex: set ro:
  * !!!!!!!   DO NOT EDIT THIS FILE   !!!!!!!
  *
- * This file is generated automatically from 'src/ops/core.ops'
- * by ops2c.
+ * This file is generated automatically from 'src/ops/core.ops' (and possibly other
+ * .ops files). by ops2c.nqp.
  *
  * Any changes made here will be lost!
+ *
  */
 
-#ifndef PARROT_OPS_H_GUARD
-#define PARROT_OPS_H_GUARD
-
 typedef enum {
     PARROT_OP_end,                             /* 0 */
     PARROT_OP_noop,                            /* 1 */
@@ -1313,12 +1315,14 @@
 
 } parrot_opcode_enums;
 
-#endif /* PARROT_OPS_H_GUARD */
+
+#endif /* PARROT_OPLIB_OPS_H_GUARD */
+
 
 /*
  * Local variables:
  *   c-file-style: "parrot"
+ *   buffer-read-only: t
  * End:
  * vim: expandtab shiftwidth=4:
  */
-    
\ No newline at end of file

Modified: branches/ops_pct/src/ops/core_ops.c
==============================================================================
--- branches/ops_pct/src/ops/core_ops.c	Sat May 15 18:31:28 2010	(r46680)
+++ branches/ops_pct/src/ops/core_ops.c	Sat May 15 19:21:45 2010	(r46681)
@@ -9,13 +9,6 @@
  *
  */
 
-#include "parrot/parrot.h"
-#include "parrot/oplib.h"
-#include "parrot/runcore_api.h"
-
- op_lib_t *Parrot_DynOp_core_2_3_0(PARROT_INTERP, long init);
-
-
 #include "parrot/oplib/core_ops.h"
 #include "pmc/pmc_parrotlibrary.h"
 #include "pmc/pmc_callcontext.h"


More information about the parrot-commits mailing list