Skip to content

Commit

Permalink
Added run-time handling of (MetaModelica) lists to the C runtime. Two…
Browse files Browse the repository at this point in the history
… new files.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2869 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Kristian Stavåker committed Aug 16, 2007
1 parent 489e7cc commit e3622b7
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 2 deletions.
5 changes: 3 additions & 2 deletions c_runtime/Makefile
Expand Up @@ -23,12 +23,13 @@ biglag.o ddassl.o dogleg.o fdjac1.o hybrj.o newuoa.o qrfac.o trsapp.o
daux.o dlamch.o dpmpar.o hybrd.o lsame.o newuob.o r1mpyq.o update.o $(EXTRA_OBJS)

SIMOBJS = $(FOBJS) simulation_runtime.o simulation_init.o simulation_input.o simulation_events.o \
solver_dasrt.o solver_euler.o simulation_result.o ../mosh/src/options.o dgesv_aux.o $(EXTRA_SIMOBJS)
solver_dasrt.o solver_euler.o simulation_result.o ../mosh/src/options.o dgesv_aux.o meta_modelica.o $(EXTRA_SIMOBJS)

HFILES = blaswrap.h f2c.h integer_array.h memory_pool.h modelica_string.h \
real_array.h string_array.h boolean_array.h index_spec.h matrix.h \
modelica.h read_write.h simulation_runtime.h simulation_events.h utility.h \
simulation_init.h simulation_input.h solver_dasrt.h solver_euler.h simulation_result.h
simulation_init.h simulation_input.h solver_dasrt.h solver_euler.h simulation_result.h \
meta_modelica.h

LIBS = libc_runtime.a libsim.a

Expand Down
133 changes: 133 additions & 0 deletions c_runtime/meta_modelica.c
@@ -0,0 +1,133 @@
/*
Copyright (c) 1998-2007, Linköpings universitet, Department of
Computer and Information Science, PELAB
All rights reserved.
(The new BSD license, see also
http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Linköpings universitet nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "modelica.h"

struct mmc_header {
mmc_uint_t header;
};

const struct mmc_header mmc_prim_nil = { MMC_NILHDR };


struct mmc_struct {
mmc_uint_t header; /* MMC_STRUCTHDR(slots,ctor) */
void *data[1]; /* `slots' elements */
};

struct mmc_real {
mmc_uint_t header; /* MMC_REALHDR */
mmc_uint_t data[MMC_SIZE_DBL/MMC_SIZE_INT];
};

struct mmc_string {
mmc_uint_t header; /* MMC_STRINGHDR(bytes) */
char data[1]; /* `bytes' elements + terminating '\0' */
};

union mmc_double_as_words {
double d;
mmc_uint_t data[2];
};

void *alloc_bytes(unsigned nbytes)
{
void *p;
if( (p = malloc(nbytes)) == 0 ) {
fprintf(stderr, "malloc(%u) failed: %s\n", nbytes, strerror(errno));
exit(1);
}
return p;
}

void *alloc_words(unsigned nwords)
{
return alloc_bytes(nwords * sizeof(void*));
}

void mmc_prim_set_real(struct mmc_real *p, double d)
{
union mmc_double_as_words u;
u.d = d;
p->data[0] = u.data[0];
p->data[1] = u.data[1];
}

void *mmc_mk_nil(void)
{
return MMC_TAGPTR(&mmc_prim_nil);
}

void *mmc_mk_cons(void *car, void *cdr)
{
return mmc_mk_box2(1, car, cdr);
}

void *mmc_mk_box2(unsigned ctor, void *x0, void *x1)
{
struct mmc_struct *p = alloc_words(3);
p->header = MMC_STRUCTHDR(2, ctor);
p->data[0] = x0;
p->data[1] = x1;
return MMC_TAGPTR(p);
}

void *mmc_mk_icon(int i)
{
return MMC_IMMEDIATE(MMC_TAGFIXNUM((mmc_sint_t)i));
}

void *mmc_mk_rcon(double d)
{
struct mmc_real *p = alloc_words(MMC_SIZE_DBL/MMC_SIZE_INT + 1);
mmc_prim_set_real(p, d);
p->header = MMC_REALHDR;
return MMC_TAGPTR(p);
}

void *mmc_mk_scon(char *s)
{
unsigned nbytes = strlen(s);
unsigned header = MMC_STRINGHDR(nbytes);
unsigned nwords = MMC_HDRSLOTS(header) + 1;
struct mmc_string *p = alloc_words(nwords);
p->header = header;
memcpy(p->data, s, nbytes+1); /* including terminating '\0' */
return MMC_TAGPTR(p);
}
87 changes: 87 additions & 0 deletions c_runtime/meta_modelica.h
@@ -0,0 +1,87 @@
/*
Copyright (c) 1998-2007, Linköpings universitet, Department of
Computer and Information Science, PELAB
All rights reserved.
(The new BSD license, see also
http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name of Linköpings universitet nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* File: meta_modelica.h
* Description: This is the C header file for the C code generated from
* Modelica. It includes e.g. the C object representation of the builtin types
* and arrays, etc.
*/

#ifndef META_MODELICA_H_
#define META_MODELICA_H_


#if defined(__cplusplus)
extern "C" {
#endif

#define MMC_SIZE_DBL 8
#define MMC_SIZE_INT 4
#define MMC_LOG2_SIZE_INT 2
#define MMC_TAGPTR(p) ((void*)((char*)(p) + 3))
#define MMC_UNTAGPTR(x) ((void*)((char*)(x) - 3))
#define MMC_STRUCTHDR(slots,ctor) (((slots) << 10) + (((ctor) & 255) << 2))
#define MMC_NILHDR MMC_STRUCTHDR(0,0)
#define MMC_CONSHDR MMC_STRUCTHDR(2,1)
#define MMC_OFFSET(p,i) ((void*)((void**)(p) + (i)))
#define MMC_FETCH(p) (*(void**)(p))
#define MMC_CAR(X) MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(X),1))
#define MMC_CDR(X) MMC_FETCH(MMC_OFFSET(MMC_UNTAGPTR(X),2))
#define MMC_NILTEST(x) (MMC_GETHDR(x) == MMC_NILHDR)
#define MMC_IMMEDIATE(i) ((void*)(i))
#define MMC_TAGFIXNUM(i) ((i) << 1)
#define MMC_REALHDR (((MMC_SIZE_DBL/MMC_SIZE_INT) << 10) + 9)
#define MMC_STRINGHDR(nbytes) (((nbytes)<<(10-MMC_LOG2_SIZE_INT))+((1<<10)+5))
#define MMC_HDRSLOTS(hdr) ((hdr) >> 10)

typedef unsigned int mmc_uint_t;
typedef int mmc_sint_t;

extern void *mmc_mk_nil(void);
extern void *mmc_mk_cons(void *car, void *cdr);
extern void *mmc_mk_box2(unsigned ctor, void *x0, void *x1);
extern void *mmc_mk_icon(int i);
extern void *mmc_mk_rcon(double d);
extern void *mmc_mk_scon(char *s);

#if defined(__cplusplus)
}
#endif

#endif
1 change: 1 addition & 0 deletions c_runtime/modelica.h
Expand Up @@ -69,6 +69,7 @@ typedef void* modelica_complex; /* currently only External objects are represent
#include <assert.h>
#include "read_write.h"
#include "matrix.h"
#include "meta_modelica.h"


typedef real_array_t real_array;
Expand Down

0 comments on commit e3622b7

Please sign in to comment.