Skip to content

Commit 36f5a41

Browse files
author
Peter Aronsson
committed
Added socket communication for an interactive mode compiler. Start modeq with +d=interactive,dump,interactivedump and then telnet to port 29500 to send models to the server. So far the models are not saved anywhere, they are only output if dump flag is set.
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@776 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent ea5743e commit 36f5a41

File tree

9 files changed

+373
-6
lines changed

9 files changed

+373
-6
lines changed

modeq/Makefile.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ RMLHOME = @rmlhome@
1919
RML = @rmlc_bin@ -g
2020
RMLINC = -I$(RMLHOME)/include/plain
2121

22-
LDFLAGS = -L$(RMLHOME)/lib/plain $(ANTLR_LIBP) -lrml -lm -lantlr
22+
LDFLAGS = -L$(RMLHOME)/lib/plain $(ANTLR_LIBP) -lrml -lm -lantlr -lsocket -lnsl
2323

2424
PROG = modeq
2525
AST = absyn_builder/absyn_builder.a
26-
RTOBJ = runtime/rtopts.o
26+
RTOBJ = runtime/rtopts.o runtime/socketimpl.o
2727

2828
SRCRML= absyn.rml \
2929
algorithm.rml \

modeq/absyn_builder/parse.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,5 +67,51 @@ RML_BEGIN_LABEL(Parser__parse)
6767
}
6868
RML_END_LABEL
6969

70+
RML_BEGIN_LABEL(Parser__parsestring)
71+
{
72+
char* str = RML_STRINGDATA(rmlA0);
73+
bool debug = check_debug_flag("parsedump");
74+
try
75+
{
76+
std::istrstream stream(str);
77+
78+
modelica_lexer lex(stream);
79+
modelica_parser parse(lex);
80+
parse.stored_definition();
81+
antlr::RefAST t = parse.getAST();
82+
83+
if (t)
84+
{
85+
if (debug)
86+
{
87+
parse_tree_dumper dumper(std::cout);
88+
dumper.dump(t);
89+
}
90+
91+
modelica_tree_parser build;
92+
void* ast = build.stored_definition(t);
93+
94+
if (debug)
95+
{
96+
std::cout << "Build done\n";
97+
}
98+
99+
rmlA0 = ast ? ast : mk_nil();
100+
101+
RML_TAILCALLK(rmlSC);
102+
}
103+
}
104+
catch (std::exception &e)
105+
{
106+
std::cerr << "Error while parsing:\n" << e.what() << "\n";
107+
}
108+
catch (...)
109+
{
110+
std::cerr << "Error while parsing\n";
111+
}
112+
RML_TAILCALLK(rmlFC);
113+
}
114+
RML_END_LABEL
115+
70116
} // extern "C"
71117

modeq/codegen.rml

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,8 +1091,8 @@ relation generate_algorithm_statement : (Algorithm.Statement, int)
10911091
Util.string_append_list(["simple_index_alloc_",ident_type_str,
10921092
"1(&",evar,", ",tvar,", &",ivar,");"])
10931093
=> stmt_array &
1094-
Util.string_append_list([ivar," = *(",array_type_str,"_element_addr(&",
1095-
evar,", 1, ",tvar,");"]) => stmt_scalar &
1094+
Util.string_append_list([ivar," = *(",array_type_str,"_element_addr1(&",
1095+
evar,", 1, ",tvar,");"]) => stmt_scalar & (* Use fast implementation for 1 dim *)
10961096

