An initial import of the teyjus source code in the C runtime for GF. The two runtime are still not connected but the source code compiles.

This commit is contained in:
kr.angelov
2012-07-31 15:16:04 +00:00
parent 314662dd09
commit 83b321d862
76 changed files with 23808 additions and 3 deletions
+28
View File
@@ -0,0 +1,28 @@
all: instrformats/gen pervasives/gen
instrformats/gen: instrformats/y.tab.o instrformats/lex.yy.o \
instrformats/instrgen-c.o instrformats/instrgen-ocaml.o \
util/util.o
gcc -o instrformats/gen $^
pervasives/gen: pervasives/y.tab.o pervasives/lex.yy.o \
pervasives/ccode.o pervasives/ocamlcode.o \
pervasives/pervgen-c.o pervasives/pervgen-ocaml.o \
pervasives/types.o pervasives/op.o \
util/util.o
gcc -o pervasives/gen $^
.o : .c
gcc -c -o $@ $^
instrformats/y.tab.c instrformats/y.tab.h : instrformats/instrformats.y
yacc -o instrformats/y.tab.c --defines=instrformats/y.tab.h instrformats/instrformats.y
instrformats/lex.yy.c : instrformats/instrformats.l instrformats/y.tab.h
flex -o instrformats/lex.yy.c instrformats/instrformats.l
pervasives/y.tab.c pervasives/y.tab.h : pervasives/pervasives.y
yacc -o pervasives/y.tab.c --defines=pervasives/y.tab.h pervasives/pervasives.y
pervasives/lex.yy.c : pervasives/pervasives.l pervasives/y.tab.h
flex -o pervasives/lex.yy.c pervasives/pervasives.l
@@ -0,0 +1,69 @@
%{
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
// Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#include "y.tab.h"
#include <string.h>
#include <stdlib.h>
static int commentLev = 0;
%}
LETTER [A-Za-z]
DIGIT [0-9]
SYMBOL "_"|"+"|"-"|"*"|"/"|"!"|"~"|"@"|"$"|"%"|"^"|"&"|"*"|"<"|">"|"="|"'"|":"|","
ID ({LETTER}|{SYMBOL})({LETTER}|{DIGIT}|{SYMBOL})*
NUM {DIGIT}+
WSPACE [ \t]+
STRING [^*/]+
STRING2 [^}]+
%x COMMENT COMMENT2 INCLUDE
%%
<INITIAL,COMMENT>"\n" {continue; }
<INITIAL>";" {return SEMICOLON; }
<INITIAL>"[" {return LBRACKET; }
<INITIAL>"]" {return RBRACKET; }
<INITIAL>"OPERAND TYPES" {return OPTYPES; }
<INITIAL>"OPCODE" {return OPCODE; }
<INITIAL>"INSTR CATEGORY" {return INSTRCAT; }
<INITIAL>"MAX OPERAND" {return MAXOPERAND; }
<INITIAL>"CALL_I1_LEN" {return CALL_I1_LEN; }
<INITIAL>"INSTRUCTIONS" {return INSTRUCTIONS; }
<INITIAL>{WSPACE} {continue; }
<INITIAL>"/%" {commentLev = 1; BEGIN(COMMENT); continue; }
<INITIAL>"/*" {BEGIN(COMMENT2); continue; }
<INITIAL>"{" {BEGIN(INCLUDE); continue; }
<INITIAL>{ID} {yylval.name = strdup(yytext); return ID; }
<INITIAL>{NUM} {yylval.isval.ival = atoi(yytext);
yylval.isval.sval = strdup(yytext);
return NUM; }
<COMMENT2>"*/" {BEGIN(INITIAL); continue; }
<COMMENT2>{STRING} {yylval.text = strdup(yytext); return STRING; }
<COMMENT>[^%/\n]+ {continue; }
<COMMENT>"/%" {commentLev++; continue; }
<COMMENT>"%/" {commentLev--;
if (!commentLev) BEGIN(INITIAL); continue; }
<INCLUDE>"}" {BEGIN(INITIAL); continue; }
<INCLUDE>{STRING2} {yylval.text = strdup(yytext); return STRING2; }
. {return ERROR; }
@@ -0,0 +1,283 @@
%{
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
// Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include "instrgen-c.h"
#include "instrgen-ocaml.h"
#include "../util/util.h"
extern int yylex();
int yywrap() {return 1;}
void yyerror(const char* str)
{
printf("%s\n", str);
}
%}
%union{
char* name;
char* text;
struct
{
int ival;
char* sval;
} isval;
}
%token OPTYPES INSTRCAT INSTRUCTIONS OPCODE MAXOPERAND
CALL_I1_LEN SEMICOLON ERROR LBRACKET RBRACKET
%token <name> ID
%token <isval> NUM
%token <text> STRING STRING2
%start instr_format
%type <name> operand_name operand_tname operand_type instr_name instr_cat
instr_head instr_length operand_comp_type
%type <text> comments compiler_include
%type <isval> max_operand opcode operand_size
%%
instr_format : compiler_include operands instrcats instructions
compiler_include : STRING2 { ocgenInclude($1);}
operands : OPTYPES operand_decs opcode_type
{ cgenOpsH(); ocgenOps();}
;
operand_decs : operand_dec operand_decs
| operand_dec_last
;
operand_dec : operand_name operand_tname operand_type operand_size operand_comp_type comments
{ cgenOpTypes($1, $2, $3, $6, 0);
ocgenOpType($1, $4.ival, $5);
}
| operand_name comments
{ cgenOpTypes($1, NULL, NULL, $2, 0); }
;
operand_dec_last : operand_name operand_tname operand_type operand_size operand_comp_type comments
{ cgenOpTypes($1, $2, $3, $6, 1);
ocgenOpType($1, $4.ival, $5);
}
| operand_name comments
{ cgenOpTypes($1, NULL, NULL, $2, 1); }
;
operand_name : ID { $$ = $1; }
;
operand_tname : ID { $$ = $1; }
;
operand_type : ID { $$ = $1; }
;
operand_comp_type : ID { $$ = $1; }
;
comments : STRING {$$ = $1; }
;
operand_size : NUM { $$ = $1; }
;
opcode_type : OPCODE ID operand_size
{ cgenOpCodeType($2);
ocgenOpCodeType($3.ival);}
;
instrcats : INSTRCAT max_operand instrcat_decs CALL_I1_LEN NUM
{ cgenInstrCatH($5.sval); cgenInstrCatC($2.sval);
ocgenInstrCat();
}
;
max_operand : MAXOPERAND NUM { $$ = $2; }
;
instrcat_decs : instrcat_dec instrcat_decs
| instrcat_dec_last
;
instrcat_dec : ID LBRACKET instr_format RBRACKET instr_lengths
{ cgenOneInstrCatH($1, 0); cgenOneInstrCatC($1, 0);
ocgenOneInstrCat($1);
}
;
instrcat_dec_last : ID LBRACKET instr_format RBRACKET instr_lengths
{ cgenOneInstrCatH($1, 1); cgenOneInstrCatC($1, 1);
ocgenOneInstrCat($1);
}
;
instr_format : oneOp instr_format
| lastOp
;
oneOp : ID { cgenInstrFormat($1, 0); ocgenInstrFormat($1); }
;
lastOp : ID { cgenInstrFormat($1, 1); ocgenInstrFormat($1); }
;
instr_lengths : instr_len_first SEMICOLON instr_lengths_rest
| instr_len_first
;
instr_lengths_rest : instr_len SEMICOLON instr_lengths_rest
| instr_len
;
instr_len_first : ID NUM
{cgenInstrLength($1, $2.sval);
ocgenInstrLength($1, $2.sval);}
;
instr_len : ID NUM { cgenInstrLength($1, $2.sval);}
;
instructions : instr_head instrs
{ cgenInstrH($1); cgenInstrC(); cgenSimDispatch();
ocgenInstr();
}
;
instr_head : INSTRUCTIONS NUM
{ cinitInstrC($2.ival);
cinitSimDispatch($2.ival);
$$ = $2.sval;
}
;
instrs : instr SEMICOLON instrs
| last_instr
;
instr : comments opcode instr_name instr_cat instr_length
{ cgenOneInstrH($1, $2.sval , $3);
cgenOneInstrC($2.ival, $3, $4, $5, 0);
cgenOneSimDispatch($2.ival, $3, 0);
ocgenOneInstr($2.sval, $3, $4, $5, 0);
}
| opcode instr_name instr_cat instr_length
{ cgenOneInstrH(NULL, $1.sval , $2);
cgenOneInstrC($1.ival, $2, $3, $4, 0);
cgenOneSimDispatch($1.ival, $2, 0);
ocgenOneInstr($1.sval, $2, $3, $4, 0);
}
;
last_instr : comments opcode instr_name instr_cat instr_length
{ cgenOneInstrH($1, $2.sval , $3);
cgenOneInstrC($2.ival, $3, $4, $5, 1);
cgenOneSimDispatch($2.ival, $3, 1);
ocgenOneInstr($2.sval, $3, $4, $5, 1);
}
| opcode instr_name instr_cat instr_length
{ cgenOneInstrH(NULL, $1.sval , $2);
cgenOneInstrC($1.ival, $2, $3, $4, 1);
cgenOneSimDispatch($1.ival, $2, 1);
ocgenOneInstr($1.sval, $2, $3, $4, 1);
}
;
opcode : NUM { $$ = $1; }
;
instr_name : ID { $$ = $1; }
;
instr_cat : ID { $$ = $1; }
;
instr_length : ID { $$ = $1; }
;
%%
extern FILE* yyin;
int main(argc, argv)
int argc;
char * argv[];
{
char * root = NULL;
int ret = 0;
if(argc <= 1)
{
if (sizeof(void*) == 8)
{
//printf("No input file specified; using 'instrformats_64.in'.\n");
yyin = UTIL_fopenR("instrformats_64.in");
}
else
{
//printf("No input file specified; using 'instrformats_32.in'.\n");
yyin = UTIL_fopenR("instrformats_32.in");
}
}
else
{
yyin = UTIL_fopenR(argv[1]);
}
if(argc > 2)
{
root = argv[2];
}
else
{
//printf("Teyjus source root directory not specified; using '../../'.\n");
root = "../../";
}
//printf("Generating instruction files...\n");
ret = yyparse();
UTIL_fclose(yyin);
if(ret != 0)
{
printf("Generation failed.\n");
return -1;
}
cspitCInstructionsH(root);
cspitCInstructionsC(root);
cspitSimDispatch(root);
//ocSpitInstructionMLI(root);
//ocSpitInstructionML(root);
//printf("Done.\n");
return 0;
}
@@ -0,0 +1,346 @@
{
type intref = int ref
type aconstant = Absyn.aconstant
type akind = Absyn.akind
}
OPERAND TYPES
P /* (1 byte) padding */
WP /* (1 word) padding */
R RegInd Byte 1 int /* argument register number */
E EnvInd Byte 1 int /* environment variable number */
N NextClauseInd Byte 1 int /* next clause number in impt or impl pt */
I1 OneByteInt Byte 1 int /* 1 byte natural number */
CE ClEnvInd Byte 1 int /* closure environment variable number */
SEG ImpSegInd Byte 1 int /* import segment index */
C CstIndex TwoBytes 2 aconstant /* constant symbol table index */
K KstIndex TwoBytes 2 akind /* kind symbol table index */
L CodeLabel CSpacePtr 4 intref /* code location */
I Int int 4 int /* integer immediate value */
F Float float 4 float /* floating point immediate value */
S Str DF_StrDataPtr 2 int /* string pointer */
MT ModTab MemPtr 2 int /* module table address */
IT ImplTab MemPtr 2 int /* impl table address */
HT HashTab MemPtr 2 int /* hash table address */
BVT BranchTab MemPtr 2 int /* branch table for bound var indexing */
X /* operand list terminator */
OPCODE Byte 1
INSTR CATEGORY
MAX OPERAND 8
X [P P P X X X X X]
X_LEN 4
RX [R P P X X X X X]
RX_LEN 4; RX_R 1
EX [E P P X X X X X]
EX_LEN 4; EX_E 1
I1X [I1 P P X X X X X]
I1X_LEN 4; I1X_I1 1
CX [P C X X X X X X]
CX_LEN 4; CX_C 2
KX [P K X X X X X X]
KX_LEN 4; KX_K 2
IX [P P P I X X X X]
IX_LEN 8; IX_I 4
FX [P P P F X X X X]
FX_LEN 8; FX_F 4
SX [P P P S X X X X]
SX_LEN 8; SX_S 4
MTX [P P P MT X X X X]
MTX_LEN 8; MTX_MT 4
LX [P P P L X X X X]
LX_LEN 8; LX_L 4
RRX [R R P X X X X X]
RRX_LEN 4; RRX_R1 1; RRX_R2 2
ERX [E R P X X X X X]
ERX_LEN 4; ERX_E 1; ERX_R 2
RCX [R C X X X X X X]
RCX_LEN 4; RCX_R 1; RCX_C 2
RIX [R P P I X X X X]
RIX_LEN 8; RIX_R 1; RIX_I 4
RFX [R P P F X X X X]
RFX_LEN 8; RFX_R 1; RFX_F 4
RSX [R P P S X X X X]
RSX_LEN 8; RSX_R 1; RSX_S 4
RI1X [R I1 P X X X X X]
RI1X_LEN 4; RI1X_R 1; RI1X_I1 2
RCEX [R CE P X X X X X]
RCEX_LEN 4; RCEX_R 1; RCEX_CE 2
ECEX [E CE P X X X X X]
ECEX_LEN 4; ECEX_E 1; ECEX_CE 2
CLX [P C L X X X X X]
CLX_LEN 8; CLX_C 2; CLX_L 4
RKX [R K X X X X X X]
RKX_LEN 4; RKX_R 1; RKX_K 2
ECX [E C X X X X X X]
ECX_LEN 4; ECX_E 1; ECX_C 2
I1ITX [I1 P P IT X X X X]
I1ITX_LEN 8; I1ITX_I1 1; I1ITX_IT 4
I1LX [I1 P P L X X X X]
I1LX_LEN 8; I1LX_I1 1; I1LX_L 4
SEGLX [SEG P P L X X X X]
SEGLX_LEN 8; SEGLX_SEG 1; SEGLX_L 4
I1LWPX [I1 P P L WP X X X]
I1LWPX_LEN 12; I1LWPX_I1 1; I1LWPX_L 4
I1NX [I1 N P X X X X X]
I1NX_LEN 4; I1NX_I1 1; I1NX_N 2
I1HTX [I1 P P HT X X X X]
I1HTX_LEN 8; I1HTX_I1 1; I1HTX_HT 4
I1BVTX [I1 P P BVT X X X X]
I1BVTX_LEN 8; I1BVTX_I1 1; I1BVTX_BVT 4
CWPX [P C WP X X X X X]
CWPX_LEN 8; CWPX_C 2
I1WPX [I1 P P WP X X X X]
I1WPX_LEN 8; I1WPX_I1 1
RRI1X [R R I1 X X X X X]
RRI1X_LEN 4; RRI1X_R1 1; RRI1X_R2 2; RRI1X_I1 3
RCLX [R C L X X X X X]
RCLX_LEN 8; RCLX_R 1; RCLX_C 2; RCLX_L 4
RCI1X [R C I1 P P P X X]
RCI1X_LEN 8; RCI1X_R 1; RCI1X_C 2; RCI1X_I1 4
SEGI1LX [SEG I1 P L X X X X]
SEGI1LX_LEN 8; SEGI1LX_SEG 1; SEGI1LX_I1 2; SEGI1LX_L 4
I1LLX [I1 P P L L X X X]
I1LLX_LEN 12; I1LLX_I1 1; I1LLX_L1 4; I1LLX_L2 8
NLLX [N P P L L X X X]
NLLX_LEN 12; NLLX_N 1; NLLX_L1 4; NLLX_L2 8
LLLLX [P P P L L L L X]
LLLLX_LEN 20; LLLLX_L1 4; LLLLX_L2 8; LLLLX_L3 12; LLLLX_L4 16
I1CWPX [I1 C WP X X X X X]
I1CWPX_LEN 8; I1CWPX_I1 1; I1CWPX_C 2
I1I1WPX [I1 I1 P WP X X X X]
I1I1WPX_LEN 8; I1I1WPX_I11 1; I1I1WPX_I12 2
CALL_I1_LEN 7
INSTRUCTIONS 145
/* Instructions for term unification and creation */
0 put_variable_t RRX RRX_LEN;
1 put_variable_p ERX ERX_LEN;
2 put_value_t RRX RRX_LEN;
3 put_value_p ERX ERX_LEN;
4 put_unsafe_value ERX ERX_LEN;
5 copy_value ERX ERX_LEN;
6 put_m_const RCX RCX_LEN;
7 put_p_const RCX RCX_LEN;
8 put_nil RX RX_LEN;
9 put_integer RIX RIX_LEN;
10 put_float RFX RFX_LEN;
11 put_string RSX RSX_LEN;
12 put_index RI1X RI1X_LEN;
13 put_app RRI1X RRI1X_LEN;
14 put_list RX RX_LEN;
15 put_lambda RRI1X RRI1X_LEN;
16 set_variable_t RX RX_LEN;
17 set_variable_te RX RX_LEN;
18 set_variable_p EX EX_LEN;
19 set_value_t RX RX_LEN;
20 set_value_p EX EX_LEN;
21 globalize_pt ERX ERX_LEN;
22 globalize_t RX RX_LEN;
23 set_m_const CX CX_LEN;
24 set_p_const CX CX_LEN;
25 set_nil X X_LEN;
26 set_integer IX IX_LEN;
27 set_float FX FX_LEN;
28 set_string SX SX_LEN;
29 set_index I1X I1X_LEN;
30 set_void I1X I1X_LEN;
31 deref RX RX_LEN;
32 set_lambda RI1X RI1X_LEN;
33 get_variable_t RRX RRX_LEN;
34 get_variable_p ERX ERX_LEN;
35 init_variable_t RCEX RCEX_LEN;
36 init_variable_p ECEX ECEX_LEN;
37 get_m_constant RCX RCX_LEN;
38 get_p_constant RCLX RCLX_LEN;
39 get_integer RIX RIX_LEN;
40 get_float RFX RFX_LEN;
41 get_string RSX RSX_LEN;
42 get_nil RX RX_LEN;
43 get_m_structure RCI1X RCI1X_LEN;
44 get_p_structure RCI1X RCI1X_LEN;
45 get_list RX RX_LEN;
46 unify_variable_t RX RX_LEN;
47 unify_variable_p EX EX_LEN;
48 unify_value_t RX RX_LEN;
49 unify_value_p EX EX_LEN;
50 unify_local_value_t RX RX_LEN;
51 unify_local_value_p EX EX_LEN;
52 unify_m_constant CX CX_LEN;
53 unify_p_constant CLX CLX_LEN;
54 unify_integer IX IX_LEN;
55 unify_float FX FX_LEN;
56 unify_string SX SX_LEN;
57 unify_nil X X_LEN;
58 unify_void I1X I1X_LEN;
/* Instructions for type unification and creation */
59 put_type_variable_t RRX RRX_LEN;
60 put_type_variable_p ERX ERX_LEN;
61 put_type_value_t RRX RRX_LEN;
62 put_type_value_p ERX ERX_LEN;
63 put_type_unsafe_value ERX ERX_LEN;
64 put_type_const RKX RKX_LEN;
65 put_type_structure RKX RKX_LEN;
66 put_type_arrow RX RX_LEN;
67 set_type_variable_t RX RX_LEN;
68 set_type_variable_p EX EX_LEN;
69 set_type_value_t RX RX_LEN;
70 set_type_value_p EX EX_LEN;
71 set_type_local_value_t RX RX_LEN;
72 set_type_local_value_p EX EX_LEN;
73 set_type_constant KX KX_LEN;
74 get_type_variable_t RRX RRX_LEN;
75 get_type_variable_p ERX ERX_LEN;
76 init_type_variable_t RCEX RCEX_LEN;
77 init_type_variable_p ECEX ECEX_LEN;
78 get_type_value_t RRX RRX_LEN;
79 get_type_value_p ERX ERX_LEN;
80 get_type_constant RKX RKX_LEN;
81 get_type_structure RKX RKX_LEN;
82 get_type_arrow RX RX_LEN;
83 unify_type_variable_t RX RX_LEN;
84 unify_type_variable_p EX EX_LEN;
85 unify_type_value_t RX RX_LEN;
86 unify_type_value_p EX EX_LEN;
87 unify_envty_value_t RX RX_LEN;
88 unify_envty_value_p EX EX_LEN;
89 unify_type_local_value_t RX RX_LEN;
90 unify_type_local_value_p EX EX_LEN;
91 unify_envty_local_value_t RX RX_LEN;
92 unify_envty_local_value_p EX EX_LEN;
93 unify_type_constant KX KX_LEN;
/* Instructions for handling higher-order aspects */
94 pattern_unify_t RRX RRX_LEN;
95 pattern_unify_p ERX ERX_LEN;
96 finish_unify X X_LEN;
97 head_normalize_t RX RX_LEN;
98 head_normalize_p EX EX_LEN;
/* Instructions for handling logical aspects */
99 incr_universe X X_LEN;
100 decr_universe X X_LEN;
101 set_univ_tag ECX ECX_LEN;
102 tag_exists_t RX RX_LEN;
103 tag_exists_p EX EX_LEN;
104 tag_variable EX EX_LEN;
105 push_impl_point I1ITX I1ITX_LEN;
106 pop_impl_point X X_LEN;
107 add_imports SEGI1LX SEGI1LX_LEN;
108 remove_imports SEGLX SEGLX_LEN;
109 push_import MTX MTX_LEN;
110 pop_imports I1X I1X_LEN;
/* Control Instructions */
111 allocate I1X I1X_LEN;
112 deallocate X X_LEN;
113 call I1LX I1LX_LEN;
114 call_name I1CWPX I1CWPX_LEN;
115 execute LX LX_LEN;
116 execute_name CWPX CWPX_LEN;
117 proceed X X_LEN;
/* Choice Instructions */
118 try_me_else I1LX I1LX_LEN;
119 retry_me_else I1LX I1LX_LEN;
120 trust_me I1WPX I1WPX_LEN;
121 try I1LX I1LX_LEN;
122 retry I1LX I1LX_LEN;
123 trust I1LWPX I1LWPX_LEN;
124 trust_ext I1NX I1NX_LEN;
125 try_else I1LLX I1LLX_LEN;
126 retry_else I1LLX I1LLX_LEN;
127 branch LX LX_LEN;
/* Indexing Instructions */
128 switch_on_term LLLLX LLLLX_LEN;
129 switch_on_constant I1HTX I1HTX_LEN;
130 switch_on_bvar I1BVTX I1BVTX_LEN;
131 switch_on_reg NLLX NLLX_LEN;
/* Cut Instructions */
132 neck_cut X X_LEN;
133 get_level EX EX_LEN;
134 put_level EX EX_LEN;
135 cut EX EX_LEN;
/* Miscellaneous Instructions */
136 call_builtin I1I1WPX I1I1WPX_LEN;
137 builtin I1X I1X_LEN;
138 stop X X_LEN;
139 halt X X_LEN;
140 fail X X_LEN;
/* new added */
141 create_type_variable EX EX_LEN;
/* resolved by the linker */
142 execute_link_only CWPX CWPX_LEN;
143 call_link_only I1CWPX I1CWPX_LEN;
144 put_variable_te RRX RRX_LEN
@@ -0,0 +1,346 @@
{
type intref = int ref
type aconstant = Absyn.aconstant
type akind = Absyn.akind
}
OPERAND TYPES
P /* (1 byte) padding */
WP /* (1 word) padding */
R RegInd Byte 1 int /* argument register number */
E EnvInd Byte 1 int /* environment variable number */
N NextClauseInd Byte 1 int /* next clause number in impt or impl pt */
I1 OneByteInt Byte 1 int /* 1 byte natural number */
CE ClEnvInd Byte 1 int /* closure environment variable number */
SEG ImpSegInd Byte 1 int /* import segment index */
C CstIndex TwoBytes 2 aconstant /* constant symbol table index */
K KstIndex TwoBytes 2 akind /* kind symbol table index */
L CodeLabel CSpacePtr 8 intref /* code location */
I Int int 4 int /* integer immediate value */
F Float float 4 float /* floating point immediate value */
S Str DF_StrDataPtr 2 int /* string pointer */
MT ModTab MemPtr 2 int /* module table address */
IT ImplTab MemPtr 2 int /* impl table address */
HT HashTab MemPtr 2 int /* hash table address */
BVT BranchTab MemPtr 2 int /* branch table for bound var indexing */
X /* operand list terminator */
OPCODE Byte 1
INSTR CATEGORY
MAX OPERAND 12
X [P P P P P P P X X X X X]
X_LEN 8
RX [R P P P P P P X X X X X]
RX_LEN 8; RX_R 1
EX [E P P P P P P X X X X X]
EX_LEN 8; EX_E 1
I1X [I1 P P P P P P X X X X X]
I1X_LEN 8; I1X_I1 1
CX [P C P P P P X X X X X X]
CX_LEN 8; CX_C 2
KX [P K P P P P X X X X X X]
KX_LEN 8; KX_K 2
IX [P P P P P P P I X X X X]
IX_LEN 16; IX_I 8
FX [P P P P P P P F X X X X]
FX_LEN 16; FX_F 8
SX [P P P P P P P S X X X X]
SX_LEN 16; SX_S 8
MTX [P P P P P P P MT X X X X]
MTX_LEN 16; MTX_MT 8
LX [P P P P P P P L X X X X]
LX_LEN 16; LX_L 8
RRX [R R P P P P P X X X X X]
RRX_LEN 8; RRX_R1 1; RRX_R2 2
ERX [E R P P P P P X X X X X]
ERX_LEN 8; ERX_E 1; ERX_R 2
RCX [R C P P P P X X X X X X]
RCX_LEN 8; RCX_R 1; RCX_C 2
RIX [R P P P P P P I X X X X]
RIX_LEN 16; RIX_R 1; RIX_I 8
RFX [R P P P P P P F X X X X]
RFX_LEN 16; RFX_R 1; RFX_F 8
RSX [R P P P P P P S X X X X]
RSX_LEN 16; RSX_R 1; RSX_S 8
RI1X [R I1 P P P P P X X X X X]
RI1X_LEN 8; RI1X_R 1; RI1X_I1 2
RCEX [R CE P P P P P X X X X X]
RCEX_LEN 8; RCEX_R 1; RCEX_CE 2
ECEX [E CE P P P P P X X X X X]
ECEX_LEN 8; ECEX_E 1; ECEX_CE 2
CLX [P C P P P P L X X X X X]
CLX_LEN 16; CLX_C 2; CLX_L 8
RKX [R K P P P P X X X X X X]
RKX_LEN 8; RKX_R 1; RKX_K 2
ECX [E C P P P P X X X X X X]
ECX_LEN 8; ECX_E 1; ECX_C 2
I1ITX [I1 P P P P P P IT X X X X]
I1ITX_LEN 16; I1ITX_I1 1; I1ITX_IT 8
I1LX [I1 P P P P P P L X X X X]
I1LX_LEN 16; I1LX_I1 1; I1LX_L 8
SEGLX [SEG P P P P P P L X X X X]
SEGLX_LEN 16; SEGLX_SEG 1; SEGLX_L 8
I1LWPX [I1 P P P P P P L WP X X X]
I1LWPX_LEN 24; I1LWPX_I1 1; I1LWPX_L 8
I1NX [I1 N P P P P P X X X X X]
I1NX_LEN 8; I1NX_I1 1; I1NX_N 2
I1HTX [I1 P P P P P P HT X X X X]
I1HTX_LEN 16; I1HTX_I1 1; I1HTX_HT 8
I1BVTX [I1 P P P P P P BVT X X X X]
I1BVTX_LEN 16; I1BVTX_I1 1; I1BVTX_BVT 8
CWPX [P C P P P P WP X X X X X]
CWPX_LEN 16; CWPX_C 2
I1WPX [I1 P P P P P P WP X X X X]
I1WPX_LEN 16; I1WPX_I1 1
RRI1X [R R I1 P P P P X X X X X]
RRI1X_LEN 8; RRI1X_R1 1; RRI1X_R2 2; RRI1X_I1 3
RCLX [R C P P P P L X X X X X]
RCLX_LEN 16; RCLX_R 1; RCLX_C 2; RCLX_L 8
RCI1X [R C I1 P P P X X X X X X]
RCI1X_LEN 8; RCI1X_R 1; RCI1X_C 2; RCI1X_I1 4
SEGI1LX [SEG I1 P P P P P L X X X X]
SEGI1LX_LEN 16; SEGI1LX_SEG 1; SEGI1LX_I1 2; SEGI1LX_L 8
I1LLX [I1 P P P P P P L L X X X]
I1LLX_LEN 24; I1LLX_I1 1; I1LLX_L1 8; I1LLX_L2 16
NLLX [N P P P P P P L L X X X]
NLLX_LEN 24; NLLX_N 1; NLLX_L1 8; NLLX_L2 16
LLLLX [P P P P P P P L L L L X]
LLLLX_LEN 40; LLLLX_L1 8; LLLLX_L2 16; LLLLX_L3 24; LLLLX_L4 32
I1CWPX [I1 C P P P P WP X X X X X]
I1CWPX_LEN 16; I1CWPX_I1 1; I1CWPX_C 2
I1I1WPX [I1 I1 P P P P P WP X X X X]
I1I1WPX_LEN 16; I1I1WPX_I11 1; I1I1WPX_I12 2
CALL_I1_LEN 15
INSTRUCTIONS 145
/* Instructions for term unification and creation */
0 put_variable_t RRX RRX_LEN;
1 put_variable_p ERX ERX_LEN;
2 put_value_t RRX RRX_LEN;
3 put_value_p ERX ERX_LEN;
4 put_unsafe_value ERX ERX_LEN;
5 copy_value ERX ERX_LEN;
6 put_m_const RCX RCX_LEN;
7 put_p_const RCX RCX_LEN;
8 put_nil RX RX_LEN;
9 put_integer RIX RIX_LEN;
10 put_float RFX RFX_LEN;
11 put_string RSX RSX_LEN;
12 put_index RI1X RI1X_LEN;
13 put_app RRI1X RRI1X_LEN;
14 put_list RX RX_LEN;
15 put_lambda RRI1X RRI1X_LEN;
16 set_variable_t RX RX_LEN;
17 set_variable_te RX RX_LEN;
18 set_variable_p EX EX_LEN;
19 set_value_t RX RX_LEN;
20 set_value_p EX EX_LEN;
21 globalize_pt ERX ERX_LEN;
22 globalize_t RX RX_LEN;
23 set_m_const CX CX_LEN;
24 set_p_const CX CX_LEN;
25 set_nil X X_LEN;
26 set_integer IX IX_LEN;
27 set_float FX FX_LEN;
28 set_string SX SX_LEN;
29 set_index I1X I1X_LEN;
30 set_void I1X I1X_LEN;
31 deref RX RX_LEN;
32 set_lambda RI1X RI1X_LEN;
33 get_variable_t RRX RRX_LEN;
34 get_variable_p ERX ERX_LEN;
35 init_variable_t RCEX RCEX_LEN;
36 init_variable_p ECEX ECEX_LEN;
37 get_m_constant RCX RCX_LEN;
38 get_p_constant RCLX RCLX_LEN;
39 get_integer RIX RIX_LEN;
40 get_float RFX RFX_LEN;
41 get_string RSX RSX_LEN;
42 get_nil RX RX_LEN;
43 get_m_structure RCI1X RCI1X_LEN;
44 get_p_structure RCI1X RCI1X_LEN;
45 get_list RX RX_LEN;
46 unify_variable_t RX RX_LEN;
47 unify_variable_p EX EX_LEN;
48 unify_value_t RX RX_LEN;
49 unify_value_p EX EX_LEN;
50 unify_local_value_t RX RX_LEN;
51 unify_local_value_p EX EX_LEN;
52 unify_m_constant CX CX_LEN;
53 unify_p_constant CLX CLX_LEN;
54 unify_integer IX IX_LEN;
55 unify_float FX FX_LEN;
56 unify_string SX SX_LEN;
57 unify_nil X X_LEN;
58 unify_void I1X I1X_LEN;
/* Instructions for type unification and creation */
59 put_type_variable_t RRX RRX_LEN;
60 put_type_variable_p ERX ERX_LEN;
61 put_type_value_t RRX RRX_LEN;
62 put_type_value_p ERX ERX_LEN;
63 put_type_unsafe_value ERX ERX_LEN;
64 put_type_const RKX RKX_LEN;
65 put_type_structure RKX RKX_LEN;
66 put_type_arrow RX RX_LEN;
67 set_type_variable_t RX RX_LEN;
68 set_type_variable_p EX EX_LEN;
69 set_type_value_t RX RX_LEN;
70 set_type_value_p EX EX_LEN;
71 set_type_local_value_t RX RX_LEN;
72 set_type_local_value_p EX EX_LEN;
73 set_type_constant KX KX_LEN;
74 get_type_variable_t RRX RRX_LEN;
75 get_type_variable_p ERX ERX_LEN;
76 init_type_variable_t RCEX RCEX_LEN;
77 init_type_variable_p ECEX ECEX_LEN;
78 get_type_value_t RRX RRX_LEN;
79 get_type_value_p ERX ERX_LEN;
80 get_type_constant RKX RKX_LEN;
81 get_type_structure RKX RKX_LEN;
82 get_type_arrow RX RX_LEN;
83 unify_type_variable_t RX RX_LEN;
84 unify_type_variable_p EX EX_LEN;
85 unify_type_value_t RX RX_LEN;
86 unify_type_value_p EX EX_LEN;
87 unify_envty_value_t RX RX_LEN;
88 unify_envty_value_p EX EX_LEN;
89 unify_type_local_value_t RX RX_LEN;
90 unify_type_local_value_p EX EX_LEN;
91 unify_envty_local_value_t RX RX_LEN;
92 unify_envty_local_value_p EX EX_LEN;
93 unify_type_constant KX KX_LEN;
/* Instructions for handling higher-order aspects */
94 pattern_unify_t RRX RRX_LEN;
95 pattern_unify_p ERX ERX_LEN;
96 finish_unify X X_LEN;
97 head_normalize_t RX RX_LEN;
98 head_normalize_p EX EX_LEN;
/* Instructions for handling logical aspects */
99 incr_universe X X_LEN;
100 decr_universe X X_LEN;
101 set_univ_tag ECX ECX_LEN;
102 tag_exists_t RX RX_LEN;
103 tag_exists_p EX EX_LEN;
104 tag_variable EX EX_LEN;
105 push_impl_point I1ITX I1ITX_LEN;
106 pop_impl_point X X_LEN;
107 add_imports SEGI1LX SEGI1LX_LEN;
108 remove_imports SEGLX SEGLX_LEN;
109 push_import MTX MTX_LEN;
110 pop_imports I1X I1X_LEN;
/* Control Instructions */
111 allocate I1X I1X_LEN;
112 deallocate X X_LEN;
113 call I1LX I1LX_LEN;
114 call_name I1CWPX I1CWPX_LEN;
115 execute LX LX_LEN;
116 execute_name CWPX CWPX_LEN;
117 proceed X X_LEN;
/* Choice Instructions */
118 try_me_else I1LX I1LX_LEN;
119 retry_me_else I1LX I1LX_LEN;
120 trust_me I1WPX I1WPX_LEN;
121 try I1LX I1LX_LEN;
122 retry I1LX I1LX_LEN;
123 trust I1LWPX I1LWPX_LEN;
124 trust_ext I1NX I1NX_LEN;
125 try_else I1LLX I1LLX_LEN;
126 retry_else I1LLX I1LLX_LEN;
127 branch LX LX_LEN;
/* Indexing Instructions */
128 switch_on_term LLLLX LLLLX_LEN;
129 switch_on_constant I1HTX I1HTX_LEN;
130 switch_on_bvar I1BVTX I1BVTX_LEN;
131 switch_on_reg NLLX NLLX_LEN;
/* Cut Instructions */
132 neck_cut X X_LEN;
133 get_level EX EX_LEN;
134 put_level EX EX_LEN;
135 cut EX EX_LEN;
/* Miscellaneous Instructions */
136 call_builtin I1I1WPX I1I1WPX_LEN;
137 builtin I1X I1X_LEN;
138 stop X X_LEN;
139 halt X X_LEN;
140 fail X X_LEN;
/* new added */
141 create_type_variable EX EX_LEN;
/* resolved by the linker */
142 execute_link_only CWPX CWPX_LEN;
143 call_link_only I1CWPX I1CWPX_LEN;
144 put_variable_te RRX RRX_LEN
@@ -0,0 +1,650 @@
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
// Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/******************************************************************************/
/* File instrgen-c.h. This files contains function declarations for generating*/
/* files instructions.h and instructions.c */
/******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "instrgen-c.h"
#include "../util/util.h"
#define INDENT1 " "
#define INDENT1_LEN 4u
#define PREFIX "INSTR_"
#define PREFIX_LEN 6u
#define CATPREFIX "INSTR_CAT_"
#define CATPREFIX_LEN 10u
#define DEF "#define "
#define DEF_LEN 8u
/*************************************************************************/
/* instructions.h */
/*************************************************************************/
#define COMMENTS_BEG_H \
"/****************************************************************************/\n/* File instructions.h. */ \n/* This file defines instruction operand types, instruction categories and */ \n/* instruction opcode. */ \n/****************************************************************************/ \n"
#define COMPDEF_BEG_H "#ifndef INSTRUCTIONS_H\n#define INSTRUCTIONS_H\n"
#define COMPDEF_END_H "#endif //INSTRUCTIONS_H\n"
#define INCLUDE_H \
"#include \"../simulator/mctypes.h\" //to be changed \n#include \"../simulator/dataformats.h\" //to be changed \n"
/* OPERAND TYPES */
#define OPTYPES_COMMENTS_H \
"/****************************************************************************/\n/* OPERAND TYPES */ \n/****************************************************************************/ \n\n"
#define OPTYPES_COMMENTS_H_LEN 300
#define OPERAND_TYPE_BEG \
"/* possible types of instruction operands */ \ntypedef enum INSTR_OperandType \n{\n"
#define OPERAND_TYPE_BEG_LEN 200
#define OPERAND_TYPE_END "} INSTR_OperandType;\n\n"
#define OPERAND_TYPE_END_LEN 30
static char* opTypes = NULL;
static char* opTypeMaps = NULL ;
void cgenOpTypes(char *name, char* typeName, char* types, char* comments,
int last)
{
char* myOpTypes = opTypes;
char* myOpTypeMaps = opTypeMaps;
size_t length;
size_t commentLen = comments ? strlen(comments) : 0u;
length = (opTypes ? strlen(opTypes) : 0u) + INDENT1_LEN + PREFIX_LEN +
strlen(name) + (comments ? strlen(comments) : 0u) + 30u;
opTypes = UTIL_mallocStr(length);
if (myOpTypes) { strcpy(opTypes, myOpTypes); strcat(opTypes, INDENT1); }
else strcpy(opTypes, INDENT1);
if (comments) {
strcat(opTypes, "//");
strcat(opTypes, comments);
strcat(opTypes, "\n");
strcat(opTypes, INDENT1);
}
strcat(opTypes, PREFIX);
strcat(opTypes, name);
if (last) strcat(opTypes, "\n");
else strcat(opTypes, ",\n");
if (myOpTypes) free(myOpTypes);
if (typeName) {
length = (opTypeMaps ? strlen(opTypeMaps) : 0u) + PREFIX_LEN +
strlen(types) + strlen(typeName) + 30u;
opTypeMaps = UTIL_mallocStr(length);
if (myOpTypeMaps) {
strcpy(opTypeMaps, myOpTypeMaps);
strcat(opTypeMaps, "typedef ");
} else strcpy(opTypeMaps, "typedef ");
strcat(opTypeMaps, types);
strcat(opTypeMaps, " ");
strcat(opTypeMaps, PREFIX);
strcat(opTypeMaps, typeName);
strcat(opTypeMaps, ";\n");
if (myOpTypeMaps) free(myOpTypeMaps);
}
}
#define OPTYPEMAP_COMMENT \
"/**************************************************************************/ \n/* Types for instruction operants */ \n/**************************************************************************/ \n\n"
#define OPTYPEMAP_COMMENT_LEN 300
static char *opcodeType = NULL;
void cgenOpCodeType(char* optype)
{
size_t length = PREFIX_LEN + strlen(optype) + 50;
opcodeType = UTIL_mallocStr(length);
strcpy(opcodeType, "typedef ");
strcat(opcodeType, optype);
strcat(opcodeType, " ");
strcat(opcodeType, PREFIX);
strcat(opcodeType, "OpCode;\n");
}
static char *opsH = NULL;
void cgenOpsH() //assume neither opTypes nor opTypeMaps is empty
{
size_t length = OPTYPES_COMMENTS_H_LEN + OPERAND_TYPE_BEG_LEN +
OPTYPEMAP_COMMENT_LEN + OPERAND_TYPE_END_LEN + strlen(opTypes) +
strlen(opTypeMaps) + strlen(opcodeType) + 50u;
opsH = UTIL_mallocStr(length);
strcpy(opsH, OPTYPES_COMMENTS_H);
strcat(opsH, OPERAND_TYPE_BEG);
strcat(opsH, opTypes);
strcat(opsH, OPERAND_TYPE_END);
strcat(opsH, OPTYPEMAP_COMMENT);
strcat(opsH, opcodeType);
strcat(opsH, opTypeMaps);
free(opTypes);
free(opcodeType);
free(opTypeMaps);
}
/* INSTRUCTION CATEGORIES */
#define INSTRCAT_COMMENTS_H \
"/***************************************************************************/ \n/* INSTRUCTION CATEGORIES */ \n/***************************************************************************/ \n"
#define INSTRCAT_COMMENTS_H_LEN 300
#define INSTRCAT_TYPE_BEG \
" /* The names of instruction categories no longer include padding bytes. */\n/* Thus we do not need to maintain two sets of names for different machine */ \n/* architectures. */ \ntypedef enum INSTR_InstrCategory \n{\n"
#define INSTRCAT_TYPE_BEG_LEN 350
#define INSTRCAT_TYPE_END "} INSTR_InstrCategory;\n\n"
#define INSTRCAT_TYPE_END_LEN 50
static char *instrcat_type = NULL;
static char *instrLen = NULL;
static char *oneInstrLen = NULL;
static int catNum = 0;
void cgenOneInstrCatH(char* name, int last)
{
char *myInstrCat = instrcat_type, *myInstrLen = instrLen;
size_t length = (myInstrCat ? strlen(myInstrCat) : 0u) + strlen(name) +
CATPREFIX_LEN + INDENT1_LEN + 10u;
instrcat_type = UTIL_mallocStr(length);
if (myInstrCat) {
strcpy(instrcat_type, myInstrCat);
strcat(instrcat_type, INDENT1);
} else strcpy(instrcat_type, INDENT1);
strcat(instrcat_type, CATPREFIX);
strcat(instrcat_type, name);
strcat(instrcat_type, " = ");
strcat(instrcat_type, UTIL_itoa(catNum));
if (last) strcat(instrcat_type, "\n");
else strcat(instrcat_type, ",\n");
catNum++;
if (myInstrCat) free(myInstrCat);
//assume oneInstrLen cannot be empty
length = (myInstrLen ? strlen(myInstrLen) : 0u) + strlen(name) +
CATPREFIX_LEN + 10u + strlen(oneInstrLen);
instrLen = UTIL_mallocStr(length);
if (myInstrLen) {
strcpy(instrLen, myInstrLen);
strcat(instrLen, "//");
} else strcpy(instrLen, "//");
strcat(instrLen, CATPREFIX);
strcat(instrLen, name);
strcat(instrLen, "\n");
strcat(instrLen, oneInstrLen);
free(oneInstrLen);
oneInstrLen = NULL;
if (myInstrLen) free(myInstrLen);
}
#define INSTRLEN_COMMENTS \
"/**************************************************************************/ \n/* Macros defines instruction lengths and distances between op code and */ \n/* operands. */ \n/* The assumption is that the op code occupies 1 byte. */ \n/**************************************************************************/ \n\n"
#define INSTRLEN_COMMENTS_LEN 450u
void cgenInstrLength(char* name, char* len)
{
char *myInstrLen = oneInstrLen;
size_t length = (myInstrLen ? strlen(myInstrLen) : 0u) + DEF_LEN + PREFIX_LEN
+ strlen(name) + strlen(len) + 10u;
oneInstrLen = UTIL_mallocStr(length);
if (myInstrLen) {
strcpy(oneInstrLen, myInstrLen);
strcat(oneInstrLen, DEF);
} else strcpy(oneInstrLen, DEF);
strcat(oneInstrLen, PREFIX);
strcat(oneInstrLen, name);
strcat(oneInstrLen, " ");
strcat(oneInstrLen, len);
strcat(oneInstrLen, "\n");
free(myInstrLen);
}
#define OPTYPE_TAB_H \
"/****************************************************************************/\n/* OPERAND TYPES TABLE */ \n/****************************************************************************/ \n \n//the operand types array in a given entry \nINSTR_OperandType* INSTR_operandTypes(INSTR_InstrCategory index); \n"
#define OPTYPE_TAB_H_LEN 500
static char *instrCatH = NULL;
void cgenInstrCatH(char* callI1Len)
{
size_t length = strlen(instrcat_type) + strlen(instrLen) +
INSTRCAT_TYPE_BEG_LEN + INSTRCAT_TYPE_END_LEN + INSTRCAT_COMMENTS_H_LEN
+ INSTRLEN_COMMENTS_LEN + OPTYPE_TAB_H_LEN + 160u;
instrCatH = UTIL_mallocStr(length);
strcpy(instrCatH, INSTRCAT_COMMENTS_H);
strcat(instrCatH, INSTRCAT_TYPE_BEG);
strcat(instrCatH, instrcat_type);
strcat(instrCatH, INSTRCAT_TYPE_END);
strcat(instrCatH, DEF);
strcat(instrCatH, "INSTR_NUM_INSTR_CATS ");
strcat(instrCatH, UTIL_itoa(catNum));
strcat(instrCatH, "\n\n");
strcat(instrCatH, DEF);
strcat(instrCatH, "INSTR_CALL_I1_LEN ");
strcat(instrCatH, callI1Len);
strcat(instrCatH, "\n\n");
strcat(instrCatH, INSTRLEN_COMMENTS);
strcat(instrCatH, instrLen);
strcat(instrCatH, "\n");
strcat(instrCatH, OPTYPE_TAB_H);
free(instrcat_type);
free(instrLen);
}
#define INSTR_COMMENTS_H \
"/***************************************************************************/ \n/* OPCODES OF INSTRUCTIONS */ \n/***************************************************************************/ \n"
#define INSTR_COMMENTS_H_LEN 250
static char* instrH = NULL;
void cgenOneInstrH(char* comments, char* opCode, char* instrName)
{
char* myInstrH = instrH;
size_t length = (myInstrH ? strlen(myInstrH) : 0u) + strlen(instrName) +
strlen(opCode) + DEF_LEN + CATPREFIX_LEN +
(comments ? strlen(comments) : 0u) + 10u;
instrH = UTIL_mallocStr(length);
if (myInstrH) {
strcpy(instrH, myInstrH);
if (comments) {
strcat(instrH, "//");
strcat(instrH, comments);
strcat(instrH, "\n");
strcat(instrH, DEF);
} else strcat(instrH, DEF);
} else {
if (comments) {
strcpy(instrH, "//");
strcat(instrH, comments);
strcat(instrH, "\n");
strcat(instrH, DEF);
} else strcpy(instrH, DEF);
}
strcat(instrH, instrName);
strcat(instrH, " ");
strcat(instrH, opCode);
strcat(instrH, "\n");
if (myInstrH) free(myInstrH);
}
#define INSTRTAB_H \
"/***************************************************************************/ \n/* INSTRUCTION INFORMATION TABLE */ \n/***************************************************************************/ \nINSTR_InstrCategory INSTR_instrType(int index); //instr type in a given entry \nchar* INSTR_instrName(int index); //instr name in a given entry \nint INSTR_instrSize(int index); //instr size in a given entry \n"
#define INSTRTAB_H_LEN 500
char* instrOpc = NULL;
void cgenInstrH(char* numInstr)
{
size_t length = INSTR_COMMENTS_H_LEN + strlen(instrH) + DEF_LEN +
strlen(numInstr) + INSTRTAB_H_LEN + 20u;
instrOpc = UTIL_mallocStr(length);
strcpy(instrOpc, INSTR_COMMENTS_H);
strcat(instrOpc, instrH);
strcat(instrOpc, "\n\n");
strcat(instrOpc, DEF);
strcat(instrOpc, "INSTR_NUM_INSTRS");
strcat(instrOpc, " ");
strcat(instrOpc, numInstr);
strcat(instrOpc, "\n\n");
strcat(instrOpc, INSTRTAB_H);
free(instrH);
}
/* dump instructions.h" */
void cspitCInstructionsH(char * root)
{
FILE* outFile;
char * filename = malloc(strlen(root) + 32);
strcpy(filename, root);
strcat(filename, "tables/instructions.h");
outFile = UTIL_fopenW(filename);
fprintf(outFile, "%s\n%s\n%s\n", COMMENTS_BEG_H, COMPDEF_BEG_H, INCLUDE_H);
fprintf(outFile, "%s\n", opsH);
fprintf(outFile, "%s\n", instrCatH);
fprintf(outFile, "%s\n", instrOpc);
fprintf(outFile, "%s\n", COMPDEF_END_H);
UTIL_fclose(outFile);
free(opsH);
free(instrCatH);
free(instrOpc);
free(filename);
}
/*************************************************************************/
/* instructions.c */
/*************************************************************************/
#define COMMENTS_BEG_C \
"/****************************************************************************/\n/* */ \n/* File instructions.c. This file defines the operand types table and */ \n/* the instruction information table. */ \n/* */ \n/****************************************************************************/ \n\n"
#define INCLUDE_C "#include \"instructions.h\"\n"
/*OPERAND TYPE TABLE */
#define OPTYPE_TAB_COMMENTS \
"/****************************************************************************/\n/* OPERAND TYPES TABLE */ \n/****************************************************************************/ \n\n"
#define OPTYPE_TAB_COMMENTS_LEN 250
#define MAX_OP_COMMENTS \
"/* Max number of operand that could be taken by instructions including the */\n/* padding bytes and one to terminate the list. (machine dependent) */ \n"
#define MAX_OP_COMMENTS_LEN 200
#define OPTYPE_TAB_TYPE \
"/* this array is indexed by instruction category. For each category, \n INSTR_operandTypeTab contains a string of values indicating the type \n of the operand at that position, terminated by INSTR_X. This \n information is useful when parsing instruction streams. */ \ntypedef INSTR_OperandType \n INSTR_OperandTypeTab[INSTR_NUM_INSTR_CATS][INSTR_MAX_OPERAND]; \n\n"
#define OPTYPE_TAB_TYPE_LEN 500
#define OPTYPE_TAB_BEG "INSTR_OperandTypeTab INSTR_operandTypeTable ={\n"
#define OPTYPE_TAB_BEG_LEN 80
#define OPTYPE_TAB_END "};\n\n"
#define OPTYPE_TAB_END_LEN 10
static char* optypeTabEntry = NULL;
void cgenInstrFormat(char* opType, int last)
{
char* mytabEntry = optypeTabEntry;
size_t length = (mytabEntry ? strlen(mytabEntry) : 0u) + PREFIX_LEN +
strlen(opType) + 5u;
optypeTabEntry = UTIL_mallocStr(length);
if (mytabEntry) {
strcpy(optypeTabEntry, mytabEntry);
strcat(optypeTabEntry, PREFIX);
} else strcpy(optypeTabEntry, PREFIX);
strcat(optypeTabEntry, opType);
if (!last) strcat(optypeTabEntry, ", ");
if (mytabEntry) free(mytabEntry);
}
static char* optypeTab = NULL;
//assume optypeEntry is not empty
void cgenOneInstrCatC(char* name, int last)
{
char* myoptypeTab = optypeTab;
size_t length = (myoptypeTab ? strlen(myoptypeTab) : 0u) + INDENT1_LEN*2 +
strlen(optypeTabEntry) + strlen(name) + 10u + CATPREFIX_LEN;
optypeTab = UTIL_mallocStr(length);
if (myoptypeTab) {
strcpy(optypeTab, myoptypeTab);
strcat(optypeTab, INDENT1);
} else strcpy(optypeTab, INDENT1);
strcat(optypeTab, "//");
strcat(optypeTab, CATPREFIX);
strcat(optypeTab, name);
strcat(optypeTab, "\n");
strcat(optypeTab, INDENT1);
strcat(optypeTab, "{");
strcat(optypeTab, optypeTabEntry);
if (last) strcat(optypeTab, "}\n");
else strcat(optypeTab, "},\n");
free(optypeTabEntry);
optypeTabEntry = NULL;
if (myoptypeTab) free(myoptypeTab);
}
#define OPTYPE_FUNC \
"INSTR_OperandType* INSTR_operandTypes(INSTR_InstrCategory index) \n{ \n return INSTR_operandTypeTable[index]; \n}\n"
#define OPTYPE_FUNC_LEN 250
static char* opTypeC = NULL;
void cgenInstrCatC(char* max_op){
size_t length = OPTYPE_TAB_COMMENTS_LEN + MAX_OP_COMMENTS_LEN +
OPTYPE_TAB_TYPE_LEN + OPTYPE_TAB_BEG_LEN + OPTYPE_TAB_END_LEN +
strlen(optypeTab) + OPTYPE_FUNC_LEN + strlen(max_op) + 100u;
opTypeC = UTIL_mallocStr(length);
strcpy(opTypeC, OPTYPE_TAB_COMMENTS);
strcat(opTypeC, MAX_OP_COMMENTS);
strcat(opTypeC, "#define INSTR_MAX_OPERAND ");
strcat(opTypeC, max_op);
strcat(opTypeC, "\n\n");
strcat(opTypeC, OPTYPE_TAB_TYPE);
strcat(opTypeC, OPTYPE_TAB_BEG);
strcat(opTypeC, optypeTab);
strcat(opTypeC, OPTYPE_TAB_END);
strcat(opTypeC, OPTYPE_FUNC);
}
//dynamic string array type
typedef struct StringArray
{
char **array;
unsigned int length;
} StringArray;
static StringArray instrTab;
void cinitInstrC(int numInstrs)
{
instrTab.length = numInstrs;
instrTab.array = (char**)UTIL_malloc(sizeof(char*)*numInstrs);
}
void cgenOneInstrC(int opcode, char* name, char* cat, char* len, int last)
{
size_t length = strlen(name) + strlen(cat) + strlen(len) + PREFIX_LEN
+ CATPREFIX_LEN + 20u + INDENT1_LEN ;
char* myText = UTIL_mallocStr(length);
strcpy(myText, INDENT1);
strcat(myText, "{\"");
strcat(myText, name);
strcat(myText, "\", ");
strcat(myText, CATPREFIX);
strcat(myText, cat);
strcat(myText, ", ");
strcat(myText, PREFIX);
strcat(myText, len);
if (last) strcat(myText, "}\n");
else strcat(myText, "},\n");
instrTab.array[opcode] = myText;
}
#define INSTR_TAB_C_COMMENTS \
"/****************************************************************************/\n/* INSTRUCTION INFORMATION TABLE */ \n/****************************************************************************/ \n"
#define INSTR_TAB_C_COMMENTS_LEN 250
#define INSTR_TAB_TYPE \
"typedef struct //entry of the instruction info table \n{ \n char* name; \n INSTR_InstrCategory type; \n int size; \n} INSTR_InstrInfoTab_; \n\ntypedef INSTR_InstrInfoTab_ INSTR_InstrInfoTab[INSTR_NUM_INSTRS]; \n\n"
#define INSTR_TAB_TYPE_LEN 600
#define INSTR_TAB_BEG "INSTR_InstrInfoTab INSTR_instrInfoTable ={\n"
#define INSTR_TAB_BEG_LEN 80
#define INSTR_TAB_END "};\n\n"
#define INSTR_TAB_END_LEN 10
#define INSTR_TAB_FUNC_C \
"/* Accessing functions */ \nINSTR_InstrCategory INSTR_instrType(int index) \n{ \n return (INSTR_instrInfoTable[index]).type; \n} \n\nchar* INSTR_instrName(int index) \n{ \n return (INSTR_instrInfoTable[index]).name; \n} \n\nint INSTR_instrSize(int index) \n{ \n return (INSTR_instrInfoTable[index]).size; \n}\n\n"
#define INSTR_TAB_FUNC_C_LEN 1000
static char* instrC = NULL;
void cgenInstrC()
{
size_t i, length;
char *myText = NULL, *myText2;
for (i = 0; i < instrTab.length; i++) {
if (instrTab.array[i]) {
length = (myText ? strlen(myText) : 0u) + strlen(instrTab.array[i]);
myText2 = UTIL_mallocStr(length + 100u);
if (myText) {
strcpy(myText2, myText);
strcat(myText2, instrTab.array[i]);
} else strcpy(myText2, instrTab.array[i]);
free(instrTab.array[i]);
free(myText);
myText = myText2;
}
}
free(instrTab.array);
length = INSTR_TAB_C_COMMENTS_LEN + INSTR_TAB_TYPE_LEN +
INSTR_TAB_BEG_LEN + INSTR_TAB_END_LEN + INSTR_TAB_FUNC_C_LEN +
strlen(myText);
instrC = UTIL_mallocStr(length);
strcpy(instrC, INSTR_TAB_C_COMMENTS);
strcat(instrC, INSTR_TAB_TYPE);
strcat(instrC, INSTR_TAB_BEG);
strcat(instrC, myText);
strcat(instrC, INSTR_TAB_END);
strcat(instrC, INSTR_TAB_FUNC_C);
free(myText);
}
/* dump instructions.c" */
void cspitCInstructionsC(char * root)
{
FILE* outFile;
char * filename = malloc(strlen(root) + 32);
strcpy(filename, root);
strcat(filename, "tables/instructions.c");
outFile = UTIL_fopenW(filename);
fprintf(outFile, "%s\n%s\n", COMMENTS_BEG_C, INCLUDE_C);
fprintf(outFile, "%s\n", opTypeC);
fprintf(outFile, "%s\n", instrC);
UTIL_fclose(outFile);
free(opTypeC);
free(instrC);
free(filename);
}
/* simdispatch.c */
#define SIMPREFIX "SINSTR_"
#define SIMPREFIX_LEN 7
#define SIMDIS_COMMENTS \
"/***************************************************************************/ \n/* */ \n/* File simdispatch.c. The instruction dispatch table used by the */ \n/* simulator is defined here as an array of function pointers, each of */ \n/* which refers to a function realizing a corresponding instruction. */ \n/* These functions are defined in the file ./siminstr.c. */ \n/***************************************************************************/ \n\n"
#define SIMDIS_COMMENTS_LEN 600
#define SIMDIS_INCLUDE \
"#include \"../tables/instructions.h\" //to be modified \n#include \"siminstr.h\" \n#include \"simdispatch.h\"\n\n"
#define SIMDIS_INCLUDE_LEN 250
#define SIMDIS_TAB_BEG \
"SDP_InstrFunctionPtr SDP_dispatchTable[INSTR_NUM_INSTRS] = {\n"
#define SIMDIS_TAB_BEG_LEN 80
#define SIMDIS_TAB_END "};\n"
#define SIMDIS_TAB_END_LEN 10
static StringArray dispatchTab;
void cinitSimDispatch(int size)
{
dispatchTab.length = size;
dispatchTab.array = (char**)UTIL_malloc(sizeof(char*)*size);
}
void cgenOneSimDispatch(int ind, char* instr, int last)
{
size_t length = strlen(instr) + SIMPREFIX_LEN + INDENT1_LEN + 10u;
char* myText = UTIL_mallocStr(length);
strcpy(myText, INDENT1);
strcat(myText, SIMPREFIX);
strcat(myText, instr);
if (last) strcat(myText, "\n");
else strcat(myText, ",\n");
dispatchTab.array[ind] = myText;
}
static char* dispatch = NULL;
void cgenSimDispatch()
{
size_t i, length;
char *myText = NULL, *myText2;
for(i = 0; i < dispatchTab.length; i++) {
if (dispatchTab.array[i]){
length = (myText ? strlen(myText) : 0)+strlen(dispatchTab.array[i]);
myText2 = UTIL_mallocStr(length);
if (myText){
strcpy(myText2, myText);
strcat(myText2, dispatchTab.array[i]);
} else strcpy(myText2, dispatchTab.array[i]);
free(dispatchTab.array[i]);
free(myText);
myText = myText2;
}
}
free(dispatchTab.array);
length = SIMDIS_COMMENTS_LEN + SIMDIS_INCLUDE_LEN + SIMDIS_TAB_BEG_LEN
+ SIMDIS_TAB_BEG_LEN + SIMDIS_TAB_END_LEN + strlen(myText);
dispatch = UTIL_mallocStr(length);
strcpy(dispatch, SIMDIS_COMMENTS);
strcat(dispatch, SIMDIS_INCLUDE);
strcat(dispatch, SIMDIS_TAB_BEG);
strcat(dispatch, myText);
strcat(dispatch, SIMDIS_TAB_END);
free(myText);
}
void cspitSimDispatch(char * root)
{
FILE* outFile;
char * filename = malloc(strlen(root) + 32);
strcpy(filename, root);
strcat(filename, "simulator/simdispatch.c");
outFile = UTIL_fopenW(filename);
fprintf(outFile, "%s\n", dispatch);
free(dispatch);
UTIL_fclose(outFile);
free(filename);
}
@@ -0,0 +1,70 @@
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
// Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/******************************************************************************/
/* File instrgen-c.h. This files contains function declarations for generating*/
/* files instructions.h and instructions.c */
/******************************************************************************/
/* instructions.h */
/* generating operand types */
void cgenOpTypes(char *name, char* typeName, char* types, char* comments,
int last);
void cgenOpCodeType(char* optype);
void cgenOpsH();
void cgenInstrCatH(char* callI1Len);
void cgenOneInstrCatH(char* name, int last);
void cgenInstrLength(char* name, char* length);
void cgenInstrH(char* numInstr);
void cgenOneInstrH(char* comments, char* opcode, char* name);
/* dump instructions.h" */
void cspitCInstructionsH(char * root);
/* instructions.c */
void cgenInstrFormat(char* opType, int last);
void cgenOneInstrCatC(char* name, int last);
void cgenInstrCatC(char* max_op);
void cinitInstrC(int numInstr);
void cgenOneInstrC(int opcode, char* name, char* cat, char* len, int last);
void cgenInstrC();
/* dump instructions.c" */
void cspitCInstructionsC(char * root);
/* simdispatch.c */
void cinitSimDispatch(int size);
void cgenOneSimDispatch(int ind, char* instr, int last);
void cgenSimDispatch();
void cspitSimDispatch(char * root);
@@ -0,0 +1,841 @@
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
// Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/*************************************************************************/
/* functions for generating ocaml instr.mli and instr.ml */
/*************************************************************************/
#include "../util/util.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
static char* addLine(char* str, char* addOn)
{
size_t length = (str ? strlen(str) : 0) + strlen(addOn) + 2;
char* newStr = UTIL_mallocStr(length);
if (str) {
strcpy(newStr, str);
strcat(newStr, addOn);
} else strcpy(newStr, addOn);
strcat(newStr, "\n\n");
return newStr;
}
static char* addStr(char* str, char* addOn)
{
size_t length = (str ? strlen(str) : 0) + strlen(addOn);
char* newStr = UTIL_mallocStr(length);
if (str) {
strcpy(newStr, str);
strcat(newStr, addOn);
} else strcpy(newStr, addOn);
return newStr;
}
/**********************************************************************/
/* auxiliary functions for making ocaml language constructs */
/**********************************************************************/
#define TYPE_SUFFIX "type"
#define SIZE_SUFFIX "Size"
#define WRITE_PREFIX "write"
#define READ_PREFIX "read"
#define DISPLAY_PREFIX "display"
#define INDENT " "
#define INDENT2 " "
#define WRITE "Bytecode.write"
#define READ "Bytecode.read"
#define DISPLAY "Bytecode.display"
#define INSCAT_PREFIX "inscat"
#define INS_PREFIX "Ins_"
static char* OC_mkVarDec(char* varName, char* varType)
{
size_t length = strlen(varName) + strlen(varType) + 10;
char* vardec = UTIL_mallocStr(length);
strcpy(vardec, "val ");
strcat(vardec, varName);
strcat(vardec, " : ");
strcat(vardec, varType);
strcat(vardec, "\n");
return vardec;
}
static char* OC_mkVarDef(char* varName, char* defs)
{
size_t length = strlen(varName) + strlen(defs) + 10;
char* vardef = UTIL_mallocStr(length);
strcpy(vardef, "let ");
strcat(vardef, varName);
strcat(vardef, " = ");
strcat(vardef, defs);
strcat(vardef, "\n");
return vardef;
}
static char* OC_mkTypeDec(char* typeName, char* defs)
{
size_t length = strlen(typeName) + strlen(defs) + 10;
char* typedec = UTIL_mallocStr(length);
strcpy(typedec, "type ");
strcat(typedec, typeName);
strcat(typedec, " = ");
strcat(typedec, defs);
strcat(typedec, "\n");
return typedec;
}
static char* OC_mkFunc(char* funcName, char* arg, char* body)
{
size_t length = strlen(funcName) + strlen(arg) + strlen(body) + 20;
char* func = UTIL_mallocStr(length);
strcpy(func, "let ");
strcat(func, funcName);
strcat(func, " ");
strcat(func, arg);
strcat(func, " = ");
strcat(func, body);
strcat(func, "\n");
return func;
}
static char* OC_mkCrossType(char *lop, char *rop)
{
size_t length = strlen(lop) + strlen(rop) + 5;
char* crossType = UTIL_mallocStr(length);
strcpy(crossType, lop);
strcat(crossType, " * ");
strcat(crossType, rop);
return crossType;
}
static char* OC_mkValueCtr(char* ctrName, char* types)
{
size_t length = strlen(ctrName) + strlen(types) + 10;
char* ctr = UTIL_mallocStr(length);
strcpy(ctr, ctrName);
strcat(ctr, " of ");
strcat(ctr, types);
return ctr;
}
static char* OC_mkDisjValueCtrs(char* prev, char* next)
{
size_t length = strlen(prev) + strlen(next) + 10;
char* ctr = UTIL_mallocStr(length);
strcpy(ctr, prev);
strcat(ctr, "\n");
strcat(ctr, INDENT);
strcat(ctr, "| ");
strcat(ctr, next);
return ctr;
}
static char* OC_mkFuncSeq(char* prev, char* new)
{
size_t length = strlen(prev) + strlen(new) + 20;
char* funcSeq = UTIL_mallocStr(length);
strcpy(funcSeq, prev);
strcat(funcSeq, "; ");
strcat(funcSeq, new);
return funcSeq;
}
static char* OC_mkArgList(char* prev, char* new)
{
size_t length = strlen(prev) + strlen(new) + 2;
char* args = UTIL_mallocStr(length);
strcpy(args, prev);
strcat(args, ", ");
strcat(args, new);
return args;
}
static char* OC_mkStrConcat(char* prev, char* new)
{
size_t length = strlen(prev) + strlen(new) + 20;
char* str = UTIL_mallocStr(length);
strcpy(str, "(");
strcat(str, prev);
strcat(str, ") ^ \", \" ^ (");
strcat(str, new);
strcat(str, ")");
return str;
}
static char* OC_mkArrow(char* left, char* right)
{
size_t length = strlen(left) + strlen(right) + 20;
char* arrow = UTIL_mallocStr(length);
strcpy(arrow, left);
strcat(arrow, " -> ");
strcat(arrow, right);
return arrow;
}
static char* OC_mkStructure(char* func, char* arg)
{
size_t length = strlen(func) + strlen(arg) + 5;
char* app = UTIL_mallocStr(length);
strcpy(app, func);
strcat(app, "(");
strcat(app, arg);
strcat(app, ")");
return app;
}
static char* OC_mkCond(char* cond, char* branch)
{
size_t length = strlen(cond) + strlen(branch) + 20;
char* str = UTIL_mallocStr(length);
strcpy(str, INDENT);
strcat(str, "if ");
strcat(str, cond);
strcat(str, " then ");
strcat(str, branch);
strcat(str, "\n");
strcat(str, INDENT);
strcat(str, "else");
return str;
}
static char* OC_mkLetIn(char* varName, char* def)
{
size_t length = strlen(varName) + strlen(def) + 20;
char* str = UTIL_mallocStr(length);
strcpy(str, INDENT);
strcat(str, "let ");
strcat(str, varName);
strcat(str, " = ");
strcat(str, def);
strcat(str, " in\n");
return str;
}
/**************************************************************************/
/* type definitions */
/**************************************************************************/
static char* typeDefs;
void ocgenInclude(char* include)
{
typeDefs = include;
}
/**************************************************************************/
/* operand types */
/**************************************************************************/
static char* opTypes;
static char* opSizesMLI;
static char* opSizesML;
static char* writeFuncs;
static char* readFuncs;
static char* ocgenWriteOpFunc(char* typeName, char* compType, int numBytes)
{
char* funcName = UTIL_appendStr(WRITE_PREFIX, typeName);
char* numBytesText = UTIL_itoa(numBytes);
char* arg = "arg";
char* funcBody1 = UTIL_mallocStr(strlen(WRITE) + strlen(compType) +
strlen(numBytesText));
char *funcBody2, *func;
strcpy(funcBody1, WRITE);
strcat(funcBody1, compType);
strcat(funcBody1, numBytesText); free(numBytesText);
funcBody2 = UTIL_appendStr(funcBody1, " arg"); free(funcBody1);
func = OC_mkFunc(funcName, arg, funcBody2);
free(funcName); free(funcBody2);
return func;
}
static char* ocgenReadOpFunc(char* typeName, char* compType, int numBytes)
{
char* funcName = UTIL_appendStr(READ_PREFIX, typeName);
char* numBytesText = UTIL_itoa(numBytes);
char* arg = "()";
char* funcBody1 = UTIL_mallocStr(strlen(READ) + strlen(compType) +
strlen(numBytesText));
char* funcBody2, *func;
strcpy(funcBody1, READ);
strcat(funcBody1, compType);
strcat(funcBody1, numBytesText); free(numBytesText);
funcBody2 = UTIL_appendStr(funcBody1, " ()"); free(funcBody1);
func = OC_mkFunc(funcName, arg, funcBody2);
free(funcName); free(funcBody2);
return func;
}
void ocgenOpType(char* typeName, int numBytes, char* compType)
{
/* generate type declarations*/
char* myName = UTIL_lowerCase(typeName);
char* myTypeName = UTIL_appendStr(myName, TYPE_SUFFIX);
char* myOpType = OC_mkTypeDec(myTypeName, compType);
char* myopTypes = addStr(opTypes, myOpType);
/* generate write functions */
char* func = ocgenWriteOpFunc(typeName, compType, numBytes);
char* myWriteFuncs = addStr(writeFuncs, func);
/* generate read functions */
char* readFunc = ocgenReadOpFunc(typeName, compType, numBytes);
char* myReadFuncs = addStr(readFuncs, readFunc);
/* generate sizes */
if (numBytes < 4) {
char* mySizeName = UTIL_appendStr(myName, SIZE_SUFFIX);
char* myOpSizeMLI = OC_mkVarDec(mySizeName, "int");
char* size = UTIL_itoa((int)(0 /*pow(2,(numBytes * 8))-1*/));
char* myOpSizeML = OC_mkVarDef(mySizeName, size);
char* myopSizesMLI = addStr(opSizesMLI, myOpSizeMLI);
char* myopSizesML = addStr(opSizesML, myOpSizeML);
free(mySizeName); free(size); free(myOpSizeMLI); free(myOpSizeML);
free(opSizesMLI); free(opSizesML);
opSizesMLI = myopSizesMLI;
opSizesML = myopSizesML;
}
free(myTypeName); free(myName);
free(opTypes); free(myOpType);
opTypes = myopTypes;
free(writeFuncs); free(func);
writeFuncs = myWriteFuncs;
free(readFuncs); free(readFunc);
readFuncs = myReadFuncs;
}
void ocgenOpCodeType(int numBytes)
{
char* mySizeName = UTIL_appendStr("opcode", SIZE_SUFFIX);
char* size = UTIL_itoa((int)(0 /*pow(2,(numBytes * 8))-1*/));
char* myOpCodeSizeMLI = OC_mkVarDec(mySizeName, "int");
char* myOpCodeSizeML = OC_mkVarDef(mySizeName, size);
char* myopSizeMLI = addLine(opSizesMLI, myOpCodeSizeMLI);
char* myopSizeML = addLine(opSizesML, myOpCodeSizeML);
char* func = ocgenWriteOpFunc("opcode", "int", numBytes);
char* myWriteFuncs = addLine(writeFuncs, func);
char* readFunc = ocgenReadOpFunc("opcode", "int", numBytes);
char* myReadFuncs = addLine(readFuncs, readFunc);
free(size); free(mySizeName);
free(opSizesMLI); free(myOpCodeSizeMLI);
free(opSizesML); free(myOpCodeSizeML);
free(writeFuncs); free(func);
free(readFuncs); free(readFunc);
opSizesMLI = myopSizeMLI;
opSizesML = myopSizeML;
writeFuncs = myWriteFuncs;
readFuncs = myReadFuncs;
}
static char* opMLI;
static char* opML;
void ocgenOps()
{
char* wordSizeName = "wordSize";
char* wordSizeMLI = OC_mkVarDec(wordSizeName, "int");
char* wordSize = UTIL_itoa(sizeof(void*));
char* wordSizeML = OC_mkVarDef(wordSizeName, wordSize);
char* text;
free(wordSize);
opMLI = addLine(opMLI, wordSizeMLI); free(wordSizeMLI);
text = addLine(opMLI, opSizesMLI); free(opMLI); free(opSizesMLI);
opMLI = addLine(text, opTypes); free(text);
opML = addLine(opML, wordSizeML); free(wordSizeML);
text = addLine(opML, opSizesML); free(opML); free(opSizesML);
opML = addLine(text, writeFuncs); free(text); free(writeFuncs);
text = addLine(opML, readFuncs); free(opML); free(readFuncs);
opML = addLine(text, opTypes); free(text); free(opTypes);
}
/****************************************************************************/
/* instruction categories */
/****************************************************************************/
static char* instrCatWriteFunc = NULL;
static char* instrCatReadFunc = NULL;
static char* instrCatDisplayFunc = NULL;
static char* instrCatType = NULL;
static int argInd = 1;
static char* argList = NULL;
void ocgenInstrFormat(char* opName)
{
char *myop, *myOpName, *myFuncName, *myArgInd, *myFuncCall, *myArg,
*myArgList, *myinstrCatType, *myinstrCatWriteFunc, *myReadBody,
*myinstrCatReadFunc, * myinstrCatDisplayFunc;
if (strcmp(opName, "P") == 0 || strcmp(opName, "WP") == 0 ||
strcmp(opName, "X") == 0) return;
//type declaration
myop = UTIL_lowerCase(opName);
myOpName = UTIL_appendStr(myop, TYPE_SUFFIX); free(myop);
if (instrCatType) {
myinstrCatType = OC_mkCrossType(instrCatType, myOpName);
free(instrCatType); free(myOpName);
instrCatType = myinstrCatType;
} else instrCatType = myOpName;
//argument
myArgInd = UTIL_itoa(argInd);
argInd++;
myArg = UTIL_appendStr("arg", myArgInd); free(myArgInd);
//argument list
if (argList) {
myArgList = OC_mkArgList(argList, myArg); free(argList);
argList = myArgList;
} else argList = myArg;
//write function
myFuncName = UTIL_appendStr(WRITE_PREFIX, opName);
myFuncCall = UTIL_mallocStr(strlen(myFuncName) + strlen(myArg) + 5);
strcpy(myFuncCall, myFuncName); free(myFuncName);
strcat(myFuncCall, " ");
strcat(myFuncCall, myArg);
if (instrCatWriteFunc) {
myinstrCatWriteFunc = OC_mkFuncSeq(instrCatWriteFunc, myFuncCall);
free(instrCatWriteFunc);
instrCatWriteFunc = myinstrCatWriteFunc;
free(myFuncCall);
} else instrCatWriteFunc = myFuncCall;
//read function
myFuncName = UTIL_appendStr(READ_PREFIX, opName);
myFuncCall = UTIL_mallocStr(strlen(myFuncName) + 5);
strcpy(myFuncCall, myFuncName); free(myFuncName);
strcat(myFuncCall, " ()");
myReadBody = OC_mkLetIn(myArg, myFuncCall); free(myFuncCall);
if (instrCatReadFunc) {
myinstrCatReadFunc = UTIL_appendStr(instrCatReadFunc, myReadBody);
free(instrCatReadFunc);
instrCatReadFunc = myinstrCatReadFunc;
free(myReadBody);
} else instrCatReadFunc = myReadBody;
//display function
myFuncName = UTIL_appendStr(DISPLAY, opName);
myFuncCall = UTIL_mallocStr(strlen(myFuncName) + strlen(myArg) + 5);
strcpy(myFuncCall, myFuncName); free(myFuncName);
strcat(myFuncCall, " ");
strcat(myFuncCall, myArg);
if (instrCatDisplayFunc) {
myinstrCatDisplayFunc = OC_mkStrConcat(instrCatDisplayFunc, myFuncCall);
free(instrCatDisplayFunc);
instrCatDisplayFunc = myinstrCatDisplayFunc;
free(myFuncCall);
} else instrCatDisplayFunc = myFuncCall;
}
static char* instrCatTypes;
static char* instrCatWriteFuncs;
static char* instrCatReadFuncs;
static char* instrCatDisplayFuncs;
void ocgenOneInstrCat(char* catName)
{
char *myCatName, *myInstrCatType, *myInstrCatTypes, *myArgs,
*myWriteFuncName, *myWriteFunc, *myInstrCatWriteFuncs,
*myReadFuncName, *myReadFunc, *myReadFuncBody, *myInstrCatReadFuncs,
*myDisplayFuncName, *myDisplayFunc, *myInstrCatDisplayFuncs, *myArgs2, *temp;
if (instrCatType) {
myCatName = UTIL_appendStr(INSCAT_PREFIX, catName);
myInstrCatType = OC_mkTypeDec(myCatName, instrCatType);
myInstrCatTypes = addStr(instrCatTypes, myInstrCatType);
myArgs = UTIL_mallocStr(strlen(argList) + 5);
strcpy(myArgs, "(");
strcat(myArgs, argList);
strcat(myArgs, ")");
/* write function */
myWriteFuncName = UTIL_appendStr(WRITE_PREFIX, catName);
myWriteFunc = OC_mkFunc(myWriteFuncName, myArgs, instrCatWriteFunc);
myInstrCatWriteFuncs = addStr(instrCatWriteFuncs, myWriteFunc);
/* read function */
myReadFuncName = UTIL_appendStr(READ_PREFIX, catName);
myArgs2 = UTIL_appendStr(INDENT, myArgs);
temp = UTIL_appendStr(instrCatReadFunc, myArgs2); free(myArgs2);
myReadFuncBody= UTIL_appendStr("\n", temp); free(temp);
myReadFunc = OC_mkFunc(myReadFuncName, "()", myReadFuncBody);
myInstrCatReadFuncs = addStr(instrCatReadFuncs, myReadFunc);
/* display function */
myDisplayFuncName = UTIL_appendStr(DISPLAY_PREFIX, catName);
myDisplayFunc = OC_mkFunc(myDisplayFuncName, myArgs, instrCatDisplayFunc);
myInstrCatDisplayFuncs = addStr(instrCatDisplayFuncs, myDisplayFunc);
free(myCatName); free(myInstrCatType);
free(instrCatType); free(instrCatTypes);
free(myWriteFuncName); free(myWriteFunc);
free(instrCatWriteFunc); free(instrCatWriteFuncs);
free(myReadFuncName); free(myReadFunc);
free(instrCatReadFunc); free(instrCatReadFuncs);
free(myDisplayFuncName); free(myDisplayFunc);
free(instrCatDisplayFunc); free(instrCatDisplayFuncs);
free(argList);
argList = NULL; argInd = 1;
instrCatType = NULL;
instrCatWriteFunc = NULL; instrCatReadFunc = NULL;
instrCatDisplayFunc = NULL;
instrCatTypes = myInstrCatTypes;
instrCatWriteFuncs = myInstrCatWriteFuncs;
instrCatReadFuncs = myInstrCatReadFuncs;
instrCatDisplayFuncs = myInstrCatDisplayFuncs;
}
}
static char* instrCatLength;
void ocgenInstrLength(char* varName, char* numBytes)
{
char* myVarName = UTIL_appendStr(INSCAT_PREFIX, varName);
char* varDef = OC_mkVarDef(myVarName, numBytes);
char* myInstrCatLength = addStr(instrCatLength, varDef);
free(myVarName); free(varDef); free(instrCatLength);
instrCatLength = myInstrCatLength;
}
static char* instrCatMLI;
static char* instrCatML;
void ocgenInstrCat()
{
char* text = instrCatTypes;
char* text2 = addLine(text, "\n");
instrCatMLI = text;
text = addLine(text2, instrCatWriteFuncs);
free(instrCatWriteFuncs); free(text2);
text2 = addLine(text, instrCatReadFuncs);
free(instrCatReadFuncs); free(text);
text = addLine(text2, instrCatDisplayFuncs);
free(instrCatDisplayFuncs); free(text2);
instrCatML = addLine(text, instrCatLength);
free(text); free(instrCatLength);
}
/****************************************************************************/
/* instructions */
/****************************************************************************/
#define GETSIZE_PREFIX "getSize_"
#define WRITEOPCODE "writeopcode "
static char* instructionTypes;
static char* insWriteFuncBody;
static char* insReadFuncBody;
static char* insDisplayFuncBody;
static char* insSizesDec;
static char* insSizesDef;
static void ocgenReadFuncBody(char* opcode, char* myInsName, char* myInsLength, char* insCat,
int last)
{
char *ins, *readArgs, *returnValue, *myReadFuncBody, *mycond, *tmp;
if (strcmp(insCat, "X") == 0) ins = myInsName;
else {
readArgs = UTIL_appendStr(READ_PREFIX, insCat);
ins = UTIL_mallocStr(strlen(readArgs) + strlen(myInsName) + 10);
strcpy(ins, myInsName);
strcat(ins, " (");
strcat(ins, readArgs);
strcat(ins, " ())");
free(readArgs);
}
returnValue = UTIL_mallocStr(strlen(ins) + strlen(myInsLength) + 5);
strcpy(returnValue, "(");
strcat(returnValue, ins);
strcat(returnValue, ", ");
strcat(returnValue, myInsLength);
strcat(returnValue, ")");
if (last) {
tmp = UTIL_appendStr(" ", returnValue); free(returnValue);
}else {
mycond = UTIL_mallocStr(strlen(opcode) + 10);
strcpy(mycond, "opcode = ");
strcat(mycond, opcode);
tmp = OC_mkCond(mycond, returnValue);
free(mycond); free(returnValue);
}
if (insReadFuncBody) {
myReadFuncBody = UTIL_appendStr(insReadFuncBody, tmp);
free(insReadFuncBody); free(tmp);
insReadFuncBody = myReadFuncBody;
} else insReadFuncBody = tmp;
}
static char* OC_mkWS(int size)
{
int i;
char* text;
if (size > 0) {
text = UTIL_mallocStr(size);
for (i = 0; i < size; i++) text[i]= ' ';
text[size] = '\0';
} else text = strdup(" ");
return text;
}
static void ocgenDisplayFuncBody(char* pattern, char* insName, char* insLength,
char* insCat)
{
char *displayargs, *funcBody, *myInsName, *ins, *returnValue, *insText,
*myDisplayFuncBody;
myInsName = UTIL_appendStr(insName, OC_mkWS(25u - strlen(insName)));
insText = UTIL_mallocStr(strlen(myInsName) + 5);
strcpy(insText, "\"");
strcat(insText, myInsName); free(myInsName);
strcat(insText, "\"");
if (strcmp(insCat, "X") == 0) ins = insText;
else {
displayargs = UTIL_appendStr(DISPLAY_PREFIX, insCat);
ins = UTIL_mallocStr(strlen(displayargs) + strlen(insText) + 10);
strcpy(ins, insText);
strcat(ins, "^ (");
strcat(ins, displayargs);
strcat(ins, " arg)");
free(displayargs); free(insText);
}
returnValue = UTIL_mallocStr(strlen(ins) + strlen(insLength) + 5);
strcpy(returnValue, "(");
strcat(returnValue, ins);
strcat(returnValue, ", ");
strcat(returnValue, insLength);
strcat(returnValue, ")");
funcBody = OC_mkArrow(pattern, returnValue);
free(returnValue);
if (insDisplayFuncBody) {
myDisplayFuncBody = OC_mkDisjValueCtrs(insDisplayFuncBody, funcBody);
free(insDisplayFuncBody); free(funcBody);
insDisplayFuncBody = myDisplayFuncBody;
} else {
insDisplayFuncBody = UTIL_appendStr(INDENT2, funcBody);
free(funcBody);
}
}
void ocgenOneInstr(char* opcode, char* insName, char* insCat, char* insLength,
int last)
{
char *myCatName, *myInsName, *myValueCtr, *myInstrTypes;
char *myInsSizeName, *myInsLength, *mySizeDef, *mySizeDec, *mySizeDefs,
*mySizeDecs;
char *myPattern, *myWriteOpCodeFunc, *myfuncBody, *myFunc, *myInsWriteFuncBody;
/* value constructors for type instruction */
myInsName = UTIL_appendStr(INS_PREFIX, insName);
if (strcmp(insCat, "X") == 0) {
myValueCtr = myInsName;
} else {
myCatName = UTIL_appendStr(INSCAT_PREFIX, insCat);
myValueCtr = OC_mkValueCtr(myInsName, myCatName); free(myCatName);
}
if (instructionTypes) {
myInstrTypes = OC_mkDisjValueCtrs(instructionTypes, myValueCtr);
free(instructionTypes);
instructionTypes = myInstrTypes;
} else instructionTypes = myValueCtr;
/* write function body */
myWriteOpCodeFunc = UTIL_appendStr(WRITEOPCODE, opcode);
if (strcmp(insCat, "X") == 0) {
myPattern = strdup(myInsName);
myfuncBody = myWriteOpCodeFunc;
} else {
char* myWriteArgsName = UTIL_appendStr(WRITE_PREFIX, insCat);
char* myWriteArgs = UTIL_mallocStr(strlen(myWriteArgsName) + 5);
myPattern = OC_mkStructure(myInsName, "arg");
strcpy(myWriteArgs, myWriteArgsName); free(myWriteArgsName);
strcat(myWriteArgs, " arg");
myfuncBody = OC_mkFuncSeq(myWriteOpCodeFunc, myWriteArgs);
free(myWriteArgs);
}
myFunc = OC_mkArrow(myPattern, myfuncBody);
free(myfuncBody);
if (insWriteFuncBody) {
myInsWriteFuncBody = OC_mkDisjValueCtrs(insWriteFuncBody, myFunc);
free(insWriteFuncBody); free(myFunc);
insWriteFuncBody = myInsWriteFuncBody;
} else {
insWriteFuncBody = UTIL_appendStr(INDENT2, myFunc);
free(myFunc);
}
/* instruction sizes */
myInsSizeName = UTIL_appendStr(GETSIZE_PREFIX, insName);
myInsLength = UTIL_appendStr(INSCAT_PREFIX, insLength);
mySizeDef = OC_mkVarDef(myInsSizeName, myInsLength);
mySizeDec = OC_mkVarDec(myInsSizeName, "int"); free(myInsSizeName);
mySizeDefs = addStr(insSizesDef, mySizeDef);
free(insSizesDef); free(mySizeDef);
mySizeDecs = addStr(insSizesDec, mySizeDec);
free(insSizesDec); free(mySizeDec);
insSizesDef = mySizeDefs;
insSizesDec = mySizeDecs;
ocgenReadFuncBody(opcode, myInsName, myInsLength, insCat, last);
ocgenDisplayFuncBody(myPattern, insName, myInsLength, insCat);
free(myInsName); free(myInsLength); free(myPattern);
}
#define INSTRTYPE_HEAD "type instruction = "
#define INSTWRITEFUNC_DEF_HEAD "let writeInstruction inst =\n match inst with\n"
#define INSTWRITEFUNC_DEC "val writeInstruction : instruction -> unit\n"
#define INSTREADFUNC_DEF_HEAD \
"let readInstruction getKindFunc getConstantFunc = \n Bytecode.setGetKindFn getKindFunc; \n Bytecode.setGetConstantFn getConstantFunc; \n let opcode = readopcode () in\n"
#define INSTREADFUNC_DEC \
"val readInstruction : \n(int -> int -> Absyn.akind option) -> (int -> int -> Absyn.aconstant option) ->\n(instruction * int)\n"
#define INSTDISPLAYFUNC_DEF_HEAD \
"let displayInstruction inst =\n match inst with\n"
#define INSTDISPLAYFUNC_DEC \
"val displayInstruction : instruction -> (string * int)\n"
static char* instrMLI;
static char* instrML;
void ocgenInstr()
{
char* text = UTIL_appendStr(INSTRTYPE_HEAD, instructionTypes);
char* text2 = UTIL_appendStr(text, "\n\n");
free(instructionTypes); free(text);
text = addLine(text2, insSizesDec); free(insSizesDec);
instrMLI = addStr(text, INSTWRITEFUNC_DEC); free(text);
text = addStr(instrMLI, INSTREADFUNC_DEC); free(instrMLI);
instrMLI = addStr(text, INSTDISPLAYFUNC_DEC); free(text);
text = addLine(text2, insSizesDef); free(text2); free(insSizesDef);
text2 = addStr(text, INSTWRITEFUNC_DEF_HEAD); free(text);
instrML = addStr(text2, insWriteFuncBody);
free(text2); free(insWriteFuncBody);
text = addStr(instrML, "\n\n"); free(instrML);
text2 = addStr(text, INSTREADFUNC_DEF_HEAD); free(text);
instrML = addStr(text2, insReadFuncBody);
free(text2); free(insReadFuncBody);
text = addStr(instrML, "\n\n"); free(instrML);
text2 = addStr(text, INSTDISPLAYFUNC_DEF_HEAD); free(text);
instrML = addStr(text2, insDisplayFuncBody);
free(text2); free(insDisplayFuncBody);
}
/****************************************************************************/
/* dump files */
/****************************************************************************/
/* dump files */
void ocSpitInstructionMLI(char * root)
{
FILE* outFile;
char * filename = malloc(strlen(root) + 32);
strcpy(filename, root);
strcat(filename, "compiler/instr.mli");
outFile = UTIL_fopenW(filename);
fprintf(outFile, typeDefs);
fprintf(outFile, opMLI); free(opMLI);
fprintf(outFile, instrCatMLI); free(instrCatMLI);
fprintf(outFile, "\n\n");
fprintf(outFile, instrMLI); free(instrMLI);
UTIL_fclose(outFile);
free(filename);
}
/* dump files */
void ocSpitInstructionML(char * root)
{
FILE* outFile;
char * filename = malloc(strlen(root) + 32);
strcpy(filename, root);
strcat(filename, "compiler/instr.ml");
outFile = UTIL_fopenW(filename);
fprintf(outFile, typeDefs); free(typeDefs);
fprintf(outFile, opML); free(opML);
fprintf(outFile, instrCatML); free(instrCatML);
fprintf(outFile, instrML); free(instrML);
UTIL_fclose(outFile);
free(filename);
}
@@ -0,0 +1,47 @@
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
// Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/*************************************************************************/
/* functions for generating ocaml instr.mli and instr.ml */
/*************************************************************************/
/* include */
void ocgenInclude(char* include);
/* operand types */
void ocgenOpType(char* typeName, int numBytes, char* compType);
void ocgenOpCodeType(int numBytes);
void ocgenOps();
/* instruction category */
void ocgenInstrFormat(char* opName);
void ocgenOneInstrCat(char* catName);
void ocgenInstrLength(char* varName, char* numBytes);
void ocgenInstrCat();
/* instructions */
void ocgenOneInstr(char* opcode, char* insName, char* insCat, char* insLength,
int last);
void ocgenInstr();
/* dump files */
void ocSpitInstructionMLI(char * root);
void ocSpitInstructionML(char * root);
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,104 @@
/* A Bison parser, made by GNU Bison 2.4.1. */
/* Skeleton interface for Bison's Yacc-like parsers in C
Copyright (C) 1984, 1989, 1990, 2000, 2001, 2002, 2003, 2004, 2005, 2006
Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
/* Tokens. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
/* Put the tokens into the symbol table, so that GDB and other debuggers
know about them. */
enum yytokentype {
OPTYPES = 258,
INSTRCAT = 259,
INSTRUCTIONS = 260,
OPCODE = 261,
MAXOPERAND = 262,
CALL_I1_LEN = 263,
SEMICOLON = 264,
ERROR = 265,
LBRACKET = 266,
RBRACKET = 267,
ID = 268,
NUM = 269,
STRING = 270,
STRING2 = 271
};
#endif
/* Tokens. */
#define OPTYPES 258
#define INSTRCAT 259
#define INSTRUCTIONS 260
#define OPCODE 261
#define MAXOPERAND 262
#define CALL_I1_LEN 263
#define SEMICOLON 264
#define ERROR 265
#define LBRACKET 266
#define RBRACKET 267
#define ID 268
#define NUM 269
#define STRING 270
#define STRING2 271
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef union YYSTYPE
{
/* Line 1676 of yacc.c */
#line 38 "instrformats/instrformats.y"
char* name;
char* text;
struct
{
int ival;
char* sval;
} isval;
/* Line 1676 of yacc.c */
#line 96 "instrformats/y.tab.h"
} YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
extern YYSTYPE yylval;
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,80 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#include "types.h"
#include "op.h"
/*******************************************************************/
/* commen structures */
/*******************************************************************/
/* // <comments> */
char* C_mkOneLineComments(char* comments);
/* // empty */
char* C_mkEmptyComments();
/*
//comments \n
PERV_<name>_INDEX = <indexNum>
*/
char* C_mkIndex(char* name, char* indexNum, char* comments);
/*
PERV_<name> = <indexNum>
*/
char* C_mkIndex2(char* name, char* indexNum);
/******************************************************************/
/* kind relevant components */
/******************************************************************/
char* C_mkNumKinds(char* num);
char* C_mkKindIndexType(char* body);
char* C_mkKindH(char* indexType, char* kindNum);
char* C_mkKindTabEntry(char* name, char* arity, char* comments);
char* C_mkKindTab(char* body);
char* C_mkKindC(char* kindTab);
/******************************************************************/
/* type skeleton relevant components */
/******************************************************************/
char* C_mkNumTySkels(char* num);
char* C_mkTySkelsH(char* numTySkels);
extern int C_totalSpace;
char* C_genTySkel(Type tyskel, char* comments);
char* C_mkTySkelTabInit(char* body, int space);
char* C_mkTySkelsC(char* tySkelTab);
/******************************************************************/
/* constant relevant components */
/******************************************************************/
char* C_mkNumConsts(char* num);
char* C_mkConstIndexType(char* body);
char* C_mkConstH(char* constIndexType, char* numConsts, char* property);
char* C_mkConstTabEntry(char* name, char* tesize, OP_Prec prec,
OP_Fixity fixity, char* tyskelInd, char* neededness,
char* comments);
char* C_mkConstTab(char* body);
char* C_mkConstC(char* constTab);
char* C_mkLSTypeDec(char* body);
char* C_mkLSRange(char* start, char* end);
char* C_mkPredRange(char* start, char* end);
char* C_mkFixedBegH();
char* C_mkFixedEndH();
char* C_mkFixedBegC();
char* C_mkFixedEndC();
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,152 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/***************************************************************************/
/* ocamlcode.h{c}. */
/* These files contain usful macros and auxiliary functions for generating */
/* the pervasive.mli and pervasive.ml. */
/* The parts of the ocaml files that are independent to the pervasives.in */
/* also reside here. */
/* The length macros for string macros defined here may be larger than the */
/* extra lengthes of corresponding strings. This is ok because space and */
/* time efficiency are not of concern in generating system files. */
/***************************************************************************/
#include "types.h"
#include "op.h"
#include "../util/util.h"
/***************************************************************************/
/* Functions for making program components */
/***************************************************************************/
/* let t = Table.add (Symbol.symbol "<name>") <varName> t in\n
*/
char* OC_mkTabEntry(char* name, char* varName);
/****************************************************************************/
/* functions for making pervasive kind relevant components */
/****************************************************************************/
/* k<name> */
char* OC_mkKVarName(char* name);
/* is<name> */
char* OC_mkIsKindFuncName(char* name);
/* val <kindVarName> : Absyn.akind \n*/
char* OC_mkKindVarDec(char* kindVarName);
/* val <funcName> : Absyn.akind -> bool */
char* OC_mkIsKindFuncDec(char* funcName);
/* let <funcName> tm = tm == <kindVarName> */
char* OC_mkIsKindFuncDef(char* funcName, char* kindVarName);
/* let <varName> = Absyn.PervasiveKind(Symbol.symbol "<kindName>",
(Some <arity>), ref offset, Errormsg.none)
*/
char* OC_mkKindVar(char* varName, char* kindName, char* arity, char* offset);
/* let buildPervasiveKinds =
function () ->\n <inits> <entries>\n <tabName>\n\n */
char* OC_mkBuildKTabFunc(char* entries);
/****************************************************************************/
/* functions for making pervasive type skeleton components */
/****************************************************************************/
/* generating code for type skeleton */
char* OC_genTySkel(Type tyskel);
/* tyskel<number> */
char* OC_mkTySkelVarName(char* number);
/* Type Skeleton variable definition:
let <varName> = Some(Absyn.Skeleton(<tySkel>, ref None, ref false))
*/
char* OC_mkTYSkelVar(char* varName, char* tySkel);
/* generate tyskels for overloaded constants */
char* OC_mkFixedTySkels(char* tySkels);
/****************************************************************************/
/* functions for making pervasive constants components */
/****************************************************************************/
/* <name>Constant */
char* OC_mkCVarName(char* name);
/* is<name> */
char* OC_mkIsConstFuncName(char* name);
/* val <constVarName> : Absyn.aconstant \n*/
char* OC_mkConstVarDec(char* constVarName);
/* Constant variable definition :
let <varName> = Absyn.Constant(Symbol.symbolAlias "<constName>" "<printName>",
ref <fixity>,
ref <prec>, ref false, ref false, ref false, ref false,
ref false, ref <typrev>, ref false, ref <tySkel>,
ref <tyenvsize>, ref (Some <neededness>), ref <codeInfo>,
ref <constantCat>, ref offset, Errormsg.none)
*/
char* OC_mkConstVar(char* constName, OP_Fixity fixity, OP_Prec prec,
UTIL_Bool typrev, char* tySkel, int tyenvsize,
int neededness, OP_Code codeInfo, UTIL_Bool reDef,
char* varName, char* offset, char* printName);
/* val <funcName> : Absyn.aconstant -> bool */
char* OC_mkIsConstFuncDec(char* funcName);
/* let <funcName> tm = tm == <constVarName> */
char* OC_mkIsConstFuncDef(char* funcName, char* constVarName);
/* generate fixed constants */
char* OC_mkGenericConstVar(char* varList);
/* generate fixed constants decs */
char* OC_mkGenericConstVarDec(char* decList);
/* generate fixed constants entry in buildConstant function */
char* OC_mkGenericConstTabEntry(char* entries);
/* let buildPervasiveKinds =
function () ->\n <inits> <entries>\n <tabName>\n\n */
char* OC_mkBuildCTabFunc(char* entries);
/* make generaic const is function decs */
char* OC_mkGenericConstFuncDecs(char* funcDefs);
/* make generaic const is function defs */
char* OC_mkGenericConstFuncDefs(char* funcDefs);
char* OC_mkCompare(char* name);
char* OC_mkOr(char* operandl, char* operandr);
char* OC_mkRegClobFunc(char* body);
char* OC_mkBackTrackFunc(char* body);
/*****************************************************************************/
/* functions for making the fixed part of pervasive.mli */
/*****************************************************************************/
/*
val pervasiveKinds : Absyn.akind Table.SymbolTable.t
val pervasiveConstants : Absyn.aconstant Table.SymbolTable.t
val pervasiveTypeAbbrevs : Absyn.atypeabbrev Table.SymbolTable.t
*/
char* OC_mkFixedMLI();
/*****************************************************************************/
/* functions for making the fixed part of pervasive.ml */
/*****************************************************************************/
/*
let pervasiveKinds = buildPervasiveKinds ()
let pervasiveConstants = buildPervasiveConstants ()
let pervasiveTypeAbbrevs = Table.SymbolTable.empty
*/
char* OC_mkFixedML();
@@ -0,0 +1,80 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <string.h>
#include "../util/util.h"
#include "op.h"
OP_Prec OP_mkPrecMin1()
{
OP_Prec prec;
prec.cat = OP_PREC;
prec.data.prec = -1;
return prec;
}
OP_Prec OP_mkPrecMin2()
{
OP_Prec prec;
prec.cat = OP_PREC;
prec.data.prec = -2;
return prec;
}
OP_Prec OP_mkPrec(int precedence)
{
OP_Prec prec;
prec.cat = OP_PREC;
prec.data.prec = precedence ;
return prec;
}
OP_Prec OP_mkPrecMax()
{
OP_Prec prec;
prec.cat = OP_PREC_NAME;
prec.data.name = strdup("MAX");
return prec;
}
int OP_precIsMax(OP_Prec prec)
{
if ((prec.cat == OP_PREC_NAME) && (strcmp(prec.data.name, "MAX") == 0))
return 1;
return 0;
}
OP_Code OP_mkCodeInfoNone()
{
return -1;
}
OP_Code OP_mkCodeInfo(int ind)
{
return ind;
}
int OP_codeInfoIsNone(OP_Code code)
{
return (code < 0);
}
@@ -0,0 +1,58 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#ifndef OP_H
#define OP_H
//fixity type
typedef enum {
OP_INFIX, OP_INFIXL, OP_INFIXR, OP_PREFIX, OP_PREFIXR, OP_POSTFIX,
OP_POSTFIXL, OP_NONE
} OP_Fixity;
typedef enum {
OP_PREC, OP_PREC_NAME
} OP_PrecTypeCat;
//precedence type
typedef struct
{
OP_PrecTypeCat cat;
union
{
int prec;
char* name;
} data;
} OP_Prec;
OP_Prec OP_mkPrecMin1();
OP_Prec OP_mkPrecMin2();
OP_Prec OP_mkPrec(int prec);
OP_Prec OP_mkPrecMax();
int OP_precIsMax(OP_Prec prec);
//code info type
typedef int OP_Code;
OP_Code OP_mkCodeInfoNone();
OP_Code OP_mkCodeInfo(int ind);
int OP_codeInfoIsNone(OP_Code code);
#endif
@@ -0,0 +1,404 @@
/% input file for pervasives files generation %/
/% section I: pervasive kinds %/
/% number of pervasive kinds %/
KIND 7
/% kind data table %/
/% index name index_name arity %/
/* int */
0 int int 0 ;;
/* real */
1 real real 0 ;;
/* bool */
2 o bool 0 ;;
/* string */
3 string string 0 ;;
/* list type constructor */
4 list list 1 ;;
/* in_stream */
5 in_stream instream 0 ;;
/* out_stream */
6 out_stream outstream 0
/% section II: pervasive constants and their type skeletons %/
/% section II.I : type skeletion and constant declarations %/
/% number of pervasive constants %/
CONST 94
/% number of type skeletons %/
TYPE SKEL 42
/% type skeleton and constant data table %/
/% type skeleton format:
[comments]
TYPE index type %/
/% constant format:
index name indname tesize neededness typerv redef prec fixity codeinfo
%/
/* A */
TYPE 0 #0
/* for unnamed universal constants (Note: tesize should be 0)*/
85 <constant> univ 0 0 0 TRUE FALSE 0 NOFIXITY NOCODE
;;
/* (list A) */
TYPE 1 (@ list 1 [#0])
/* nil */
89 nil nil 0 1 0 TRUE FALSE 0 NOFIXITY NOCODE
;;
/* A->(list A)->(list A) */
TYPE 2 #0 -> (@ list 1 [#0]) -> (@ list 1 [#0])
/* cons */
93 :: cons 0 1 0 TRUE FALSE 140 INFIXR NOCODE
;;
/* int */
TYPE 3 int
/* integer constant */
90 <int_constant> intc 0 0 0 TRUE FALSE 0 NOFIXITY NOCODE
;;
/* real */
TYPE 4 real
/* real constant */
91 <real_constant> realc 0 0 0 TRUE FALSE 0 NOFIXITY NOCODE
;;
/* string */
TYPE 5 string
/* string constant */
92 <str_constant> strc 0 0 0 TRUE FALSE 0 NOFIXITY NOCODE
;;
/* o (type of proposition)*/
TYPE 6 bool
/* true proposition */
4 true true 0 0 0 TRUE FALSE 0 NOFIXITY NOCODE
/* cut predicate */
5 ! cut 0 0 0 TRUE FALSE 0 NOFIXITY NOCODE
/* fail predicate */
6 fail fail 0 0 0 TRUE FALSE 0 NOFIXITY NOCODE
/* halt the system */
9 halt halt 0 0 0 TRUE FALSE 0 NOFIXITY NOCODE
/* return to top level */
10 stop stop 0 0 0 TRUE FALSE 0 NOFIXITY NOCODE
;;
/* int -> int */
TYPE 7 int -> int
/* unary minus on integers */
56 %i~ intuminus 0 0 0 TRUE FALSE MAX PREFIX NOCODE -
/* modulus */
61 mod mod 0 0 0 TRUE TRUE 160 INFIXL NOCODE
/* integer abs */
63 %iabs iabs 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE abs
;;
/* int -> int -> int */
TYPE 8 int -> int -> int
/* addition on integers */
57 %i+ intplus 0 0 0 TRUE FALSE 150 INFIXL NOCODE +
/* subtraction on integers */
58 %i- intminus 0 0 0 TRUE FALSE 150 INFIXL NOCODE -
/* mutiplication on integers */
59 %i* intmult 0 0 0 TRUE FALSE 160 INFIXL NOCODE *
/* integer division */
60 div intdiv 0 0 0 TRUE FALSE 160 INFIXL NOCODE
;;
/* int -> int -> o */
TYPE 9 int -> int -> bool
/* less than on integers */
19 %i< intlss 0 0 0 TRUE FALSE 130 INFIX 4 <
/* greater than on integers */
20 %i> intgrt 0 0 0 TRUE FALSE 130 INFIX 5 >
/* less than or eq on integers */
21 %i<= intleq 0 0 0 TRUE FALSE 130 INFIX 6 <=
/* greater than or eq on integers*/
22 %i>= intgeq 0 0 0 TRUE FALSE 130 INFIX 7 >=
/* time predicate */
51 time time 0 0 0 TRUE TRUE 0 NOFIXITY 36
;;
/* int -> real */
TYPE 10 int -> real
/* coercion to real */
62 int_to_real itor 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
;;
/* real -> int */
TYPE 11 real -> int
/* floor function */
74 floor floor 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* ceiling function */
75 ceil ceil 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* truncation */
76 truncate trunc 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
;;
/* real -> real */
TYPE 12 real -> real
/* unary minus on real */
64 %r~ realuminus 0 0 0 TRUE FALSE MAX PREFIX NOCODE -
/* square root */
69 sqrt sqrt 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* sine */
70 sin sin 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* cosine */
71 cos cos 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* arc tan */
72 arctan arctan 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* natural log */
73 ln log 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* real abs */
77 %rabs rabs 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE rabs
;;
/* real -> string */
TYPE 13 real -> string
/* real to string */
84 real_to_string rtos 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
;;
/* real -> real -> real */
TYPE 14 real -> real -> real
/* addition on reals */
65 %r+ realplus 0 0 0 TRUE FALSE 150 INFIXL NOCODE +
/* subtraction on reals */
66 %r- realminus 0 0 0 TRUE FALSE 150 INFIXL NOCODE -
/* multiplication on reals */
67 %r* realmult 0 0 0 TRUE FALSE 160 INFIXL NOCODE *
/* division */
68 / realdiv 0 0 0 TRUE FALSE 160 INFIXL NOCODE
;;
/* real -> real -> o */
TYPE 15 real -> real -> bool
/* less than in reals */
23 %r< reallss 0 0 0 TRUE FALSE 130 INFIX 8 <
/* greater than on reals */
24 %r> realgrt 0 0 0 TRUE FALSE 130 INFIX 9 >
/* less than or eq on reals */
25 %r<= realleq 0 0 0 TRUE FALSE 130 INFIX 10 <=
/* greater than or eq on reals */
26 %r>= realgeq 0 0 0 TRUE FALSE 130 INFIX 11 >=
;;
/* string -> int */
TYPE 16 string -> int
/* string length */
79 size slen 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* ord function */
81 string_to_int stoi 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
;;
/* int -> string */
TYPE 17 int -> string
/* chr function */
80 chr itochr 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* int to string */
83 int_to_string itostr 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
;;
/* string -> string -> string */
TYPE 18 string -> string -> string
/* string concatination */
78 ^ scat 0 0 0 TRUE TRUE 150 INFIXL NOCODE
;;
/* string -> string -> o */
TYPE 19 string -> string -> bool
/* less than on strings */
27 %s< strlss 0 0 0 TRUE TRUE 130 INFIX 12 <
/* greater than on strings */
28 %s> strgrt 0 0 0 TRUE TRUE 130 INFIX 13 >
/* less than or eq on strings */
29 %s<= strleq 0 0 0 TRUE TRUE 130 INFIX 14 <=
/* greater than or eq on strings */
30 %s>= strgeq 0 0 0 TRUE TRUE 130 INFIX 15 >=
/* getenv predicate; needed? */
49 getenv getenv 0 0 0 TRUE TRUE 0 NOFIXITY 34
;;
/* string -> int -> int -> string */
TYPE 20 string -> int -> int -> string
/* substring */
82 substring substr 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
;;
/* o -> o -> o */
TYPE 21 bool -> bool -> bool
/* logical and */
0 , and 0 0 0 TRUE FALSE 110 INFIXL NOCODE
/* logical or */
1 ; or 0 0 0 TRUE FALSE 100 INFIXL NOCODE
/* another logical and */
8 & ampand 0 0 0 TRUE FALSE 120 INFIXR NOCODE
/* Prolog if; needed? */
11 :- colondash 0 0 0 TRUE FALSE 0 INFIXL NOCODE
/* implication; needed? */
12 => impl 0 0 0 TRUE FALSE 130 INFIXR NOCODE
;;
/* (A -> o) -> o */
TYPE 22 (#0 -> bool) -> bool
/* existential quantifier */
2 sigma some 1 1 1 FALSE FALSE 0 NOFIXITY NOCODE
/* universal quantifier */
3 pi all 1 1 1 FALSE FALSE 0 NOFIXITY NOCODE
;;
/* A -> A -> o */
TYPE 23 #0 -> #0 -> bool
/* is */
16 is is 1 1 1 FALSE FALSE 130 INFIX 1
/* equality (unify) predicate */
18 = eq 1 1 1 FALSE FALSE 130 INFIX 3
;;
/* in_stream */
TYPE 24 instream
/* std_in */
86 std_in stdin 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
;;
/* out_stream */
TYPE 25 outstream
/* std_out */
87 std_out stdout 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
/* std_err */
88 std_err stderr 0 0 0 TRUE TRUE 0 NOFIXITY NOCODE
;;
/* string -> in_stream -> o */
TYPE 26 string -> instream -> bool
/* open_in */
31 open_in openin 0 0 0 TRUE TRUE 0 NOFIXITY 16
/* open_string */
36 open_string openstr 0 0 0 TRUE TRUE 0 NOFIXITY 21
;;
/* string -> out_stream -> o */
TYPE 27 string -> outstream -> bool
/* open_out */
32 open_out openout 0 0 0 TRUE TRUE 0 NOFIXITY 17
/* open_append */
33 open_append openapp 0 0 0 TRUE TRUE 0 NOFIXITY 18
;;
/* in_stream -> o */
TYPE 28 instream -> bool
/* close_in */
34 close_in closein 0 0 0 TRUE TRUE 0 NOFIXITY 19
/* eof */
41 eof eof 0 0 0 TRUE TRUE 0 NOFIXITY 26
;;
/* out_stream -> o */
TYPE 29 outstream -> bool
/* close_out */
35 close_out closeout 0 0 0 TRUE TRUE 0 NOFIXITY 20
/* flush */
42 flush flush 0 0 0 TRUE TRUE 0 NOFIXITY 27
;;
/* A -> string -> o */
TYPE 30 #0 -> string -> bool
/* term_to_string */
46 term_to_string termtostr 1 1 0 FALSE TRUE 0 NOFIXITY 31
;;
/* string -> A -> o */
TYPE 31 string -> #0 -> bool
/* string_to_term */
47 string_to_term strtoterm 1 1 1 FALSE TRUE 0 NOFIXITY 32
;;
/* out_stream -> string -> o */
TYPE 32 outstream -> string -> bool
/* output */
38 output output 0 0 0 TRUE TRUE 0 NOFIXITY 23
;;
/* in_stream -> int -> string -> o */
TYPE 33 instream -> int -> string -> bool
/* input */
37 input input 0 0 0 TRUE TRUE 0 NOFIXITY 22
;;
/* in_stream -> string -> o */
TYPE 34 instream -> string -> bool
/* input_line */
39 input_line inputline 0 0 0 TRUE TRUE 0 NOFIXITY 24
/* lookahead */
40 lookahead lookahead 0 0 0 TRUE TRUE 0 NOFIXITY 25
;;
/* string -> o */
TYPE 35 string -> bool
/* print */
43 print print 0 0 0 TRUE TRUE 0 NOFIXITY 28
;;
/* A -> o */
TYPE 36 #0 -> bool
/* read */
44 read read 1 1 1 FALSE TRUE 0 NOFIXITY 29
;;
/* out_stream -> A -> o */
TYPE 37 outstream -> #0 -> bool
/* printterm */
45 printterm printterm 1 1 0 FALSE TRUE 0 NOFIXITY 30
;;
/* in_stream -> A -> o */
TYPE 38 instream -> #0 -> bool
/* readterm */
48 readterm readterm 1 1 1 FALSE TRUE 0 NOFIXITY 33
;;
/* o -> o */
TYPE 39 bool -> bool
/* solve; used by code generator */
15 solve solve 0 0 0 TRUE FALSE 0 NOFIXITY 0
/* not */
17 not not 0 0 0 TRUE FALSE 0 NOFIXITY 2
;;
/* string -> int -> in_stream -> out_stream -> o */
TYPE 40 string -> int -> instream -> outstream -> bool
/* open_socket predicate */
50 open_socket opensocket 0 0 0 FALSE TRUE 0 NOFIXITY 35
;;
/* string -> int -> o */
TYPE 41 string -> int -> bool
/* system predicate */
52 system system 0 0 0 FALSE TRUE 0 NOFIXITY 37
/% pervasive constant classification %/
LOGIC SYMBOL 11
LS_START and LS_END stop
/% logic symbol types %/
0 and
1 or
2 some
3 all
4 l_true
5 cut
6 fail
7 eq
8 ampand
9 halt
10 stop
PRED SYMBOL 37
PRED_START solve PRED_END system
REGCL solve not getenv strtoterm readterm read is lookahead input inputline
BACKTRACK eq
@@ -0,0 +1,96 @@
%{
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#include "../util/util.h"
#include "op.h"
#include "types.h"
#include "y.tab.h"
#include <stdlib.h>
#include <string.h>
static int commentLev = 0;
%}
LETTER [A-Za-z]
DIGIT [0-9]
SYMBOL "_"|"+"|"-"|"*"|"/"|"!"|"~"|"@"|"$"|"%"|"^"|"&"|"*"|"<"|">"|"="|"'"|";"|":"|","
ID ({LETTER}|{SYMBOL})({LETTER}|{DIGIT}|{SYMBOL})*
NUM {DIGIT}+
WSPACE [ \t]+
STRING [^*/]+
%x COMMENT C_COMMENT
/* Some versions of lex require an explicit positions argument */
%p 10000
%%
<INITIAL,COMMENT>"\n" {continue; }
<INITIAL>"KIND" {return KIND; }
<INITIAL>"CONST" {return CONST; }
<INITIAL>"TYPE SKEL" {return TYSKEL; }
<INITIAL>"TYPE" {return TYPE; }
<INITIAL>"->" {return TYARROW; }
<INITIAL>"@" {return TYAPP; }
<INITIAL>"[" {return LBRACKET; }
<INITIAL>"]" {return RBRACKET; }
<INITIAL>"(" {return LPAREN; }
<INITIAL>")" {return RPAREN; }
<INITIAL>"t," {return COMMA; }
<INITIAL>"#" {return POUND; }
<INITIAL>";;" {return SEMICOLON; }
<INITIAL>"INFIX" {return INFIX; }
<INITIAL>"INFIXL" {return INFIXL; }
<INITIAL>"INFIXR" {return INFIXR; }
<INITIAL>"PREFIX" {return PREFIX; }
<INITIAL>"PREFIXR" {return PREFIXR; }
<INITIAL>"POSTFIX" {return POSTFIX; }
<INITIAL>"POSTFIXL" {return POSTFIXL; }
<INITIAL>"NOFIXITY" {return NOFIXITY; }
<INITIAL>"MIN1" {return MIN1; }
<INITIAL>"MIN2" {return MIN2; }
<INITIAL>"MAX" {return MAX; }
<INITIAL>"NOCODE" {return NOCODE; }
<INITIAL>"LOGIC SYMBOL" {return LSSYMB; }
<INITIAL>"LS_START" {return LSSTART; }
<INITIAL>"LS_END" {return LSEND; }
<INITIAL>"PRED SYMBOL" {return PREDSYMB; }
<INITIAL>"PRED_START" {return PREDSTART; }
<INITIAL>"PRED_END" {return PREDEND; }
<INITIAL>"REGCL" {return REGCL; }
<INITIAL>"BACKTRACK" {return BACKTRACK; }
<INITIAL>"TRUE" {return TRUE; }
<INITIAL>"FALSE" {return FALSE; }
<INITIAL>{WSPACE} {continue; }
<INITIAL>"/%" {commentLev = 1; BEGIN(COMMENT); continue; }
<INITIAL>"/*" {BEGIN(C_COMMENT); continue; }
<INITIAL>{ID} {yylval.name = strdup(yytext); return ID; }
<INITIAL>{NUM} {yylval.isval.ival = atoi(yytext);
yylval.isval.sval = strdup(yytext);
return NUM; }
<C_COMMENT>"*/" {BEGIN(INITIAL); continue; }
<C_COMMENT>{STRING} {yylval.text = strdup(yytext); return STRING; }
<COMMENT>[^%/\n]+ {continue; }
<COMMENT>"/%" {commentLev++; continue; }
<COMMENT>"%/" {commentLev--;
if (!commentLev) BEGIN(INITIAL); continue; }
. {return ERROR; }
@@ -0,0 +1,351 @@
%{
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include "../util/util.h"
#include "op.h"
#include "types.h"
#include "pervgen-c.h"
#include "pervgen-ocaml.h"
//#include "ops.h"
extern int yylex();
int yywrap() {return 1;}
void yyerror(const char* str)
{
printf("Error: Unable to parse input: %s\n", str);
}
static int tySkelInd = 0;
%}
%union
{
char* name;
char* text;
OP_Fixity fixityType;
OP_Prec precType;
OP_Code codeType;
UTIL_Bool boolType;
struct
{
int ival;
char* sval;
} isval;
Type tyval;
TypeList tylistval;
}
%token LBRACKET RBRACKET LPAREN RPAREN COMMA POUND SEMICOLON TRUE
FALSE
TYARROW TYAPP
INFIX INFIXL INFIXR PREFIX PREFIXR POSTFIX POSTFIXL NOFIXITY
MIN1 MIN2 MAX
NOCODE
LSSYMB LSSTART LSEND PREDSYMB PREDSTART PREDEND REGCL
BACKTRACK
KIND CONST EMPTY TYSKEL TYPE EMPTYTYPE ERROR
%token <name> ID
%token <isval> NUM
%token <text> STRING
%start pervasives
%type <text> comments
%type <tyval> arrow_tyskel app_tyskel atomic_tyskel
%type <tylistval> tyskel_list
%type <isval> ty_index tesize neededness
%type <name> const_name const_ind_name
%type <fixityType> fixity
%type <precType> prec
%type <codeType> code_info
%type <boolType> redef typrev
%%
pervasives : kind const_tyskel
;
kind : kind_header kind_decls
{ cgenKindH(); cgenKindC(); ocamlGenKinds(); }
;
kind_header : KIND NUM
{ cgenKindInit($2.ival); cgenNumKinds($2.sval);
ocamlGenNumKinds($2.sval);
}
;
kind_decls : kind_decl SEMICOLON kind_decls
| kind_decl
;
kind_decl : NUM ID ID NUM
{ cgenKindIndex($1.ival, $3, $1.sval, NULL);
cgenKindData($1.ival, $2, $4.sval, NULL);
ocamlGenKind($2, $3, $4.sval, $1.sval); }
| comments NUM ID ID NUM
{ cgenKindIndex($2.ival, $4, $2.sval, $1);
cgenKindData($2.ival, $3, $5.sval, $1);
ocamlGenKind($3, $4, $5.sval, $2.sval); }
;
comments : STRING { $$ = $1;};
;
const_tyskel : const_tyskel_header const_tyskel_decls const_property
{ cgenTySkelsH(); cgenTySkelsC(); cgenConstProperty();
cgenConstH(); cgenConstC();
ocamlGenConsts();
}
;
const_tyskel_header : CONST NUM TYSKEL NUM
{ cgenNumTySkels($4.sval); cgenTySkelInit($4.ival);
cgenNumConsts($2.sval); cgenConstInit($2.ival);
ocamlGenNumConsts($2.sval);
}
;
const_tyskel_decls : const_tyskel_decl SEMICOLON const_tyskel_decls
| const_tyskel_decl
;
const_tyskel_decl : tyskel_decl const_decls
;
tyskel_decl : TYPE NUM arrow_tyskel
{tySkelInd = $2.ival;
ocamlGenTySkel($2.sval, $3);
cgenTySkelTab($2.ival, $3, NULL);
}
| comments TYPE NUM arrow_tyskel
{tySkelInd = $3.ival;
ocamlGenTySkel($3.sval, $4);
cgenTySkelTab($3.ival, $4, $1);
}
;
arrow_tyskel : app_tyskel TYARROW arrow_tyskel
{ $$ = mkArrowType($1, $3); }
| app_tyskel
{ $$ = $1; }
;
app_tyskel : LPAREN TYAPP ID NUM LBRACKET tyskel_list
RBRACKET RPAREN
{$$ = mkStrType(mkStrFuncType($3,$4.sval), $4.ival, $6);}
| atomic_tyskel
{$$ = $1; }
;
atomic_tyskel : ID
{ $$ = mkSortType($1); }
| ty_index
{ $$ = mkSkVarType($1.sval); }
| LPAREN arrow_tyskel RPAREN
{ $$ = $2; }
;
tyskel_list : arrow_tyskel COMMA tyskel_list
{ $$ = addItem($1, $3); }
| arrow_tyskel
{ $$ = addItem($1, NULL); }
ty_index : POUND NUM {$$ = $2;}
;
const_decls : const_decl const_decls
| const_decl
;
const_decl : NUM const_name const_ind_name tesize tesize neededness
typrev redef prec fixity code_info
{ cgenConstIndex($1.ival, $3, $1.sval, NULL);
cgenConstData($1.ival, $2, $4.sval, $9, $10, tySkelInd,
$5.sval, NULL);
ocamlGenConst($1.sval, $2, $3, $10, $9, $7, $8,
$4.ival, tySkelInd, $6.ival, $11,
$1.sval, $2);
}
| NUM const_name const_ind_name tesize tesize neededness
typrev redef prec fixity code_info const_name
{ cgenConstIndex($1.ival, $3, $1.sval, NULL);
cgenConstData($1.ival, $12, $4.sval, $9, $10, tySkelInd,
$5.sval, NULL);
ocamlGenConst($1.sval, $2, $3, $10, $9, $7, $8,
$4.ival, tySkelInd, $6.ival, $11,
$1.sval, $12);
}
| comments NUM const_name const_ind_name tesize tesize
neededness typrev redef prec fixity code_info
{ cgenConstIndex($2.ival, $4, $2.sval, $1);
cgenConstData($2.ival, $3, $5.sval, $10, $11,
tySkelInd, $7.sval, $1);
ocamlGenConst($2.sval, $3, $4, $11, $10, $8, $9,
$5.ival, tySkelInd, $7.ival, $12,
$2.sval, $3);
}
| comments NUM const_name const_ind_name tesize tesize
neededness typrev redef prec fixity code_info const_name
{ cgenConstIndex($2.ival, $4, $2.sval, $1);
cgenConstData($2.ival, $13, $5.sval, $10, $11,
tySkelInd, $7.sval, $1);
ocamlGenConst($2.sval, $3, $4, $11, $10, $8, $9,
$5.ival, tySkelInd, $7.ival, $12,
$2.sval, $13);
}
;
const_name : ID {$$ = $1;}
;
const_ind_name : ID {$$ = $1;}
;
tesize : NUM {$$ = $1;}
;
neededness : NUM {$$ = $1;}
;
typrev : TRUE {$$ = UTIL_TRUE;}
| FALSE {$$ = UTIL_FALSE;}
;
redef : TRUE {$$ = UTIL_TRUE;}
| FALSE {$$ = UTIL_FALSE;}
;
fixity : INFIX {$$ = OP_INFIX;}
| INFIXL {$$ = OP_INFIXL;}
| INFIXR {$$ = OP_INFIXR;}
| PREFIX {$$ = OP_PREFIX;}
| PREFIXR {$$ = OP_PREFIXR;}
| POSTFIX {$$ = OP_POSTFIX;}
| POSTFIXL {$$ = OP_POSTFIXL;}
| NOFIXITY {$$ = OP_NONE;}
;
prec : MIN1 {$$ = OP_mkPrecMin1();}
| MIN2 {$$ = OP_mkPrecMin2();}
| NUM {$$ = OP_mkPrec($1.ival);}
| MAX {$$ = OP_mkPrecMax();}
;
code_info : NOCODE {$$ = OP_mkCodeInfoNone();}
| NUM {$$ = OP_mkCodeInfo($1.ival);}
;
const_property : logic_symbol pred_symbol regclobber backtrackable
;
logic_symbol : ls_header ls_range ls_types
;
ls_header : LSSYMB NUM { cgenLogicSymbolInit($2.ival); }
;
ls_range : LSSTART const_ind_name LSEND const_ind_name
{ cgenLSRange($2, $4);}
;
ls_types : ls_type ls_types
| ls_type
;
ls_type : NUM ID {cgenLogicSymbType($1.ival, $2, $1.sval);}
;
pred_symbol : pred_header pred_range
;
pred_header : PREDSYMB NUM
{if ($2.ival == 0) {
fprintf(stderr,
"The number of predicate symbols cannot be 0\n");
exit(1);
}
}
;
pred_range : PREDSTART const_ind_name PREDEND const_ind_name
{ cgenPREDRange($2, $4); }
;
regclobber : REGCL const_list { ocamlGenRC(); }
;
backtrackable : BACKTRACK const_list { ocamlGenBC(); }
;
const_list : ID const_list { ocamlCollectConsts($1, 0); }
| ID { ocamlCollectConsts($1, 1); }
;
%%
extern FILE* yyin;
int main(argc, argv)
int argc;
char * argv[];
{
int ret = 0;
char * root = NULL;
if(argc == 1)
{
//printf("No input file specified; using 'Pervasives.in'.\n");
yyin = UTIL_fopenR("pervasives.in");
}
else
{
yyin = UTIL_fopenR(argv[1]);
}
if(argc > 2)
{
root = argv[2];
}
else
{
//printf("Teyjus source root directory not specified; using '../../'.\n");
root = "../../";
}
//printf("Generating pervasive files...\n");
ret = yyparse();
UTIL_fclose(yyin);
if(ret != 0)
{
printf("Generation failed.\n");
return -1;
}
spitCPervasivesH(root);
spitCPervasivesC(root);
//spitOCPervasiveMLI(root);
//spitOCPervasiveML(root);
//printf("Done.\n");
return 0;
}
@@ -0,0 +1,454 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/*****************************************************************************/
/* File pervgen-c.c. This files contains function definitions for generating */
/* files pervasives.h and pervasives.c. */
/*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pervgen-c.h"
#include "ccode.h"
#include "../util/util.h"
//dynamic string array type
typedef struct StringArray
{
char **array;
int length;
} StringArray;
//array initialization
static void arrayInit(char **array, int size)
{
int i ;
for (i =0; i < size; i++) array[i] = NULL;
}
//collect string in a StringArray into a single array
static char* collectStringArray(StringArray arr, char* emptyText)
{
char *myText = NULL;
char *myText2 = NULL;
int i;
int length = 0;
for (i = 0; i < arr.length; i++) {
char* text = arr.array[i];
if (text == NULL) text = emptyText;
if (myText) {
myText2 = UTIL_mallocStr(strlen(text) + strlen(myText));
strcpy(myText2, myText);
strcat(myText2, text);
free(myText);
} else {
myText2 = UTIL_mallocStr(strlen(text));
strcpy(myText2, text);
}
if (arr.array[i]) free(arr.array[i]);
myText = myText2;
}
free(arr.array);
return myText;
}
/****************************************************************************/
/* kind relevant components */
/****************************************************************************/
/***********************************************************************/
/* pervasives.h */
/***********************************************************************/
//number of pervasive kinds
static char* numKinds = NULL;
void cgenNumKinds(char* num)
{
numKinds = C_mkNumKinds(num);
}
//pervasive kind indices declaration
static StringArray kindIndices; //kind indices declaration
void cgenKindIndex(int index, char* name, char* indexT, char* comments)
{
char* kindIndex;
char* kindIndexText;
if (index >= kindIndices.length) {
fprintf(stderr, "kind index exceed total number of kinds\n");
exit(1);
}
kindIndex = C_mkIndex(name, indexT, comments);
kindIndexText = UTIL_mallocStr(strlen(kindIndex) + 2);
strcpy(kindIndexText, kindIndex); free(kindIndex);
if (index != (kindIndices.length - 1)) strcat(kindIndexText, ",");
strcat(kindIndexText, "\n");
kindIndices.array[index] = kindIndexText;
}
//pervasive kind relevant information in pervasives.h
static char* kindH;
void cgenKindH()
{
char* emptyText = C_mkEmptyComments();
char* kindIndexBody = collectStringArray(kindIndices, emptyText);
char* kindIndexTypeDef = C_mkKindIndexType(kindIndexBody);
kindH = C_mkKindH(kindIndexTypeDef, numKinds);
free(kindIndexBody); free(kindIndexTypeDef); free(numKinds);
free(emptyText);
}
/***********************************************************************/
/* pervasives.c */
/***********************************************************************/
//pervasive kind table entries
static StringArray kindData;
void cgenKindData(int index, char* name, char* arity, char* comments)
{
char* oneKindData;
char* kindDataText;
if (index >= kindData.length) {
fprintf(stderr, "kind index exceed total number of kinds\n");
exit(1);
}
oneKindData = C_mkKindTabEntry(name, arity, comments);
kindDataText = UTIL_mallocStr(strlen(oneKindData) + 2);
strcpy(kindDataText, oneKindData); free(oneKindData);
if (index != kindData.length - 1) strcat(kindDataText, ",");
strcat(kindDataText, "\n");
kindData.array[index] = kindDataText;
}
#define EMPTY_TEXT_KIND_TAB " //nothing \n {NULL, 0},\n"
//pervasive kind relevant information in pervasives.c
static char* kindC;
void cgenKindC()
{
char* kindTabBody = collectStringArray(kindData, EMPTY_TEXT_KIND_TAB);
char* kindTab = C_mkKindTab(kindTabBody);
kindC = C_mkKindC(kindTab);
free(kindTabBody); free(kindTab);
}
//kind indices info and kind table info initiation
void cgenKindInit(int length)
{
kindIndices.length = length;
kindIndices.array = (char**)UTIL_malloc(sizeof(char*)*length);
arrayInit(kindIndices.array, length);
kindData.length = length;
kindData.array = (char**)UTIL_malloc(sizeof(char*)*length);
arrayInit(kindData.array, length);
}
/****************************************************************************/
/* type skeleton relevant components */
/****************************************************************************/
/***********************************************************************/
/* pervasives.h */
/***********************************************************************/
//number of type skeletons for pervasive constants
static char* numTySkels = NULL;
void cgenNumTySkels(char* num)
{
numTySkels = C_mkNumTySkels(num);
}
//type skeleton relevant information in pervasives.h
static char* tySkelsH;
void cgenTySkelsH()
{
tySkelsH = C_mkTySkelsH(numTySkels);
free(numTySkels);
}
/***********************************************************************/
/* pervasives.c */
/***********************************************************************/
//type skeleton creation code
static StringArray tySkels;
void cgenTySkelTab(int index, Type tyskel, char* comments)
{
if (index >= tySkels.length){
fprintf(stderr,
"type skeleton index exceed total number of type skeletons\n");
exit(1);
}
tySkels.array[index] = C_genTySkel(tyskel, comments);
}
//generate types skeleton initialization code
static char* cgenTySkelTabInit()
{
char* body = collectStringArray(tySkels, "");
char* text = C_mkTySkelTabInit(body, C_totalSpace);
free(body);
return text;
}
//type skeleton info initiation
void cgenTySkelInit(int length)
{
if (length == 0) {
fprintf(stderr, "The number of type skeletons cannot be 0\n");
exit(1);
}
tySkels.length = length;
tySkels.array = (char**)UTIL_malloc(sizeof(char*)*length);
arrayInit(tySkels.array, length);
}
//type skeleton relevant information in pervasives.c
static char* tySkelsC;
void cgenTySkelsC()
{
char* tySkelTab = cgenTySkelTabInit();
tySkelsC = C_mkTySkelsC(tySkelTab); free(tySkelTab);
}
/****************************************************************************/
/* constant relevant components */
/****************************************************************************/
/***********************************************************************/
/* pervasives.h */
/***********************************************************************/
//number of pervasive constants
static char* numConsts = NULL;
void cgenNumConsts(char* num)
{
numConsts = C_mkNumConsts(num);
}
//pervasive constant indices declaration
static StringArray constIndices;
void cgenConstIndex(int index, char* name, char* indexT, char* comments)
{
char* constIndex;
char* constIndexText;
if (index >= constIndices.length) {
fprintf(stderr, "constant index exceed total number of constants\n");
exit(1);
}
constIndex = C_mkIndex(name, indexT, comments);
constIndexText = UTIL_mallocStr(strlen(constIndex) + 2);
strcpy(constIndexText, constIndex);
if (index != (constIndices.length - 1)) strcat(constIndexText, ",");
strcat(constIndexText, "\n");
constIndices.array[index] = constIndexText;
}
/***********************************************************************/
/* constant property functions */
/***********************************************************************/
static StringArray logicSymbTypes;
//initiale logic symb types
void cgenLogicSymbolInit(int length)
{
logicSymbTypes.length = length;
logicSymbTypes.array = (char**)UTIL_malloc(sizeof(char*)*length);
arrayInit(logicSymbTypes.array, length);
}
//generate logic symbol types
void cgenLogicSymbType(int index, char* name, char* indexT)
{
char* constIndex;
char* constIndexText;
if (index >= logicSymbTypes.length) {
fprintf(stderr, "logic symbol type index exceed the total number of logic symbols\n");
exit(1);
}
constIndex = C_mkIndex2(name, indexT);
constIndexText = UTIL_mallocStr(strlen(constIndex) + 2);
strcpy(constIndexText, constIndex);
if (index != (constIndices.length - 1)) strcat(constIndexText, ",");
strcat(constIndexText, "\n");
logicSymbTypes.array[index] = constIndexText;
}
static char* lsRange = NULL;
//generate logic symbol start/end position
void cgenLSRange(char* start, char* end)
{
lsRange = C_mkLSRange(start, end);
}
static char* predRange = NULL;
//generate predicate symbol start/end position
void cgenPREDRange(char* start, char* end)
{
predRange = C_mkPredRange(start, end);
}
static char* constProperty = NULL;
void cgenConstProperty()
{
char* emptyText = C_mkEmptyComments();
char* logicSymbTypeBody = collectStringArray(logicSymbTypes, emptyText);
char* logicSymbTypeDec = C_mkLSTypeDec(logicSymbTypeBody);
constProperty = UTIL_mallocStr(strlen(logicSymbTypeDec) + strlen(lsRange)
+ strlen(predRange));
strcpy(constProperty, lsRange);
strcat(constProperty, predRange);
strcat(constProperty , logicSymbTypeDec);
free(emptyText); free(logicSymbTypeBody); free(logicSymbTypeDec);
free(lsRange); free(predRange);
}
//pervasive kind relevant information in pervasives.h
static char* constH;
void cgenConstH()
{
char* emptyText = C_mkEmptyComments();
char* constIndexBody = collectStringArray(constIndices, emptyText);
char* constIndexTypeDef = C_mkConstIndexType(constIndexBody);
constH = C_mkConstH(constIndexTypeDef, numConsts, constProperty);
free(constIndexBody); free(constIndexTypeDef);
free(emptyText); free(constProperty);
}
/***********************************************************************/
/* pervasives.c */
/***********************************************************************/
//pervasive const table entries
static StringArray constData;
void cgenConstData(int index, char* name, char* tesize, OP_Prec prec,
OP_Fixity fixity, int tySkelInd, char* neededness,
char* comments)
{
char* oneConstData;
char* constDataText;
char* tySkelIndText = UTIL_itoa(tySkelInd);
if (index >= constData.length) {
fprintf(stderr, "const index exceed total number of consts\n");
exit(1);
}
oneConstData = C_mkConstTabEntry(name, tesize, prec, fixity, tySkelIndText,
neededness, comments);
free(tySkelIndText);
constDataText = UTIL_mallocStr(strlen(oneConstData) + 2);
strcpy(constDataText, oneConstData); free(oneConstData);
if (index != constData.length - 1) strcat(constDataText, ",");
strcat(constDataText, "\n");
constData.array[index] = constDataText;
}
#define EMPTY_TEXT_CONST_TAB \
" //nothing\n {NULL, 0, 0, 0, 0, OP_NONE },\n"
//pervasive const relevant information in pervasives.c
static char* constC;
void cgenConstC()
{
char* constTabBody = collectStringArray(constData, EMPTY_TEXT_CONST_TAB);
char* constTab = C_mkConstTab(constTabBody);
constC = C_mkConstC(constTab);
free(constTabBody); free(constTab);
}
//const indices info and const table info initiation
void cgenConstInit(int length)
{
constIndices.length = length;
constIndices.array = (char**)UTIL_malloc(sizeof(char*)*length);
arrayInit(constIndices.array, length);
constData.length = length;
constData.array = (char**)UTIL_malloc(sizeof(char*)*length);
arrayInit(constData.array, length);
}
/****************************************************************************/
/* Writing files */
/****************************************************************************/
static char* pervBegH;
static char* pervEndH;
static void cgenFixedH()
{
pervBegH = C_mkFixedBegH();
pervEndH = C_mkFixedEndH();
}
static char* pervBegC;
static char* pervEndC;
static void cgenFixedC()
{
pervBegC = C_mkFixedBegC();
pervEndC = C_mkFixedEndC();
}
/* dump peravsives.h */
void spitCPervasivesH(char * root)
{
FILE* outFile;
char * filename = malloc(strlen(root) + 32);
strcpy(filename, root);
strcat(filename, "tables/pervasives.h");
outFile = UTIL_fopenW(filename);
cgenFixedH();
fprintf(outFile, "%s\n", pervBegH); free(pervBegH);
fprintf(outFile, "%s\n", kindH); free(kindH);
fprintf(outFile, "%s\n", tySkelsH); free(tySkelsH);
fprintf(outFile, "%s\n", constH); free(constH);
fprintf(outFile, "%s\n", pervEndH); free(pervEndH);
UTIL_fclose(outFile);
free(filename);
}
/* dump pervasives.c */
void spitCPervasivesC(char * root)
{
FILE* outFile;
char * filename = malloc(strlen(root) + 32);
strcpy(filename, root);
strcat(filename, "tables/pervasives.c");
outFile = UTIL_fopenW(filename);
cgenFixedC();
fprintf(outFile, "%s\n", pervBegC); free(pervBegC);
fprintf(outFile, "%s\n", kindC); free(kindC);
fprintf(outFile, "%s\n", tySkelsC); free(tySkelsC);
fprintf(outFile, "%s\n", constC); free(constC);
fprintf(outFile, "%s\n", pervEndC); free(pervEndC);
UTIL_fclose(outFile);
free(filename);
}
@@ -0,0 +1,95 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/*****************************************************************************/
/* File pervgen-c.h. This files contains function definitions for generating */
/* files pervasives.h and pervasives.c. */
/*****************************************************************************/
#include "types.h"
#include "op.h"
/****************************************************************************/
/* kind relevant components */
/****************************************************************************/
//kind indices info and kind table info initiation
void cgenKindInit(int size);
//number of pervasive kinds
void cgenNumKinds(char* num);
//pervasive kind indices declaration
void cgenKindIndex(int index, char* name, char* indexT, char* comments);
//pervasive kind relevant information in pervasives.h
void cgenKindH();
//pervasive kind table entries
void cgenKindData(int index, char* name, char* arity, char* comments);
//pervasive kind relevant information in pervasives.c
void cgenKindC();
/****************************************************************************/
/* type skeleton relevant components */
/****************************************************************************/
//number of type skeletons for pervasive constants
void cgenNumTySkels(char* num);
//type skeleton relevant information in pervasives.h
void cgenTySkelsH();
//type skeleton creation code
void cgenTySkelTab(int index, Type tyskel, char* comments);
//type skeleton info initiation
void cgenTySkelInit(int length);
//type skeleton relevant information in pervasives.c
void cgenTySkelsC();
/****************************************************************************/
/* constant relevant components */
/****************************************************************************/
//const indices info and const table info initiation
void cgenConstInit(int length);
//number of pervasive constants
void cgenNumConsts(char* num);
//pervasive constant indices declaration
void cgenConstIndex(int index, char* name, char* indexT, char* comments);
//pervasive constant relevant information in pervasives.h
void cgenConstH();
//pervasive constant table entries
void cgenConstData(int index, char* name, char* tesize, OP_Prec prec,
OP_Fixity fixity, int tySkelInd, char* neededness,
char* comments);
//pervasive const relevant information in pervasives.c
void cgenConstC();
//initiale logic symb types
void cgenLogicSymbolInit(int length);
//generate logic symbol types
void cgenLogicSymbType(int index, char* name, char* indexText);
//generate logic symbol start/end position
void cgenLSRange(char* start, char* end);
//generate predicate symbol start/end position
void cgenPREDRange(char* start, char* end);
void cgenConstProperty();
/****************************************************************************/
/* Writing files */
/****************************************************************************/
/* dump files pervasives.h */
void spitCPervasivesH(char * root);
/* dump files pervasives.c */
void spitCPervasivesC(char * root);
@@ -0,0 +1,350 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/*****************************************************************************/
/* File pervgen-ocaml.c. This files contains function definitions for */
/* generating files pervasive.mli and pervasive.ml. */
/*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pervgen-ocaml.h"
#include "ocamlcode.h"
static char* addLine(char* str, char* addOn)
{
size_t length = (str ? strlen(str) : 0) + strlen(addOn) + 2;
char* newStr = UTIL_mallocStr(length);
if (str) {
strcpy(newStr, str);
strcat(newStr, addOn);
} else strcpy(newStr, addOn);
strcat(newStr, "\n\n");
return newStr;
}
static char* addStr(char* str, char* addOn)
{
size_t length = (str ? strlen(str) : 0) + strlen(addOn);
char* newStr = UTIL_mallocStr(length);
if (str) {
strcpy(newStr, str);
strcat(newStr, addOn);
} else strcpy(newStr, addOn);
return newStr;
}
/**************************************************************************/
/* generating pervasive kind relevant part */
/**************************************************************************/
static char* numKindsML = NULL;
static char* numKindsMLI = NULL;
void ocamlGenNumKinds(char* number)
{
numKindsMLI = strdup("val numberPervasiveKinds : int");
numKindsML = addStr("let numberPervasiveKinds = ", number);
}
static char* kindVarList = NULL; //kind variable definitions
static char* buildPervKindBody = NULL; //buildPervKind function defs
static char* kindVarDecs = NULL; //kind vars in signature
static char* isKindFuncDecs = NULL; //is kind function decs
static char* isKindFuncDefs = NULL; //is kind function defs
void ocamlGenKind(char* kindName, char* kVarName, char* arity, char* offset)
{
char* kindVarName = OC_mkKVarName(kVarName);
char* funcName = OC_mkIsKindFuncName(kindVarName);
char* kindVar = OC_mkKindVar(kindVarName, kindName, arity, offset);
char* kindTabEntry = OC_mkTabEntry(kindName, kindVarName);
char* kindVarDec = OC_mkKindVarDec(kindVarName);
char* funcDec = OC_mkIsKindFuncDec(funcName);
char* funcDef = OC_mkIsKindFuncDef(funcName, kindVarName);
char *myKindVarList, *myBuildPervKindBody, *myKindVarDecs,
*myisKindFuncDecs, *myisKindFuncDefs;
free(kindVarName);
myKindVarList = addLine(kindVarList, kindVar);
free(kindVarList); free(kindVar);
kindVarList = myKindVarList;
myBuildPervKindBody = addStr(buildPervKindBody, kindTabEntry);
free(buildPervKindBody); free(kindTabEntry);
buildPervKindBody = myBuildPervKindBody;
myKindVarDecs = addStr(kindVarDecs, kindVarDec);
free(kindVarDecs); free(kindVarDec);
kindVarDecs = myKindVarDecs;
myisKindFuncDecs = addStr(isKindFuncDecs, funcDec);
free(isKindFuncDecs); free(funcDec);
isKindFuncDecs = myisKindFuncDecs;
myisKindFuncDefs = addLine(isKindFuncDefs, funcDef);
free(isKindFuncDefs); free(funcDef);
isKindFuncDefs = myisKindFuncDefs;
}
static char* kindML = NULL; //kind relevant code in pervasive.ml
static char* kindMLI = NULL; //kind relevant code in pervasive.mli
void ocamlGenKinds()
{
char* buildTabFunc = OC_mkBuildKTabFunc(buildPervKindBody);
size_t length = strlen(kindVarList) + strlen(buildTabFunc) +
strlen(isKindFuncDefs) + strlen(numKindsML) + 4;
kindML = UTIL_mallocStr(length);
strcpy(kindML, kindVarList);
strcat(kindML, "\n");
strcat(kindML, numKindsML);
strcat(kindML, "\n\n");
strcat(kindML, buildTabFunc);
strcat(kindML, "\n");
strcat(kindML, isKindFuncDefs);
free(buildPervKindBody); free(buildTabFunc); free(kindVarList);
free(isKindFuncDefs); free(numKindsML);
length = strlen(kindVarDecs) + strlen(isKindFuncDecs) +
strlen(numKindsMLI) + 4;
kindMLI = UTIL_mallocStr(length);
strcpy(kindMLI, kindVarDecs);
strcat(kindMLI, "\n\n");
strcat(kindMLI, numKindsMLI);
strcat(kindMLI, "\n\n");
strcat(kindMLI, isKindFuncDecs);
free(kindVarDecs); free(isKindFuncDecs); free(numKindsMLI);
}
/**************************************************************************/
/* generating pervasive type skeleton relevant part */
/**************************************************************************/
static char* tySkelVarList = NULL; //type skel vars
void ocamlGenTySkel(char* ind, Type tySkel)
{
char* varName = OC_mkTySkelVarName(ind);
char* tySkelText = OC_genTySkel(tySkel);
char* tySkelVarDef = OC_mkTYSkelVar(varName, tySkelText);
size_t length = (tySkelVarList ? strlen(tySkelVarList) : 0) +
strlen(tySkelVarDef) + 1;
char* mytySkelVarList = UTIL_mallocStr(length + 1);
free(varName); free(tySkelText);
mytySkelVarList = addLine(tySkelVarList, tySkelVarDef);
free(tySkelVarList); free(tySkelVarDef);
tySkelVarList = mytySkelVarList;
}
/**************************************************************************/
/* generating pervasive constants relevant part */
/**************************************************************************/
static char* numConstsML = NULL;
static char* numConstsMLI = NULL;
void ocamlGenNumConsts(char* number)
{
numConstsMLI = strdup("val numberPervasiveConstants : int");
numConstsML = addStr("let numberPervasiveConstants = ", number);
}
static char* constVarList = NULL; //constant vars
static char* buildPervConstBody = NULL; //buildPervConst function defs
static char* constVarDecs = NULL; //constant vars in signature
static char* isConstFuncDecs = NULL; //is constant function decs
static char* isConstFuncDefs = NULL; //is constant function defs
void ocamlGenConst(char* ind, char* name, char* cVarName, OP_Fixity fixity,
OP_Prec prec, UTIL_Bool tyPrev, UTIL_Bool redef, int tesize,
int tyskelInd, int neededness, OP_Code codeInfo,
char* offset, char *printName)
{
char* constVarName = OC_mkCVarName(cVarName);
char* funcName = OC_mkIsConstFuncName(constVarName);
char* tyskelText = UTIL_itoa(tyskelInd);
char* tyskelName = OC_mkTySkelVarName(tyskelText);
char* constVar = OC_mkConstVar(name, fixity, prec, tyPrev, tyskelName,
tesize, neededness, codeInfo, redef,
constVarName, offset, printName);
char* constTabEntry = OC_mkTabEntry(name, constVarName);
char* constVarDec = OC_mkConstVarDec(constVarName);
char* funcDec = OC_mkIsConstFuncDec(funcName);
char* funcDef = OC_mkIsConstFuncDef(funcName, constVarName);
char *myConstVarList, *myBuildPervConstBody, *myConstVarDecs,
*myisConstFuncDecs, *myisConstFuncDefs;
free(constVarName); free(funcName); free(tyskelName); free(tyskelText);
myConstVarList = addLine(constVarList, constVar);
free(constVarList); free(constVar);
constVarList = myConstVarList;
myBuildPervConstBody = addStr(buildPervConstBody, constTabEntry);
free(buildPervConstBody); free(constTabEntry);
buildPervConstBody = myBuildPervConstBody;
myConstVarDecs = addStr(constVarDecs, constVarDec);
free(constVarDecs); free(constVarDec);
constVarDecs = myConstVarDecs;
myisConstFuncDecs = addStr(isConstFuncDecs, funcDec);
free(isConstFuncDecs); free(funcDec);
isConstFuncDecs = myisConstFuncDecs;
myisConstFuncDefs = addLine(isConstFuncDefs, funcDef);
free(isConstFuncDefs); free(funcDef);
isConstFuncDefs = myisConstFuncDefs;
}
static char* constMLI = NULL; //const relevant code in pervasive.mli
static char* constML = NULL; //const relevant code in pervasive.ml
void ocamlGenConsts()
{
char* tyskels = OC_mkFixedTySkels(tySkelVarList);
char* varDefs = OC_mkGenericConstVar(constVarList);
char* varDecs = OC_mkGenericConstVarDec(constVarDecs);
char* buildFuncBody = OC_mkGenericConstTabEntry(buildPervConstBody);
char* buildTabFunc = OC_mkBuildCTabFunc(buildFuncBody);
char* funcDefs = OC_mkGenericConstFuncDefs(isConstFuncDefs);
char* funcDecs = OC_mkGenericConstFuncDecs(isConstFuncDecs);
size_t length = strlen(varDefs) + strlen(buildTabFunc) + strlen(funcDefs)
+ strlen(numConstsML) + 4;
tySkelVarList = tyskels;
constML = UTIL_mallocStr(length);
strcpy(constML, varDefs); free(varDefs);
strcat(constML, "\n");
strcat(constML, numConstsML); free(numConstsML);
strcat(constML, "\n\n");
strcat(constML, buildTabFunc); free(buildTabFunc); free(buildFuncBody);
strcat(constML, "\n");
strcat(constML, funcDefs); free(funcDefs);
length = strlen(varDecs) + strlen(funcDecs) + strlen(numConstsMLI) + 4;
constMLI = UTIL_mallocStr(length);
strcpy(constMLI, varDecs); free(varDecs);
strcat(constMLI, "\n\n");
strcat(constMLI, numConstsMLI); free(numConstsMLI);
strcat(constMLI, "\n\n");
strcat(constMLI, funcDecs); free(funcDecs);
}
static char* constProperty = NULL;
void ocamlCollectConsts(char* name, int last)
{
char* constName = OC_mkCVarName(name);
char* cond = OC_mkCompare(constName);
char* body;
free(constName);
if (last) body = cond;
else {
if (constProperty) {
body = OC_mkOr(cond, constProperty);
free(constProperty);
free(cond);
} else body = cond;
}
constProperty = body;
}
static char* regClob = NULL;
void ocamlGenRC()
{
regClob = OC_mkRegClobFunc(constProperty);
free(constProperty);
constProperty = NULL;
}
static char* backTrack = NULL;
void ocamlGenBC()
{
backTrack = OC_mkBackTrackFunc(constProperty);
free(constProperty);
constProperty = NULL;
}
/**************************************************************************/
/* generating fixed part of pervasive.ml and pervasive.mli */
/**************************************************************************/
static char* fixedML = NULL; //fixed part of pervasive.ml
static char* fixedMLI = NULL; //fixed part of pervasive.mli
static void ocamlGenFixedML()
{
fixedML = OC_mkFixedML();
}
static void ocamlGenFixedMLI()
{
fixedMLI = OC_mkFixedMLI();
}
/***************************************************************************/
/* Dump code into pervasive.ml and pervasive.mli */
/***************************************************************************/
/* dump peravsive.ml */
void spitOCPervasiveML(char * root)
{
FILE* outFile;
char * filename = malloc(strlen(root) + 32);
strcpy(filename, root);
strcat(filename, "compiler/pervasive.ml");
outFile = UTIL_fopenW(filename);
ocamlGenFixedML();
fprintf(outFile, "%s\n\n", kindML); free(kindML);
fprintf(outFile, "%s\n\n", tySkelVarList); free(tySkelVarList);
fprintf(outFile, "%s\n\n", constML); free(constML);
fprintf(outFile, "%s\n\n", fixedML); free(fixedML);
fprintf(outFile, "%s\n\n", regClob); free(regClob);
fprintf(outFile, "%s\n\n", backTrack); free(backTrack);
UTIL_fclose(outFile);
free(filename);
}
/* dump peravsive.mli */
void spitOCPervasiveMLI(char * root)
{
FILE* outFile;
char * filename = malloc(strlen(root) + 32);
strcpy(filename, root);
strcat(filename, "compiler/pervasive.mli");
outFile = UTIL_fopenW(filename);
ocamlGenFixedMLI();
fprintf(outFile, "%s\n\n", kindMLI); free(kindMLI);
fprintf(outFile, "%s\n\n", constMLI); free(constMLI);
fprintf(outFile, "%s\n\n", fixedMLI); free(fixedMLI);
UTIL_fclose(outFile);
free(filename);
}
@@ -0,0 +1,60 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/*****************************************************************************/
/* File pervgen-ocaml.c. This files contains function definitions for */
/* generating files pervasive.mli and pervasive.ml. */
/*****************************************************************************/
#include "op.h"
#include "types.h"
#include "../util/util.h"
/**************************************************************************/
/* generating pervasive kind relevant part */
/**************************************************************************/
void ocamlGenNumKinds(char* number);
void ocamlGenKind(char* kindName, char* kVarName, char* arity, char* offset);
void ocamlGenKinds();
/**************************************************************************/
/* generating pervasive type skeleton relevant part */
/**************************************************************************/
void ocamlGenTySkel(char* ind, Type tySkel);
/**************************************************************************/
/* generating pervasive constants relevant part */
/**************************************************************************/
void ocamlGenNumConsts(char* number);
void ocamlGenConst(char* ind, char* name, char* cVarName, OP_Fixity fixity,
OP_Prec prec, UTIL_Bool tyPrev, UTIL_Bool redef, int tesize,
int tyskelInd, int neededness, OP_Code codeInfo,
char* offset, char* printName);
void ocamlGenConsts();
void ocamlCollectConsts(char* name, int last);
void ocamlGenRC();
void ocamlGenBC();
/***************************************************************************/
/* Dump code into pervasive.ml and pervasive.mli */
/***************************************************************************/
/* dump peravsive.ml */
void spitOCPervasiveML(char * root);
/* dump peravsive.mli */
void spitOCPervasiveMLI(char * root);
@@ -0,0 +1,114 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/****************************************************************************/
/* File types.c. This file contains "abstract syntax" representation of */
/* type skeletons that is used for parsing those in pervasives.in. */
/****************************************************************************/
#include <stdlib.h>
#include "types.h"
#include "../util/util.h"
Type mkSortType(char* name)
{
Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
rtPtr -> tag = SORT;
rtPtr -> data.sort = name;
return rtPtr;
}
Type mkSkVarType(char* index)
{
Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
rtPtr -> tag = SKVAR;
rtPtr -> data.skvar = index;
return rtPtr;
}
Type mkStrFuncType(char* name, char* arity)
{
Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
rtPtr -> tag = FUNC;
rtPtr -> data.func.name = name;
rtPtr -> data.func.arity = arity;
return rtPtr;
}
Type mkStrType(Type func, int arity, TypeList args)
{
Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
rtPtr -> tag = STR;
rtPtr -> data.str.functor = func;
rtPtr -> data.str.arity = arity;
rtPtr -> data.str.args = args;
return rtPtr;
}
Type mkArrowType(Type lop, Type rop)
{
Type rtPtr = (Type)UTIL_malloc(sizeof(Type_));
rtPtr -> tag = ARROW;
rtPtr -> data.arrow.lop = lop;
rtPtr -> data.arrow.rop = rop;
return rtPtr;
}
void freeType(Type ty)
{
if (ty -> tag == SORT) free(ty->data.sort);
else if (ty -> tag == SKVAR) free(ty->data.skvar);
else if (ty -> tag == FUNC) {
free(ty->data.func.name);
free(ty->data.func.arity);
}
free(ty);
}
TypeList addItem(Type data, TypeList typeList)
{
TypeList new = (TypeList)UTIL_malloc(sizeof(TypeList_));
new -> oneType = data;
if (typeList) new -> next = typeList;
else new -> next = NULL;
typeList = new;
return typeList;
}
TypeList addItemToEnd(TypeList typeList, Type data)
{
TypeList new = (TypeList)UTIL_malloc(sizeof(TypeList_));
new -> oneType = data;
new -> next = NULL;
if (typeList) {
TypeList temp = typeList;
while (temp -> next) temp = temp -> next;
temp -> next = new;
} else typeList = new;
return typeList;
}
TypeList append(TypeList typeList1, TypeList typeList2)
{
if (typeList1) {
TypeList temp = typeList1;
while (temp -> next) temp = temp -> next;
temp -> next = typeList2;
} else typeList1 = typeList2;
return typeList1;
}
@@ -0,0 +1,92 @@
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/****************************************************************************/
/* File types.h. This file contains "abstract syntax" representation of */
/* type skeletons that is used for parsing those in pervasives.in. */
/****************************************************************************/
#ifndef TYPES_H
#define TYPES_H
typedef struct Type_ *Type;
typedef struct TypeList_ *TypeList;
//type arrow information
typedef struct ArrowInfo
{
Type lop;
Type rop;
} ArrowInfo;
//structure functor information
typedef struct FuncInfo
{
char *name;
char *arity;
} FuncInfo;
//type structure information
typedef struct StrInfo
{
Type functor;
int arity;
TypeList args;
} StrInfo;
//type skeleton category
typedef enum {
SORT, SKVAR, ARROW, STR, FUNC
} TypeCats;
//type representation
typedef struct Type_
{
TypeCats tag;
union
{
char* sort;
char* skvar;
FuncInfo func;
ArrowInfo arrow;
StrInfo str;
} data;
} Type_;
//type list representation
typedef struct TypeList_
{
Type oneType;
TypeList next;
} TypeList_;
Type mkSortType(char* name);
Type mkSkVarType(char* index);
Type mkStrFuncType(char* name, char* arity);
Type mkStrType(Type name, int arity, TypeList args);
Type mkArrowType(Type lop, Type rop);
void freeType(Type ty);
TypeList addItem(Type data, TypeList typeList);
TypeList addItemToEnd(TypeList typeList, Type data);
TypeList append(TypeList typeList1, TypeList typeList2);
#endif //TYPES_H
+135
View File
@@ -0,0 +1,135 @@
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
// Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "util.h"
/**************************************************************************/
/* Space allocation */
/**************************************************************************/
/* allocate space of n bytes*/
void* UTIL_malloc(size_t n)
{
void* ptr = (void*)malloc(n);
if (ptr) return ptr;
printf("Error : cannot allocate space\n");
exit(1);
}
/* allocate space for a string of given size */
char* UTIL_mallocStr(size_t size)
{
char* ptr = (char*)malloc(sizeof(char)*(size + 1));
if (ptr) return ptr;
printf("Error : cannot allocate space\n");
exit(1);
}
/**************************************************************************/
/* string operation */
/**************************************************************************/
/* Append two strings */
char* UTIL_appendStr(char* str1, char* str2)
{
size_t length = strlen(str1) + strlen(str2);
char* ptr = UTIL_mallocStr(length + 1);
strcpy(ptr, str1);
strcat(ptr, str2);
return ptr;
}
//convert lower case letters in a string to upper case ones
char* UTIL_upperCase(char* str)
{
char *newstr, *tmp;
newstr = strdup(str);
tmp = newstr;
while ((*tmp) != '\0'){
if ((97 <= (int)*tmp) && ((int)*tmp <= 122))
*tmp = (char)((int)*tmp - 32);
tmp++;
}
return newstr;
}
//convert to lower cases
char* UTIL_lowerCase(char* str)
{
char *newstr, *tmp;
newstr = strdup(str);
tmp = newstr;
while ((*tmp) != '\0'){
if ((65 <= (int)*tmp) && ((int)*tmp) <= 90)
*tmp = (char)((int)*tmp + 32);
tmp++;
}
return newstr;
}
//covert an non-negtive integer to string
char* UTIL_itoa(int num)
{
char *str = UTIL_mallocStr(33);
sprintf(str, "%d", num);
return str;
}
/**************************************************************************/
/* file operation */
/**************************************************************************/
/* open file in read mode */
FILE* UTIL_fopenR(char* filename)
{
FILE* filePtr = fopen(filename, "r");
if (filePtr) return filePtr;
printf("Error : cannot open input file %s\n", filename);
exit(1);
}
/* open file in write mode */
FILE* UTIL_fopenW(char* filename)
{
FILE* filePtr = fopen(filename, "w");
if (filePtr) return filePtr;
printf("Error : cannot open output file %s\n", filename);
exit(1);
}
/* close file */
void UTIL_fclose(FILE* file)
{
fclose(file);
}
@@ -0,0 +1,71 @@
//////////////////////////////////////////////////////////////////////////////
//Copyright 2008
// Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
//////////////////////////////////////////////////////////////////////////////
// This file is part of Teyjus. //
// //
// Teyjus is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// Teyjus is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with Teyjus. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////
/**************************************************************************/
/* util.h{c}. */
/* Auxiliary functions needed for generating source files. */
/**************************************************************************/
#ifndef UTIL_H
#define UTIL_H
#include <stdio.h>
/**************************************************************************/
/* Space allocation */
/**************************************************************************/
/* allocate space */
void* UTIL_malloc(size_t size);
/* allocate space for a string of given size */
char* UTIL_mallocStr(size_t size);
/**************************************************************************/
/* string operation */
/**************************************************************************/
/* append two strings */
char* UTIL_appendStr(char* str1, char* str2);
/* capitalizing */
char* UTIL_upperCase(char* str);
/* to lower cases */
char* UTIL_lowerCase(char* str);
/* covert a non-negative integer to string */
char* UTIL_itoa(int num);
/**************************************************************************/
/* file operation */
/**************************************************************************/
/* open file in read mode */
FILE* UTIL_fopenR(char* filename);
/* open file in write mode */
FILE* UTIL_fopenW(char* filename);
/* close file */
void UTIL_fclose(FILE* file);
/* bool type */
typedef enum {
UTIL_FALSE, UTIL_TRUE
} UTIL_Bool;
#endif