Skip to content

Commit

Permalink
partial fix for bug: #2106
Browse files Browse the repository at this point in the history
- a bit of a hack, more work is needed
  recordArray[i].recordComponent does not work yet, just recordArray[i] directly.
- generate array operations on all records in Model_functions.h
- models that now compile:
  Modelica.Media.Examples.RealGasAir.MoistAir* 
  Modelica.Media.Examples.Water95.*




git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16093 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed May 20, 2013
1 parent 300e1e2 commit 7d59278
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 23 deletions.
2 changes: 2 additions & 0 deletions Compiler/FrontEnd/Absyn.mo
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ encapsulated package Absyn
protected import Debug;
protected import Dump;
protected import System;
protected import Flags;

public
type Ident = String "An identifier, for example a variable name" ;
Expand Down Expand Up @@ -2663,6 +2664,7 @@ algorithm
// second element is: .Modelica.Fluid.Interfaces.FluidPort_a__OMC__88 port_a;
case(TPATH(p1,oad1), TPATH(p2,oad2))
equation
true = Flags.isSet(Flags.SCODE_INST_SHORTCUT);
i1 = pathLastIdent(p1);
i2 = pathLastIdent(p2);
pos1 = System.stringFind(i1, "__OMC__");
Expand Down
13 changes: 13 additions & 0 deletions Compiler/FrontEnd/SCode.mo
Original file line number Diff line number Diff line change
Expand Up @@ -5217,5 +5217,18 @@ algorithm
end match;
end propagatePrefixInnerOuter;

public function isPartial
"function: isPartial
Return true if Class is a partial."
input Element inClass;
output Boolean outBoolean;
algorithm
outBoolean := matchcontinue (inClass)
case CLASS(partialPrefix = PARTIAL()) then true;
case _ then false;
end matchcontinue;
end isPartial;


end SCode;