10971097
Util.if(a,stmt_array,stmt_scalar) => stmt &
10981098
generate_algorithm_statements(stmts,tnr3) => (cfn4,tnr4) &
@@ -1987,6 +1987,32 @@ relation generate_scalar_lhs_cref : (Exp.Type, Exp.ComponentRef, int)
19871987
generate_scalar_lhs_cref(t,cref,tnr)
19881988
=> (cfn,var,tnr')
19891989

1990+
(* two special cases rules for 1 and 2 dimensions for faster code (no vararg) *)
1991+
rule Debug.fprintln("gcge","generating cref ccode") &
1992+
generate_indices(idx,tnr) => (cfn1,idxs1,tnr1) &
1993+
list_length idxs1 => 1 & (* ndims == 1*)
1994+
int_string 1 => ndims_str &
1995+
Util.string_delimit_list(idxs1,", ") => idxs_str &
1996+
exp_type_str(t,true) => type_str &
1997+
Util.string_append_list(["(*",type_str,"_element_addr1(&",id,", ",ndims_str,
1998+
", ",idxs_str,"))"]) => cref1
1999+
------------------------------------------------
2000+
generate_scalar_lhs_cref(t,Exp.CREF_IDENT(id,idx),tnr)
2001+
=> (cfn1,cref1,tnr1)
2002+
2003+
rule Debug.fprintln("gcge","generating cref ccode") &
2004+
generate_indices(idx,tnr) => (cfn1,idxs1,tnr1) &
2005+
list_length idxs1 => 2 & (* ndims == 2 *)
2006+
int_string 2 => ndims_str &
2007+
Util.string_delimit_list(idxs1,", ") => idxs_str &
2008+
exp_type_str(t,true) => type_str &
2009+
Util.string_append_list(["(*",type_str,"_element_addr2(&",id,", ",ndims_str,
2010+
", ",idxs_str,"))"]) => cref1
2011+
------------------------------------------------
2012+
generate_scalar_lhs_cref(t,Exp.CREF_IDENT(id,idx),tnr)
2013+
=> (cfn1,cref1,tnr1)
2014+
2015+
19902016
rule Debug.fprintln("gcge","generating cref ccode") &
19912017
generate_indices(idx,tnr) => (cfn1,idxs1,tnr1) &
19922018
list_length idxs1 => ndims &
@@ -2041,6 +2067,32 @@ end
20412067
relation generate_scalar_rhs_cref : (string, Exp.Type,Exp.Subscript list, int)
20422068
=> (CFunction, string, int) =
20432069

2070+
(* Two special rules for faster code when ndims == 1 or 2 *)
2071+
2072+
rule generate_indices(subs,tnr) => (cfn1,idxs1,tnr1) &
2073+
list_length idxs1 => 1 & (* ndims == 1*)
2074+
int_string 1 => ndims_str &
2075+
Util.string_delimit_list(idxs1,", ") => idxs_str &
2076+
exp_type_str (crt,true) => array_type_str &
2077+
Util.string_append_list(["(*",array_type_str,"_element_addr1(&",
2078+
cref_str,", ",ndims_str,
2079+
", ",idxs_str,"))"]) => cref1
2080+
--------------------
2081+
generate_scalar_rhs_cref (cref_str,crt,subs,tnr)
2082+
=> (cfn1,cref1,tnr1)
2083+
2084+
rule generate_indices(subs,tnr) => (cfn1,idxs1,tnr1) &
2085+
list_length idxs1 => 2 & (* ndims == 2*)
2086+
int_string 2 => ndims_str &
2087+
Util.string_delimit_list(idxs1,", ") => idxs_str &
2088+
exp_type_str (crt,true) => array_type_str &
2089+
Util.string_append_list(["(*",array_type_str,"_element_addr2(&",
2090+
cref_str,", ",ndims_str,
2091+
", ",idxs_str,"))"]) => cref1
2092+
--------------------
2093+
generate_scalar_rhs_cref (cref_str,crt,subs,tnr)
2094+
=> (cfn1,cref1,tnr1)
2095+
20442096
rule generate_indices(subs,tnr) => (cfn1,idxs1,tnr1) &
20452097
list_length idxs1 => ndims &
20462098
int_string ndims => ndims_str &

modeq/debug.rml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Debug:
3838
relation fprintl: (string, string list) => ()
3939
relation fprint_list: (string, 'a list, 'a => (), string) => ()
4040
relation fcall: (string, 'a => (), 'a) => ()
41+
relation notfcall: (string, 'a => (), 'a) => ()
4142
end
4243

4344
with "rtopts.rml"
@@ -134,6 +135,18 @@ relation fcall: (string, 'a => (), 'a) => () =
134135

135136
end
136137

138+
(* Call the given function (2nd arg) if the flag given in 1st arg is NOT set *)
139+
140+
relation notfcall: (string, 'a => (), 'a) => () =
141+
142+
rule RTOpts.debug_flag (flag) => false &
143+
func(str)
144+
------------
145+
notfcall (flag, func, str)
146+
147+
axiom notfcall (_,_,_)
148+
149+
end
137150

138151
relation fprint_list: (string, 'a list, 'a => (), string) => () =
139152

modeq/main.rml

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,86 @@ with "inst.rml"
4343
with "rtopts.rml"
4444
with "debug.rml"
4545
with "codegen.rml"
46+
with "socket.rml"
47+
48+
(** relation: server_loop
49+
**
50+
** This relation is the main loop of the server listening to a port
51+
which recieves modelica expressions,
52+
**)
53+
54+
relation server_loop: int => () =
55+
56+
rule Socket.handlerequest shandle => str &
57+
Debug.fprint ("interactivedump" ,"------- Recieved Data from client -----\n") &
58+
Debug.fprint ("interactivedump" , str ) &
59+
Debug.fprint ("interactivedump", "------- End recieved Data-----\n") &
60+
handle_command str => true &
61+
Socket.sendreply(shandle,"OK\n") &
62+
server_loop shandle
63+
-----------------------------
64+
server_loop shandle
65+
66+
rule print "Exiting\n" &
67+
Socket.sendreply(shandle, "quit requested, shutting server down") &
68+
Socket.close shandle
69+
---------------------
70+
server_loop shandle
71+
72+
end
73+
74+
relation charlistcompare: (char list, char list, int) => bool =
75+
rule a = b
76+
-------------------
77+
charlistcompare(a::_,b::_,1) => true
78+
79+
rule int_sub(n,1) => n1 &
80+
a = b &
81+
charlistcompare(l1,l2,n1) => true
82+
-------------------------------------
83+
charlistcompare(a::l1,b::l2,n) => true
84+
85+
axiom charlistcompare(_,_,_) => false
86+
end
87+
88+
relation strncmp: (string,string,int)=>bool =
89+
rule string_list(s1) => clst1 &
90+
string_list(s2) => clst2 &
91+
string_length(s1) => s1len &
92+
string_length(s2) => s2len &
93+
int_ge(s1len,n) => true &
94+
int_ge(s2len,n) => true &
95+
charlistcompare(clst1,clst2,n) => true
96+
-------------------------------------
97+
strncmp (s1,s2,n) => true
98+
axiom strncmp(_,_,_) => false
99+
end
100+
(** relation handle_command
101+
**
102+
** This relation handles the commands in form of strings send to the server
103+
** If the command is quit, the relation returns false, otherwise it sends the string to the parse relation and returns true.
104+
**)
105+
relation handle_command: string => bool =
106+
rule let quitcmd = "quit()" &
107+
string_length(quitcmd) => slen &
108+
strncmp("quit()","quit()",6)=> true &
109+
strncmp("abc","cde",3) => false &
110+
print "Check completed\n" &
111+
strncmp("quit()",str,slen) => true
112+
---------------------------
113+
handle_command str => false
114+
115+
rule Parser.parsestring str => p
116+
& Debug.fprint ("dump", "\n--------------- Parsed program ---------------\n")
117+
& Debug.fcall ("dumpgraphviz", DumpGraphviz.dump, p)
118+
& Debug.fcall ("dump", Dump.dump, p)
119+
------------------------------------
120+
handle_command str => true
121+
122+
rule print "Error occured building AST\n"
123+
----------------------------------
124+
handle_command _ => true
125+
end
46126

47127
(** relation: translate_file
48128
**
@@ -109,7 +189,14 @@ relation fix_modelica_output : DAE.DAElist => DAE.DAElist =
109189
end
110190

111191

192+
relation interactivemode: string list => () =
112193

194+
rule Socket.waitforconnect 29500 => shandle &
195+
server_loop shandle
196+
-------------------
197+
interactivemode _
198+
end
199+
113200

114201
(** relation: main
115202
**
@@ -120,7 +207,8 @@ end
120207
relation main : string list => () =
121208

122209
rule RTOpts.args args => args' &
123-
translate_file args'
210+
Debug.fcall ("interactive", interactivemode, args' ) &
211+
Debug.notfcall ("interactive", translate_file, args')
124212
--------------------
125213
main args
126214

modeq/parse.rml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
module Parser:
2525
with "absyn.rml"
2626
relation parse: string => Absyn.Program
27+
relation parsestring: string => Absyn.Program
2728
end
2829

2930

modeq/runtime/Makefile.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ SHELL = /bin/sh
1212
CC = gcc
1313
CFLAGS = -I$(RMLINCLUDE)
1414

15-
SRC = rtopts.c
15+
SRC = rtopts.c socketimpl.c
1616

1717
OBJ = $(SRC:.c=.o)
1818

1919
all: $(OBJ)
2020

2121
clean:
2222
$(RM) $(OBJ)
23+
24+

0 commit comments

Comments
 (0)