poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[COMMITTED] Support for `rand'


From: Jose E. Marchesi
Subject: [COMMITTED] Support for `rand'
Date: Fri, 06 Sep 2019 15:48:26 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)

Hi people!

I just pushed the patch below, which adds the capability to generate
pseudo-random numbers in a Poke program.

Salud!

commit 7ed7e405a20aa713b6fb3460e1e5f97da37a3081
Author: Jose E. Marchesi <address@hidden>
Date:   Fri Sep 6 15:40:52 2019 +0200

    src,testsuite: add support for the randr Poke builtin
    
    This commit adds support for a new PVM instruction called `rand',
    which pushes to the stack a pseudo-random 32-bit signed integer.  It
    also adds a compiler built-in called `rand'.
    
    ChangeLog:
    
    2019-09-06  Jose E. Marchesi  <address@hidden>
    
            * src/pkl-insn.def: New instruction RAND.
            * src/pvm.jitter (rand): New instruction.
            (wrapped-functions): Add wrapper for `random'.
            * bootstrap.conf (gnulib_modules): Add `random' gnulib module.
            * src/pkl-ast.h (PKL_AST_BUILTIN_RAND): Define.
            * src/pkl-gen.c (pkl_gen_ps_comp_stmt): Emit code for the RAND
            built-int.
            * src/pkl-lex.l: Recognize BUILTIN_RAND token.
            * src/pkl-tab.y: Define token BUILTIN_RANDR.
            (comp_stmt): New rule for builtin randr.
            * testsuite/poke.pkl/rand-1.pk: New file.

diff --git a/ChangeLog b/ChangeLog
index fb84956..b0f1172 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,19 @@
 2019-09-06  Jose E. Marchesi  <address@hidden>
 
+       * src/pkl-insn.def: New instruction RAND.
+       * src/pvm.jitter (rand): New instruction.
+       (wrapped-functions): Add wrapper for `random'.
+       * bootstrap.conf (gnulib_modules): Add `random' gnulib module.
+       * src/pkl-ast.h (PKL_AST_BUILTIN_RAND): Define.
+       * src/pkl-gen.c (pkl_gen_ps_comp_stmt): Emit code for the RAND
+       built-int.
+       * src/pkl-lex.l: Recognize BUILTIN_RAND token.
+       * src/pkl-tab.y: Define token BUILTIN_RANDR.
+       (comp_stmt): New rule for builtin randr.
+       * testsuite/poke.pkl/rand-1.pk: New file.
+
+2019-09-06  Jose E. Marchesi  <address@hidden>
+
        * HACKING (Jitter): Replace git:// URL with an http://, for
        jitter.
        (Development Environment): Add a note about setting POKEDATADIR to
diff --git a/bootstrap.conf b/bootstrap.conf
index bbaf8df..ef064eb 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -20,7 +20,7 @@
 gnulib_modules="
   progname list array-list xalloc getopt-gnu getopt-gnu printf-posix isatty 
readline
   parse-datetime mkstemp tempname tmpdir secure_getenv gcd maintainer-makefile
-  streq gendocs readline"
+  streq gendocs readline random"
 
 checkout_only_file=
 MSGID_BUGS_ADDRESS=address@hidden
diff --git a/src/pkl-ast.h b/src/pkl-ast.h
index 3d2b4b7..1f0558e 100644
--- a/src/pkl-ast.h
+++ b/src/pkl-ast.h
@@ -1181,6 +1181,7 @@ pkl_ast_node pkl_ast_make_var (pkl_ast ast,
 
 #define PKL_AST_BUILTIN_NONE 0
 #define PKL_AST_BUILTIN_PRINT 1
+#define PKL_AST_BUILTIN_RAND 2
 
 struct pkl_ast_comp_stmt
 {
diff --git a/src/pkl-gen.c b/src/pkl-gen.c
index 33f9b7d..f44ef8f 100644
--- a/src/pkl-gen.c
+++ b/src/pkl-gen.c
@@ -332,10 +332,9 @@ PKL_PHASE_BEGIN_HANDLER (pkl_gen_pr_comp_stmt)
   pkl_ast_node comp_stmt = PKL_PASS_NODE;
 
   if (PKL_AST_COMP_STMT_BUILTIN (comp_stmt) == PKL_AST_BUILTIN_NONE)
-    {  
-      /* Push a frame into the environment.  */
-      pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_PUSHF);
-    }
+    /* Push a frame into the environment.  */
+    pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_PUSHF);
+    
 }
 PKL_PHASE_END_HANDLER
 