113 changes: 91 additions & 22 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2876,8 +2876,11 @@ template simulationFunctionsHeaderFile(String filePrefix, list<Function> functio
#ifdef __cplusplus
extern "C" {
#endif
<%recordDecls |> rd => recordDeclarationHeader(rd) ;separator="\n"%>
<%\n%>
<%recordDecls |> rd => recordDeclarationHeader(rd) ;separator="\n\n"%>
<%\n%>
<%functionHeaders(functions)%>
<%\n%>
#ifdef __cplusplus
}
#endif
Expand Down Expand Up @@ -3282,10 +3285,18 @@ end subscriptsToCStr;
template subscriptToCStr(Subscript subscript)
::=
match subscript
case INDEX(exp=ICONST(integer=i)) then i
case INDEX(exp=ENUM_LITERAL(index=i)) then i
case SLICE(exp=ICONST(integer=i)) then i
case WHOLEDIM(__) then "WHOLEDIM"
case INDEX(__) then
match exp
case ICONST(integer=i) then i
case ENUM_LITERAL(index=i) then i
case _ then
let &varDecls = buffer "" /*BUFD*/
let &preExp = buffer "" /*BUFD*/
let index = daeExp(exp, contextOther, &preExp, &varDecls)
'<%index%>'
end match
else "UNKNOWN_SUBSCRIPT"
end subscriptToCStr;

Expand All @@ -3307,10 +3318,6 @@ template crefToMStr(ComponentRef cr)
else "CREF_NOT_IDENT_OR_QUAL"
end crefToMStr;





template subscriptsToMStr(list<Subscript> subscripts)
::=
if subscripts then
Expand All @@ -3319,17 +3326,22 @@ end subscriptsToMStr;

template subscriptToMStr(Subscript subscript)
::=
let &preExp = buffer ""
let &varDecls = buffer ""
match subscript
case INDEX(exp=ICONST(integer=i)) then i
case INDEX(exp=ENUM_LITERAL(index=i)) then i
case SLICE(exp=ICONST(integer=i)) then i
case WHOLEDIM(__) then "WHOLEDIM"
case INDEX(__) then
match exp
case ICONST(integer=i) then i
case ENUM_LITERAL(index=i) then i
case _ then
let &varDecls = buffer "" /*BUFD*/
let &preExp = buffer "" /*BUFD*/
let index = daeExp(exp, contextOther, &preExp, &varDecls)
'<%index%>'
end match
else "UNKNOWN_SUBSCRIPT"
end subscriptToMStr;


template contextArrayCref(ComponentRef cr, Context context)
"Generates code for an array component reference depending on the context."
::=
Expand Down Expand Up @@ -3470,7 +3482,7 @@ template functionHeader(Function fn, Boolean inFunc)
>>
<<
typedef struct <%fname%>_rettype_s {
struct <%fname%> c1;
<%fname%> c1;
} <%fname%>_rettype;

<%fname%>_rettype omc_<%fname%>(<%funArgsStr%>);
Expand Down Expand Up @@ -3542,9 +3554,66 @@ template recordDeclarationHeader(RecordDeclaration recDecl)
match recDecl
case RECORD_DECL_FULL(__) then
<<
struct <%name%> {
typedef struct <%name%>_s {
<%variables |> var as VARIABLE(__) => '<%varType(var)%> <%crefStr(var.name)%>;' ;separator="\n"%>
};
} <%name%>;

/* functions for array operations on record <%name%> */
typedef base_array_t <%name%>_array;
static <%name%>* <%name%>_alloc(int n)
{
return alloc_elements(n,sizeof(<%name%>));
}
static <%name%> <%name%>_get(const <%name%>_array *a, size_t i) {
return ((<%name%> *) a->data)[i];
}
static <%name%> *<%name%>_ptrget(const <%name%>_array *a, size_t i) {
return ((<%name%> *) a->data) + i;
}
static void <%name%>_set(<%name%>_array *a, size_t i, <%name%> r) {
((<%name%> *) a->data)[i] = r;
}
static void alloc_<%name%>_array(<%name%>_array *dest, int ndims, ...) {
size_t elements = 0;
va_list ap;
va_start(ap, ndims);
elements = alloc_base_array(dest, ndims, ap);
va_end(ap);
dest->data = <%name%>_alloc(elements);
}
static void array_alloc_scalar_<%name%>_array(<%name%>_array* dest, int n, <%name%> first,...)
{
int i;
va_list ap;
simple_alloc_1d_base_array(dest, n, <%name%>_alloc(n));
va_start(ap,first);
<%name%>_set(dest, 0, first);
for(i = 1; i < n; ++i) {
<%name%>_set(dest, i, va_arg(ap,<%name%>));
}
va_end(ap);
}
static <%name%>* <%name%>_array_element_addr(const <%name%>_array * source,int ndims,...) {
va_list ap;
<%name%>* tmp;
va_start(ap,ndims);
tmp = <%name%>_ptrget(source, calc_base_index_va(source, ndims, ap));
va_end(ap);
return tmp;
}
static void copy_<%name%>_array_data(const <%name%>_array *source, <%name%>_array *dest)
{
size_t i, nr_of_elements;
assert(base_array_ok(source));
assert(base_array_ok(dest));
assert(base_array_shape_eq(source, dest));
nr_of_elements = base_array_nr_of_elements(source);
for(i = 0; i < nr_of_elements; ++i) {
<%name%>_set(dest, i, <%name%>_get(source, i));
}
}


<%recordDefinitionHeader(dotPath(defPath),
underscorePath(defPath),
listLength(variables))%>
Expand Down Expand Up @@ -3873,7 +3942,7 @@ template extType(Type type, Boolean isInput, Boolean isArray)
case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__))
then "void *"
case T_COMPLEX(complexClassType=RECORD(path=rname))
then 'struct <%underscorePath(rname)%>'
then '<%underscorePath(rname)%>'
case T_METATYPE(__) case T_METABOXED(__) then "modelica_metatype"
else error(sourceInfo(), 'Unknown external C type <%unparseType(type)%>')
match type case T_ARRAY(__) then s else if isInput then (if isArray then '<%match s case "const char*" then "" else "const "%><%s%>*' else s) else '<%s%>*'
Expand All @@ -3892,7 +3961,7 @@ template extTypeF77(Type type, Boolean isReference)
case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__))
then "void*"
case T_COMPLEX(complexClassType=RECORD(path=rname))
then 'struct <%underscorePath(rname)%>'
then '<%underscorePath(rname)%>'
case T_METATYPE(__) case T_METABOXED(__) then "void*"
else error(sourceInfo(), 'Unknown external F77 type <%unparseType(type)%>')
match type case T_ARRAY(__) then s else if isReference then '<%s%>*' else s
Expand Down Expand Up @@ -4445,7 +4514,7 @@ case RECORD_CONSTRUCTOR(__) then
let fname = underscorePath(name)
let retType = '<%fname%>_rettype'
let retVar = tempDecl(retType, &varDecls /*BUFD*/)
let structType = 'struct <%fname%>'
let structType = '<%fname%>'
let structVar = tempDecl(structType, &varDecls /*BUFD*/)
let _ = (locals |> var hasindex i1 fromindex 1 =>
varInitRecord(var, i1, structVar, &varDecls /*BUFD*/, &varInits /*BUFC*/) ; empty /* increase the counter! */
Expand Down Expand Up @@ -4611,7 +4680,7 @@ template unboxRecord(String recordVar, Type ty, Text &preExp, Text &varDecls)
::=
match ty
case T_COMPLEX(complexClassType = RECORD(path = path), varLst = vars) then
let tmpVar = tempDecl('struct <%underscorePath(path)%>', &varDecls)
let tmpVar = tempDecl('<%underscorePath(path)%>', &varDecls)
let &preExp += (vars |> TYPES_VAR(name = compname) hasindex offset fromindex 2 =>
let varType = mmcTypeShort(ty)
let untagTmp = tempDecl('modelica_metatype', &varDecls)
Expand Down Expand Up @@ -7646,7 +7715,7 @@ case CAST(__) then
let from = expTypeFromExpShort(exp)
let &preExp += 'cast_<%from%>_array_to_<%to%>(&<%expVar%>, &<%tvar%>);<%\n%>'
'<%tvar%>'
case T_COMPLEX(complexClassType=rec as RECORD(__)) then '(*((struct <%underscorePath(rec.path)%>*)&<%expVar%>))'
case T_COMPLEX(complexClassType=rec as RECORD(__)) then '(*((<%underscorePath(rec.path)%>*)&<%expVar%>))'
else
'(<%expVar%>) /* could not cast, using the variable as it is */'
end daeExpCast;
Expand Down Expand Up @@ -8417,7 +8486,7 @@ template expTypeShort(DAE.Type type)
case T_ARRAY(__) then expTypeShort(ty)
case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__))
then "complex"
case T_COMPLEX(__) then 'struct <%underscorePath(ClassInf.getStateName(complexClassType))%>'
case T_COMPLEX(__) then '<%underscorePath(ClassInf.getStateName(complexClassType))%>'
case T_METATYPE(__) case T_METABOXED(__) then "metatype"
case T_FUNCTION_REFERENCE_VAR(__) then "fnptr"
case T_UNKNOWN(__) then if acceptMetaModelicaGrammar() /* TODO: Don't do this to me! */
Expand Down Expand Up @@ -8514,7 +8583,7 @@ template expTypeFlag(DAE.Type ty, Integer flag)
match ty case T_COMPLEX(complexClassType=EXTERNAL_OBJ(__)) then
'modelica_<%expTypeShort(ty)%>'
else match ty case T_COMPLEX(__) then
'struct <%underscorePath(ClassInf.getStateName(complexClassType))%>'
'<%underscorePath(ClassInf.getStateName(complexClassType))%>'
else match ty case T_ARRAY(ty = t as T_COMPLEX(__)) then
'<%expTypeShort(t)%>'
else
Expand Down
1 change: 0 additions & 1 deletion Compiler/Template/CodegenUtil.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ template subscriptStr(Subscript subscript)
else "UNKNOWN_SUBSCRIPT"
end subscriptStr;


template initValXml(Exp initialValue)
::=
match initialValue
Expand Down

0 comments on commit 7d59278

Please sign in to comment.