language

some fools attempt at an interpreted language
Log | Files | Refs | README

pc.h (1886B)


      1 /* `pc.h` - Program counter
      2  */
      3 
      4 #ifndef PC_H
      5 #define PC_H
      6 
      7 #define PC_RETURN_DEPTH 0xFFFF
      8 
      9 #include <stdlib.h>
     10 #include <stdio.h>
     11 
     12 #include "is.h"
     13 #include "fh.h"
     14 #include "bc.h"
     15 #include "helper.h"
     16 
     17 typedef unsigned long pc_addr;
     18 
     19 /* Address stack structure
     20  */
     21 typedef struct pc_addr_stk {
     22 	long     size;           // Capacity of stack
     23 	long     ptr;            // Stack pointer
     24 	pc_addr* addresses;      // Stack data
     25 } pc_addr_stk;
     26 
     27 /* Program counter structure
     28  */
     29 typedef struct pc_t {
     30 	pc_addr      limit;   // End of program
     31 	pc_addr      address; // Current address
     32 	pc_addr_stk* stk;     // Address stack
     33 	bc_cont*     line;    // Current instruction
     34 	bc_t*        bc;      // Bytecode container instance
     35 } pc_t;
     36 
     37 /* Initalizes program counter, returns pc_t* instance
     38  * char* - filename of file containing bytecode
     39  */
     40 pc_t* pc_new(char*);
     41 
     42 /* Creates a new address stack.
     43  *  ns_addr - inital address
     44  */
     45 pc_addr_stk* pc_addr_stk_new(ns_addr);
     46 
     47 /* Frees memory assosiated with pc_t* instance
     48  */
     49 void pc_del(pc_t*);
     50 
     51 /* Frees memory assosiated with pc_addr_stk* instance
     52  */
     53 void pc_addr_stk_del(pc_addr_stk*);
     54 
     55 /* Updates current insturction
     56  *
     57  * When called, pc_t->line will reflect pc_t->bc[pc_t->address]
     58  */
     59 void pc_update(pc_t*);
     60 
     61 /* Increment program counter by +-addr
     62  *  pc_t*   - Program counter instance
     63  *  pc_addr - Address to increment by
     64  */
     65 void pc_inc(pc_t*, pc_addr);
     66 
     67 /* Branch to specified address.
     68  *  pc_t*   - Program counter instance
     69  *  pc_addr - Address to branch to
     70  */
     71 void pc_branch(pc_t*, pc_addr);
     72 
     73 /* Return from previous branch.
     74  *  pc_t* - Program counter instance
     75  */
     76 void pc_return(pc_t*);
     77 
     78 /* Simply goto that address
     79  *  pc_t*   - Program counter instance
     80  *  pc_addr - Address to go to
     81  */
     82 void pc_goto(pc_t*, pc_addr);
     83 
     84 /* For now, a simple function that tests if the address is in range
     85  */
     86 int pc_safe(pc_t* pc);
     87 
     88 #endif // PC_H