@@ -355,8 +354,12 @@ PKL_PHASE_BEGIN_HANDLER (pkl_gen_ps_comp_stmt)
     {
       switch (comp_stmt_builtin)
         {
+        case PKL_AST_BUILTIN_RAND:
+          pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_RAND);
+          pkl_asm_insn (PKL_GEN_ASM, PKL_INSN_RETURN);
+          break;
         default:
-            assert (0);
+          assert (0);
         }
     }
   else
diff --git a/src/pkl-insn.def b/src/pkl-insn.def
index 4df1b75..6e78636 100644
--- a/src/pkl-insn.def
+++ b/src/pkl-insn.def
@@ -352,6 +352,7 @@ PKL_DEF_INSN (PKL_INSN_NOP, "v", "nop")
 PKL_DEF_INSN (PKL_INSN_NOTE, "v", "note")
 PKL_DEF_INSN (PKL_INSN_SIZ, "", "siz")
 PKL_DEF_INSN (PKL_INSN_STRACE, "n", "strace")
+PKL_DEF_INSN (PKL_INSN_RAND, "", "rand")
 
 /* Exceptions handling instructions.  */
 
diff --git a/src/pkl-lex.l b/src/pkl-lex.l
index da2902d..7743b30 100644
--- a/src/pkl-lex.l
+++ b/src/pkl-lex.l
@@ -171,6 +171,7 @@ D [0-9]
 "printf"       { return PRINTF; }
 "isa"          { return ISA; }
 "unmap"                { return UNMAP; }
+"__PKL_BUILTIN_RAND__" { return BUILTIN_RAND; }
 
 "uint<"         { return UINTCONSTR; }
 "int<"          { return INTCONSTR; }
diff --git a/src/pkl-rt.pk b/src/pkl-rt.pk
index c02f4a4..63cb81f 100644
--- a/src/pkl-rt.pk
+++ b/src/pkl-rt.pk
@@ -31,6 +31,10 @@
 defvar true = 1;
 defvar false = 0;
 
+/* Compiler built-ins. */
+
+defun rand = int<32>: __PKL_BUILTIN_RAND__;
+
 /* Exceptions.
 
    XXX: use an enumeration?
diff --git a/src/pkl-tab.y b/src/pkl-tab.y
index 6d875f2..99c406e 100644
--- a/src/pkl-tab.y
+++ b/src/pkl-tab.y
@@ -238,6 +238,7 @@ pkl_register_dummies (struct pkl_parser *parser, int n)
 %token PRINT
 %token PRINTF
 %token UNMAP
+%token BUILTIN_RAND;
 
 /* ATTRIBUTE operator.  */
 
@@ -1293,6 +1294,15 @@ comp_stmt:
               /* Pop the frame pushed by the `pushlevel' above.  */
               pkl_parser->env = pkl_env_pop_frame (pkl_parser->env);
             }
+        | pushlevel BUILTIN_RAND
+        {
+          $$ = pkl_ast_make_builtin (pkl_parser->ast,
+                                     PKL_AST_BUILTIN_RAND);
+          PKL_AST_LOC ($$) = @$;
+          
+          /* Pop the frame pushed by the `pushlevel' above.  */
+          pkl_parser->env = pkl_env_pop_frame (pkl_parser->env);
+        }
         ;
 
 stmt_decl_list:
diff --git a/src/pvm.jitter b/src/pvm.jitter
index 8cf5a67..3ab6df7 100644
--- a/src/pvm.jitter
+++ b/src/pvm.jitter
@@ -65,6 +65,7 @@ wrapped-functions
   ios_read_int
   ios_read_uint
   ios_read_string
+  random
 end
 
 #wrapped-globals
@@ -716,6 +717,12 @@ instruction strace (?n) # ( -- )
   end
 end
 
+instruction rand () # ( -- INT )
+  code
+    JITTER_PUSH_STACK (pvm_make_int (random (), 32));
+  end
+end
+
 
 
 ## Stack manipulation instructions
diff --git a/testsuite/poke.pkl/rand-1.pk b/testsuite/poke.pkl/rand-1.pk
new file mode 100644
index 0000000..5fbe88b
--- /dev/null
+++ b/testsuite/poke.pkl/rand-1.pk
@@ -0,0 +1,4 @@
+/* { dg-do run } */
+
+defvar random_number = rand;
+



reply via email to

[Prev in Thread] Current Thread [Next in Thread]