Commit 5826a3f0 authored by Damien George's avatar Damien George

Initial support for MicroPython

parent e32aae96
......@@ -215,6 +215,8 @@ procedure BuildSupport is
C_Set_Language_To_BlackBox_Device;
when Language_QGenAda => C_Set_Language_To_QGenAda;
when Language_QGenC => C_Set_Language_To_QGenC;
when Language_MicroPython =>
C_Set_Language_To_MicroPython;
when others => Exit_On_Error (True,
"Language is currently not supported: "
& Source_Language'Img);
......
......@@ -162,6 +162,7 @@ package Imported_Routines is
procedure C_Set_Language_To_GUI;
procedure C_Set_Language_To_VHDL;
procedure C_Set_Language_To_System_C;
procedure C_Set_Language_To_MicroPython;
procedure C_Set_Native_Encoding;
procedure C_Set_UPER_Encoding;
procedure C_Set_ACN_Encoding;
......@@ -249,6 +250,8 @@ private
pragma Import (C, C_Set_Language_To_GUI, "Set_Language_To_GUI");
pragma Import (C, C_Set_Language_To_VHDL, "Set_Language_To_VHDL");
pragma Import (C, C_Set_Language_To_System_C, "Set_Language_To_System_C");
pragma Import (C, C_Set_Language_To_MicroPython,
"Set_Language_To_MicroPython");
pragma Import (C, C_Set_UPER_Encoding, "Set_UPER_Encoding");
pragma Import (C, C_Set_ACN_Encoding, "Set_ACN_Encoding");
pragma Import (C, C_Set_Native_Encoding, "Set_Native_Encoding");
......
......@@ -62,6 +62,9 @@ void c_preamble(FV * fv)
else if (rtds == fv->language) {
fprintf(vm_if, "#include \"glue_%s.h\"\n\n", fv->name);
}
else if (micropython == fv->language) {
fprintf(vm_if, "#include \"%s_mpy_bindings.h\"\n\n", fv->name);
}
if (hasparam) {
fprintf(vm_if, "#include \"C_ASN1_Types.h\"\n\n");
......@@ -714,7 +717,8 @@ void GLUE_C_Backend(FV * fv)
if (c == fv->language || gui == fv->language || ada == fv->language || vdm == fv->language
|| qgenada == fv->language || qgenc == fv->language
|| rtds == fv->language || cpp == fv->language || opengeode == fv->language) {
|| rtds == fv->language || cpp == fv->language || opengeode == fv->language
|| micropython == fv->language) {
Init_C_Glue_Backend(fv);
FOREACH(i, Interface, fv->interfaces, {
GLUE_C_ProvidedInterface(i);
......
This diff is collapsed.
/* Buildsupport is (c) 2008-2016 European Space Agency
* contact: maxime.perrotin@esa.int
* License is LGPL, check LICENSE file */
/* build_c_skeletons.c
this program generates empty C functions respecting the interfaces defined
in the interface view. it provides functions to invoke RI.
updated 10/06/2016: properly align params and use FOREACH
updated 20/04/2009 to disable in case "-onlycv" flag is set
updated 8/10/2009 to rename the user_code.h/c to FV_name.h/c like in the Ada backend
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <assert.h>
#include "my_types.h"
#include "practical_functions.h"
static FILE *user_code_py = NULL;
static bool gen_this_ri(Interface * i)
{
/*
* There can be duplicate RI name but one sync, the other async
* In that case, discard the async one (generated by VT to allow
* a mix of sync and async PI in one block
*/
if (asynch == i->synchronism) {
FOREACH(interface, Interface, i->parent_fv->interfaces, {
if (RI == interface->direction &&
!strcmp(interface->name, i->name) &&
synch == interface->synchronism) {
return false;
}
});
}
return true;
}
/* Import the RI into the module-level namespace of the Python script */
void import_RI_to_MicroPython_skel(Interface * i)
{
if (NULL == user_code_py)
return;
bool comma = false;
fprintf(user_code_py, "# %s_RI_%s(", i->parent_fv->name, i->name);
FOREACH (p, Parameter, i->in, {
fprintf(user_code_py, "%sIN_%s: asn1Scc%s",
comma ? "," : "", p->name, p->type);
comma = true;
});
FOREACH (p, Parameter, i->out, {
fprintf(user_code_py,"%sOUT_%s: asn1Scc%s",
comma ? "," : "", p->name, p->type);
comma = true;
});
fprintf(user_code_py, ")\n");
fprintf(user_code_py, "from taste import %s_RI_%s\n", i->parent_fv->name, i->name);
}
/* Generate the preamble of the skeleton script */
void micropython_skel_preamble(FV * fv)
{
int hasparam = 0;
/* Check if any interface needs ASN.1 types */
FOREACH(i, Interface, fv->interfaces, {
CheckForAsn1Params(i, &hasparam);}
);
/* c. user_code.py preamble (if applicable) */
if (NULL != user_code_py) {
fprintf(user_code_py,
"# User code: This file will not be overwritten by TASTE.\n\n");
fprintf(user_code_py, "import micropython\n\n");
FOREACH(i, Interface, fv->interfaces, {
if (i->direction == RI && gen_this_ri(i)) {
import_RI_to_MicroPython_skel(i);
}
});
fprintf(user_code_py, "\n");
// TODO need to generate dummy variables for the types
if (has_context_param(fv)) {
char *fv_no_underscore =
underscore_to_dash(fv->name, strlen(fv->name));
fprintf(user_code_py,
"/* Function static data is declared in this file : */\n");
fprintf(user_code_py, "#include \"Context-%s.h\"\n\n",
fv_no_underscore);
free(fv_no_underscore);
}
fprintf(user_code_py, "def %s_startup():\n", fv->name);
fprintf(user_code_py,
" # Write your initialization code here,\n"
" # but do not make any call to a required interface.\n"
" pass\n"
"\n\n");
}
}
/* Creates user_code.h, and if necessary user_code.c (if it did not exist) */
int Init_MicroPython_GW_Backend(FV * fv)
{
char *path = NULL;
char *filename = NULL;
if (NULL != fv->system_ast->context->output) {
build_string(&path, fv->system_ast->context->output,
strlen(fv->system_ast->context->output));
}
build_string(&path, fv->name, strlen(fv->name));
build_string(&filename, fv->name, strlen(fv->name));
build_string(&filename, ".h", strlen(".h"));
filename[strlen(filename) - 1] = 'p'; /* change from .c to .p */
build_string(&filename, "y", 1); /* change from .p to .py */
if (!file_exists(path, filename)) {
printf("MicroPython: creating fresh skeleton %s/%s\n", path, filename);
create_file(path, filename, &user_code_py);
}
free(path);
micropython_skel_preamble(fv);
return 0;
}
void close_micropython_skel_files()
{
close_file(&user_code_py);
}
/* Add a Provided interface. Can contain in and out parameters
* Write in user_code.h the declaration of the user functions
* and if user_code.c is new, copy these declarations there too.
*/
void add_PI_to_MicroPython_skel(Interface * i)
{
if (NULL == user_code_py)
return;
char *signature_py = make_string("def %s_PI_%s(",
i->parent_fv->name,
i->name);
char *sep_py = make_string(",\n%*s", strlen(signature_py), "");
bool comma = false;
fprintf(user_code_py, "%s", signature_py);
#if 0
// This code generates type hints for the parameters, but need to have the identifiers defined as globals first...
FOREACH (p, Parameter, i->in, {
fprintf(user_code_py, "%sIN_%s : asn1Scc%s",
comma ? sep_py : "", p->name, p->type);
comma = true;
});
FOREACH (p, Parameter, i->out, {
fprintf(user_code_py, "%sOUT_%s : asn1Scc%s",
comma ? sep_py : "", p->name, p->type);
comma = true;
});
#else
// This code generates the parameter list without type hints.
FOREACH (p, Parameter, i->in, {
fprintf(user_code_py, "%sIN_%s", comma ? sep_py : "", p->name);
comma = true;
});
FOREACH (p, Parameter, i->out, {
fprintf(user_code_py, "%sOUT_%s", comma ? sep_py : "", p->name);
comma = true;
});
#endif
fprintf(user_code_py, "):\n # Write your code here!\n pass\n\n");
free(signature_py);
free(sep_py);
}
/* Add timer declarations to the C code skeletons */
void MicroPython_Add_timers(FV *fv)
{
(void)fv;
}
void End_MicroPython_GW_Backend()
{
close_micropython_skel_files();
}
/* External interface (the one and unique) */
void GW_MicroPython_Backend(FV * fv)
{
if (get_context()->onlycv)
return;
if (micropython == fv->language) {
Init_MicroPython_GW_Backend(fv);
FOREACH(i, Interface, fv->interfaces, {
if (i->direction == PI) {
add_PI_to_MicroPython_skel(i);
}
});
// If any timers, add declarations in code skeleton
MicroPython_Add_timers(fv);
End_MicroPython_GW_Backend();
}
}
......@@ -252,6 +252,8 @@ void Create_script()
break;
case qgenc: fprintf (script, "--subQGenC ");
break;
case micropython: fprintf (script, "--subMicroPython ");
break;
default:
ERROR ("[ERROR] Unsupported language (function %s)\n", fv->name);
ERROR (" -> please manually check the build-script.sh file\n");
......
......@@ -251,6 +251,12 @@ void Set_Language_To_System_C()
fv->language = system_c;
}
void Set_Language_To_MicroPython()
{
if (NULL != fv)
fv->language = micropython;
}
void Set_Zipfile (char *file, size_t len)
{
assert (NULL != file && 0 < len && NULL != fv);
......
......@@ -132,6 +132,7 @@ void C_End()
GW_SCADE_Backend(fv);
GW_RTDS_Backend(fv);
GW_Driver_Backend(fv);
GW_MicroPython_Backend(fv);
}
/* Export to SMP2: generate glue code and Python AST */
......@@ -201,6 +202,7 @@ void C_End()
GLUE_Ada_Wrappers_Backend(fv);
GLUE_C_Wrappers_Backend(fv);
GLUE_VT_Backend(fv);
GLUE_MicroPython_Backend(fv);
}
})
......
......@@ -27,6 +27,7 @@ Copyright 2014-2015 IB Krates <info@krates.ee>
#define ENCODING(i) (sdl==i->parent_fv->language)?"SDL":\
(simulink==i->parent_fv->language)?"Simulink":\
(micropython==i->parent_fv->language)?"MicroPython":\
(qgenc==i->parent_fv->language)?"QGenC":\
(ada==i->parent_fv->language)?"Ada":\
(vdm==i->parent_fv->language)?"Vdm":\
......
......@@ -20,6 +20,7 @@ extern void GW_Ada_Backend(FV *);
extern void GW_SCADE_Backend(FV *);
extern void GW_RTDS_Backend(FV *);
extern void GW_Driver_Backend(FV *);
extern void GW_MicroPython_Backend(FV *);
extern void Create_dataview_uniq();
extern void Delete_dataview_uniq();
......@@ -39,6 +40,7 @@ extern void GLUE_GUI_Backend(FV *);
extern void GLUE_Ada_Wrappers_Backend(FV *);
extern void GLUE_C_Wrappers_Backend(FV *);
extern void GLUE_VT_Backend(FV *);
extern void GLUE_MicroPython_Backend(FV *);
extern void Backdoor_backend(System *);
extern void Generate_Build_Script();
extern void Generate_Full_ConcurrencyView(Process_list *, char *);
......
......@@ -76,6 +76,7 @@ void Set_Language_To_C();
void Set_Language_To_Other();
void Set_Language_To_Simulink();
void Set_Language_To_SDL();
void Set_Language_To_MicroPython();
void Set_ASN1_BasicType_Unknown();
void Set_ASN1_BasicType_OctetString();
void Set_ASN1_BasicType_Choice();
......
......@@ -73,6 +73,7 @@ typedef enum {
system_c,
blackbox_device,
vdm,
micropython,
other
} Language;
......
......@@ -19,7 +19,7 @@
/* Return a string representing the selected encoding rules for a Parameter */
#define BINARY_ENCODING(p) (native==p->encoding)?"NATIVE":(uper==p->encoding)?"UPER":(acn==p->encoding)?"ACN":"#ERROR#"
#define LANGUAGE(fv) ada==fv->language?"Ada": c==fv->language?"C": sdl==fv->language?"OG": rtds==fv->language?"RTDS": scade==fv->language?"SCADE6":simulink==fv->language?"SIMULINK": blackbox_device==fv->language? "C": rhapsody==fv->language? "C": vhdl==fv->language?"VHDL": system_c==fv->language?"SYSTEM_C":gui==fv->language?"GUI": qgenc==fv->language?"QGenC": qgenada==fv->language?"QGenAda": cpp==fv->language?"CPP": vdm==fv->language?"Vdm": "UNSUPPORTED_LANGUAGE!"
#define LANGUAGE(fv) ada==fv->language?"Ada": c==fv->language?"C": sdl==fv->language?"OG": rtds==fv->language?"RTDS": scade==fv->language?"SCADE6":simulink==fv->language?"SIMULINK": blackbox_device==fv->language? "C": rhapsody==fv->language? "C": vhdl==fv->language?"VHDL": system_c==fv->language?"SYSTEM_C":gui==fv->language?"GUI": qgenc==fv->language?"QGenC": qgenada==fv->language?"QGenAda": cpp==fv->language?"CPP": vdm==fv->language?"Vdm": micropython==fv->language?"C": "UNSUPPORTED_LANGUAGE!"
#define RCM_KIND(IF) (protected==IF->rcm?"protected":unprotected==IF->rcm?"unprotected":sporadic==IF->rcm?"sporadic":cyclic==IF->rcm?"cyclic":variator==IF->rcm?"variator":"unknown")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment