Adding support for SAFEBOOL. Some code generation yet to do...
authormario
Mon, 25 May 2009 12:17:10 +0200
changeset 177 697562a5da7c
parent 176 bef3cc16c064
child 178 1622dc05c6ca
Adding support for SAFEBOOL. Some code generation yet to do...
absyntax/absyntax.def
main.cc
stage1_2/iec.flex
stage1_2/iec.y
stage1_2/stage1_2.cc
stage1_2/stage1_2.hh
stage1_2/stage1_2_priv.hh
stage4/generate_c/generate_c_base.cc
stage4/generate_c/search_base_type.cc
stage4/generate_c/search_constant_type.cc
stage4/generate_iec/generate_iec.cc
--- a/absyntax/absyntax.def	Tue May 19 16:31:35 2009 +0200
+++ b/absyntax/absyntax.def	Mon May 25 12:17:10 2009 +0200
@@ -37,6 +37,52 @@
  */
 
 
+
+
+
+
+/*****************************************************************/
+/*****************************************************************/
+/*****************************************************************/
+/*****************************************************************/
+/*                                                               */
+/*             Symbols defined in:                               */
+/*      PLCopen - Technical Committee 5                          */
+/*      Safety Software Technical Specification,                 */
+/*      Part 1: Concepts and Function Blocks,                    */
+/*      Version 1.0 – Official Release - 2006-01-31              */
+/*                                                               */
+/*****************************************************************/
+/*****************************************************************/
+/*****************************************************************/
+/*****************************************************************/
+
+
+
+
+SYM_REF0(safebool_type_name_c)
+
+
+
+
+
+/*****************************************************************/
+/*****************************************************************/
+/*****************************************************************/
+/*****************************************************************/
+/*                                                               */
+/*             Symbols defined in:                               */
+/*      FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)          */
+/*                                                               */
+/*****************************************************************/
+/*****************************************************************/
+/*****************************************************************/
+/*****************************************************************/
+
+/* Based on the                                    */
+/* FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10) */
+
+
 /* EN/ENO */
 SYM_REF0(en_param_c)
 SYM_REF0(eno_param_c)
--- a/main.cc	Tue May 19 16:31:35 2009 +0200
+++ b/main.cc	Mon May 25 12:17:10 2009 +0200
@@ -65,9 +65,6 @@
 #include <stdlib.h>  // EXIT_FAILURE
 #include "absyntax/absyntax.hh"  // symbol_c type
 
-
-
-
 /* A macro for printing out internal parser errors... */
 #include <iostream> // required for std::cerr
 #define ERROR error_exit(__FILE__,__LINE__)
@@ -79,67 +76,84 @@
 
 
 
-/* forward declarations... */
-int stage1_2(const char *filename, const char *includedir, symbol_c **tree_root, bool full);
+
+#include "stage1_2/stage1_2.hh"
+
 //int stage3(symbol_c *tree_root);
 int stage4(symbol_c *tree_root, const char *builddir);
 
 
 static void printusage(const char *cmd) {
-  printf("syntaxe: %s [-fh] <input_file> [-I <include_directory>] [<target_directory>]\n", cmd);
+  printf("syntax: %s [-h] [-f] [-s] [-I <include_directory>] [-T <target_directory>] <input_file>\n", cmd);
+  printf("  h : show this help message\n");
   printf("  f : full token location on error messages\n");
-  printf("  h : show this help message\n");
+      /******************************************************/
+      /* whether we are suporting safe extensions           */
+      /* as defined in PLCopen - Technical Committee 5      */
+      /* Safety Software Technical Specification,           */
+      /* Part 1: Concepts and Function Blocks,              */
+      /* Version 1.0 – Official Release                     */
+      /******************************************************/
+  printf("  s : allow use of safe extensions\n");
 }
 
 
 
 int main(int argc, char **argv) {
   symbol_c *tree_root;
-  char * includedir = NULL;
   char * builddir = NULL;
-  bool full = false;
-  int offset = 0; 
+  stage1_2_options_t stage1_2_options = {false, false, NULL};
+  int optres, errflg = 0;
+/*
+  extern char *optarg;
+  extern int optind, optopt;
+*/
 
-  if (argc < 2 || argc > 6) {
-    printusage(argv[0]);
-    return EXIT_FAILURE;
+  while ((optres = getopt(argc, argv, ":hfsI:T:")) != -1) {
+    switch(optres) {
+    case 'h':
+      printusage(argv[0]);
+      return 0;
+      break;
+    case 'f':
+      stage1_2_options.full_token_loc = true;
+      break;
+    case 's':
+      stage1_2_options.safe_extensions = true;
+      break;
+    case 'I':
+      stage1_2_options.includedir = optarg;
+      break;
+    case ':':       /* -I or -T without operand */
+      fprintf(stderr, "Option -%c requires an operand\n", optopt);
+      errflg++;
+      break;
+    case '?':
+    default:
+      fprintf(stderr, "Unrecognized option: -%c\n", optopt);
+      errflg++;
+      break;
+    }
   }
 
-  if (strcmp(argv[1], "-h") == 0) {
-    printusage(argv[0]);
-    return 0;
+  if (optind == argc) {
+    fprintf(stderr, "Missing input file\n");
+    errflg++;
   }
 
-  if (strcmp(argv[1], "-f") == 0) {
-    if (argc == 2) {
+  if (optind > argc) {
+    fprintf(stderr, "Too many input files\n");
+    errflg++;
+  }
+
+  if (errflg) {
       printusage(argv[0]);
       return EXIT_FAILURE;
-    }
-    full = true;
-    offset = 1;
-  }
-    
-  if (argc == (5 + offset)) {
-    builddir = argv[4 + offset];
-    argc = 4 + offset;
   }
 
-  if (argc == (4 + offset)) {
-    if (strcmp(argv[2 + offset], "-I") != 0) {
-      printusage(argv[0]);
-      return EXIT_FAILURE;
-    }
-    includedir = argv[3 + offset];
-    argc = 2 + offset;
-  }
-
-  if (argc == (3 + offset)) {
-    builddir = argv[2 + offset];
-    argc = 2 + offset;
-  }    
 
   /* 1st Pass */
-  if (stage1_2(argv[1 + offset], includedir, &tree_root, full) < 0)
+  if (stage1_2(argv[optind], &tree_root, stage1_2_options) < 0)
     return EXIT_FAILURE;
 
   /* 2nd Pass */
--- a/stage1_2/iec.flex	Tue May 19 16:31:35 2009 +0200
+++ b/stage1_2/iec.flex	Mon May 25 12:17:10 2009 +0200
@@ -1122,7 +1122,6 @@
 WSTRING		return WSTRING;		/* Keyword (Data Type) */
 STRING		return STRING;		/* Keyword (Data Type) */
 BOOL		return BOOL;		/* Keyword (Data Type) */
-
 TIME		return TIME;		/* Keyword (Data Type) */
 DATE		return DATE;		/* Keyword (Data Type) */
 DT		return DT;		/* Keyword (Data Type) */
@@ -1389,6 +1388,20 @@
 EXIT		return EXIT;		/* Keyword */
 
 
+	/*****************************************************************/
+	/* Keywords defined in "Safety Software Technical Specification" */
+	/*****************************************************************/
+        /* 
+         * NOTE: The following keywords are define in 
+         *       "Safety Software Technical Specification,
+         *        Part 1: Concepts and Function Blocks,  
+         *        Version 1.0 – Official Release"
+         *        written by PLCopen - Technical Committee 5
+         *
+         *        We only support these extensions and keywords
+         *        if the apropriate command line option is given.
+         */
+SAFEBOOL	{if (get_opt_safe_extensions()) {return SAFEBOOL;} else{REJECT;}}	/* Keyword (Data Type) */ 
 
 
 
@@ -1566,7 +1579,6 @@
 int get_direct_variable_token(const char *direct_variable_str) {return 0;}
 
 
-
 int main(int argc, char **argv) {
 
   FILE *in_file;
--- a/stage1_2/iec.y	Tue May 19 16:31:35 2009 +0200
+++ b/stage1_2/iec.y	Mon May 25 12:17:10 2009 +0200
@@ -133,12 +133,24 @@
  *       fact be defined towards the end of this same file (i.e. in the prologue)
  */
 
+
+/* NOTE: These variable are really parameters we would like the stage2__ function to pass
+ *       to the yyparse() function. However, the yyparse() function is created automatically
+ *       by bison, so we cannot add parameters to this function. The only other
+ *       option is to use global variables! yuck!
+ */
+
 /* A global flag used to tell the parser if overloaded funtions should be allowed.
  * The IEC 61131-3 standard allows overloaded funtions in the standard library,
  * but disallows them in user code...
  */
 extern bool allow_function_overloading;
 
+/* A global flag used to tell the parser whether to include the full variable location
+ * when printing out error messages...
+ */
+extern bool full_token_loc;
+
 /* A pointer to the root of the parsing tree that will be generated 
  * by bison.
  */
@@ -1247,6 +1259,16 @@
 
 %token EXIT
 
+
+/******************************************************/
+/* Symbols defined in                                 */
+/* "Safety Software Technical Specification,          */
+/*  Part 1: Concepts and Function Blocks,             */
+/*  Version 1.0 – Official Release"                   */
+/* by PLCopen - Technical Committee 5 - 2006-01-31    */
+/******************************************************/
+%token SAFEBOOL
+
 %%
 
 
@@ -2218,6 +2240,14 @@
  * and grouping type definition for reason why BOOL
  * was added to this definition.
  */
+    /******************************************************/
+    /* Symbols defined in                                 */
+    /* "Safety Software Technical Specification,          */
+    /*  Part 1: Concepts and Function Blocks,             */
+    /*  Version 1.0 – Official Release"                   */
+    /* by PLCopen - Technical Committee 5 - 2006-01-31    */
+    /******************************************************/
+| SAFEBOOL	{$$ = new safebool_type_name_c(locloc(@$));}
 ;
 
 numeric_type_name:
@@ -7409,12 +7439,29 @@
 extern tracking_t* current_tracking;
 
 
+
+
+/*************************************************************************************************/
+/* NOTE: These variables are really parameters we would like the stage2__ function to pass       */
+/*       to the yyparse() function. However, the yyparse() function is created automatically     */
+/*       by bison, so we cannot add parameters to this function. The only other                  */
+/*       option is to use global variables! yuck!                                                */ 
+/*************************************************************************************************/
+
 /* A global flag used to tell the parser if overloaded funtions should be allowed.
  * The IEC 61131-3 standard allows overloaded funtions in the standard library,
  * but disallows them in user code...
+ *
+ * In essence, a parameter we would like to pass to the yyparse() function but
+ * have to do it using a global variable, as the yyparse() prototype is fixed by bison.
  */
 bool allow_function_overloading = false;
 
+/* A global flag used to tell the parser whether to include the full variable location
+ * when printing out error messages...
+ */
+bool full_token_loc;
+
 /* A pointer to the root of the parsing tree that will be generated 
  * by bison.
  */
@@ -7437,7 +7484,6 @@
 /*  print_include_stack(); */
 }
 
-bool full_error;
 
 bool is_current_syntax_token() {
 	switch (yychar) {
@@ -7474,7 +7520,7 @@
                    int last_line,
                    int last_column,
                    const char *additional_error_msg) {
-  if (full_error)
+  if (full_token_loc)
   	fprintf(stderr, "%s:%d-%d..%d-%d: error : %s\n", current_filename, first_line, first_column, last_line, last_column, additional_error_msg);
   else
   	fprintf(stderr, "%s:%d: error : %s\n", current_filename, first_line, additional_error_msg);
@@ -7603,12 +7649,18 @@
 
 extern const char *INCLUDE_DIRECTORIES[];
 
-int stage1_2__(const char *filename, const char *includedir, symbol_c **tree_root_ref, bool full) {
+
+
+int stage2__(const char *filename, 
+             const char *includedir,     /* Include directory, where included files will be searched for... */
+             symbol_c **tree_root_ref,
+             bool full_token_loc_        /* error messages specify full token location */
+            ) {
+
   FILE *in_file = NULL, *lib_file = NULL;
   char *libfilename = NULL;
 	
-	full_error = full;
-	
+
   if((in_file = fopen(filename, "r")) == NULL) {
     char *errmsg = strdup2("Error opening main file ", filename);
     perror(errmsg);
@@ -7640,6 +7692,7 @@
   /* first parse the standard library file... */
   yyin = lib_file;
   allow_function_overloading = true;
+  full_token_loc = full_token_loc_;
   current_filename = libfilename;
   current_tracking = GetNewTracking(yyin);
   if (yyparse() != 0)
@@ -7672,6 +7725,7 @@
   /* now parse the input file... */
   yyin = in_file;
   allow_function_overloading = false;
+  full_token_loc = full_token_loc_;
   current_filename = filename;
   current_tracking = GetNewTracking(yyin);
   {int res;
--- a/stage1_2/stage1_2.cc	Tue May 19 16:31:35 2009 +0200
+++ b/stage1_2/stage1_2.cc	Mon May 25 12:17:10 2009 +0200
@@ -35,7 +35,7 @@
 #include "../absyntax/absyntax.hh"
 
 
-// #include "stage1_2.hh"
+#include "stage1_2.hh"
 #include "iec.y.hh"
 #include "stage1_2_priv.hh"
 
@@ -57,7 +57,15 @@
 const char *current_filename = NULL;
 
 
-
+/******************************************************/
+/* whether we are suporting safe extensions           */
+/* as defined in PLCopen - Technical Committee 5      */
+/* Safety Software Technical Specification,           */
+/* Part 1: Concepts and Function Blocks,              */
+/* Version 1.0 – Official Release                     */
+/******************************************************/
+bool safe_extensions_ = false;
+bool get_opt_safe_extensions() {return safe_extensions_;}
 
 /****************************************************/
 /* Controlling the entry to the body_state in flex. */
@@ -263,10 +271,26 @@
 /***********************************************************************/
 
 
-int stage1_2__(const char *filename, const char *includedir, symbol_c **tree_root_ref, bool full);
-
-
-int stage1_2(const char *filename, const char *includedir, symbol_c **tree_root_ref, bool full) {
-  return stage1_2__(filename, includedir, tree_root_ref, full);
-}
-
+int stage2__(const char *filename, 
+             const char *includedir,     /* Include directory, where included files will be searched for... */
+             symbol_c **tree_root_ref,
+             bool full_token_loc         /* error messages specify full token location */
+            );
+
+
+int stage1_2(const char *filename, symbol_c **tree_root_ref, stage1_2_options_t options) {
+      /* NOTE: we only call stage2 (bison - syntax analysis) directly, as stage 2 will itself call stage1 (flex - lexical analysis)
+       *       automatically as needed
+       */
+
+      /* NOTE: Since we do not call stage1__ (flex) directly, we cannot directly pass any parameters to that function either.
+       *       In this case, we use callback functions, i.e. stage1__ (i.e. flex) will call functions defined in this file
+       *       whenever it needs info/parameters coming from stage1_2().
+       *       These callback functions will get their data from local (to this file) global variables...
+       *       We now set those variables...
+       */
+  safe_extensions_ = options.safe_extensions;
+  
+  return stage2__(filename, options.includedir, tree_root_ref, options.full_token_loc);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stage1_2/stage1_2.hh	Mon May 25 12:17:10 2009 +0200
@@ -0,0 +1,65 @@
+/*
+ * (c) 2009 Mario de Sousa
+ *
+ * Offered to the public under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2 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.
+ *
+ * This code is made available on the understanding that it will not be
+ * used in safety-critical situations without a full and competent review.
+ */
+
+/*
+ * An IEC 61131-3 IL and ST compiler.
+ *
+ * Based on the
+ * FINAL DRAFT - IEC 61131-3, 2nd Ed. (2001-12-10)
+ *
+ */
+
+
+/*
+ * The public interface to stage1_2.cc 
+ */
+
+
+
+#ifndef _STAGE1_2_HH
+#define _STAGE1_2_HH
+
+
+
+/*
+ * This file includes the interface through which the main function accesses the stage1_2 services 
+ */
+
+
+
+typedef struct {
+		/******************************************************/
+		/* whether we are suporting safe extensions           */
+		/* as defined in PLCopen - Technical Committee 5      */
+		/* Safety Software Technical Specification,           */
+		/* Part 1: Concepts and Function Blocks,              */
+		/* Version 1.0 – Official Release                     */
+		/******************************************************/
+	bool safe_extensions;
+		/* error messages specify full token location */
+	bool full_token_loc; 
+		/* Include directory, where included files will be searched for... */
+	const char *includedir;
+} stage1_2_options_t;
+
+
+int stage1_2(const char *filename, symbol_c **tree_root, stage1_2_options_t options);
+
+
+
+
+
+#endif   /* _STAGE1_2_HH */
--- a/stage1_2/stage1_2_priv.hh	Tue May 19 16:31:35 2009 +0200
+++ b/stage1_2/stage1_2_priv.hh	Mon May 25 12:17:10 2009 +0200
@@ -42,7 +42,7 @@
 
 /* file with the declarations of symbol tables... */
 #include "../util/symtable.hh"
-
+#include "stage1_2.hh"
 
 
 /*
@@ -52,6 +52,9 @@
  * This is mostly through direct access to shared global variables, however some
  * of the global variables will only be accessed through some accessor functions.
  *
+ * This file also includes the interface between the main stage1_2() functions and 
+ * the flex lexical parser.
+ *
  * This file also includes some utility functions (strdupX() ) that are both used
  * in the lexical and syntax analysers.
  */
@@ -59,6 +62,34 @@
 
 
 
+/*************************************************************/
+/*************************************************************/
+/****                                                    *****/
+/****  I n t e r f a c e    B e t w e e n                *****/
+/****           F l e x    a n d     s t a g e 1 _ 2 ()  *****/
+/****                                                    *****/
+/*************************************************************/
+/*************************************************************/
+
+/******************************************************/
+/* whether we are suporting safe extensions           */
+/* as defined in PLCopen - Technical Committee 5      */
+/* Safety Software Technical Specification,           */
+/* Part 1: Concepts and Function Blocks,              */
+/* Version 1.0 – Official Release                     */
+/******************************************************/
+bool get_opt_safe_extensions();
+
+
+
+/*************************************************************/
+/*************************************************************/
+/****                                                    *****/
+/****  I n t e r f a c e    B e t w e e n                *****/
+/****           F l e x    a n d     B i s o n           *****/
+/****                                                    *****/
+/*************************************************************/
+/*************************************************************/
 
 /*********************************************/
 /* print the include file stack to stderr... */
@@ -80,6 +111,7 @@
  */
 extern const char *current_filename;
 
+
 #define MAX_BUFFER_LENGTH 1000
 
 typedef struct {
@@ -141,6 +173,10 @@
 /*********************************/
 /* NOTE: only accessed indirectly by the lexical parser (flex)
  *       through the function get_identifier_token()
+ *
+ *       Bison accesses these data structures directly.
+ *
+ *       In essence, they are a data passing mechanism between Bison and Flex.
  */
 /* NOTE: BOGUS_TOKEN_ID is defined in the bison generated file iec.y.hh.
  *       We need this constant defined before we can declare the symbol tables.
@@ -189,9 +225,14 @@
 int get_direct_variable_token(const char *direct_variable_str);
 
 
-/************************/
-/* Utility Functions... */
-/************************/
+/*************************************************************/
+/*************************************************************/
+/****                                                    *****/
+/****  U t i l i t y   F u n c t i o n s ...             *****/
+/****                                                    *****/
+/****                                                    *****/
+/*************************************************************/
+/*************************************************************/
 
 /* Join two strings together. Allocate space with malloc(3). */
 char *strdup2(const char *a, const char *b);
--- a/stage4/generate_c/generate_c_base.cc	Tue May 19 16:31:35 2009 +0200
+++ b/stage4/generate_c/generate_c_base.cc	Mon May 25 12:17:10 2009 +0200
@@ -536,6 +536,14 @@
     void *visit(dword_type_name_c *symbol) {s4o.print("DWORD"); return NULL;}
     void *visit(string_type_name_c *symbol) {s4o.print("STRING"); return NULL;}
     void *visit(wstring_type_name_c *symbol) {s4o.print("WSTRING"); return NULL;}
+    /******************************************************/
+    /* Extensions to the base standard as defined in      */
+    /* "Safety Software Technical Specification,          */
+    /*  Part 1: Concepts and Function Blocks,             */
+    /*  Version 1.0 – Official Release"                   */
+    /* by PLCopen - Technical Committee 5 - 2006-01-31    */
+    /******************************************************/
+    void *visit(safebool_type_name_c *symbol) {s4o.print("SAFEBOOL"); return NULL;}
 
 /********************************/
 /* B.1.3.2 - Generic data types */
@@ -672,6 +680,7 @@
 /********************************/
   /* leave for derived classes... */
 
+
 }; /* class generate_c_basic_c */
 
 
--- a/stage4/generate_c/search_base_type.cc	Tue May 19 16:31:35 2009 +0200
+++ b/stage4/generate_c/search_base_type.cc	Mon May 25 12:17:10 2009 +0200
@@ -95,6 +95,14 @@
     void *visit(constant_int_type_name_c *symbol)    {return (void *)symbol;}
     void *visit(constant_real_type_name_c *symbol)    {return (void *)symbol;}
     void *visit(direct_variable_type_name_c *symbol)    {return (void *)symbol;}
+    /******************************************************/
+    /* Extensions to the base standard as defined in      */
+    /* "Safety Software Technical Specification,          */
+    /*  Part 1: Concepts and Function Blocks,             */
+    /*  Version 1.0 – Official Release"                   */
+    /* by PLCopen - Technical Committee 5 - 2006-01-31    */
+    /******************************************************/
+    void *visit(safebool_type_name_c *symbol)	{return (void *)symbol;}
 
 /********************************/
 /* B 1.3.3 - Derived data types */
@@ -225,6 +233,8 @@
 					string_type_declaration_init) // may be == NULL!
 */
     void *visit(string_type_declaration_c *symbol)	{return symbol;}
+
+
 };
 
 
--- a/stage4/generate_c/search_constant_type.cc	Tue May 19 16:31:35 2009 +0200
+++ b/stage4/generate_c/search_constant_type.cc	Mon May 25 12:17:10 2009 +0200
@@ -68,6 +68,16 @@
   static constant_int_type_name_c      constant_int_type_name;
   static direct_variable_type_name_c   direct_variable_type_name;
 
+  /******************************************************/
+  /* Extensions to the base standard as defined in      */
+  /* "Safety Software Technical Specification,          */
+  /*  Part 1: Concepts and Function Blocks,             */
+  /*  Version 1.0 – Official Release"                   */
+  /* by PLCopen - Technical Committee 5 - 2006-01-31    */
+  /******************************************************/
+  static safebool_type_name_c safebool_type_name;
+
+
   public:
     symbol_c *get_type(symbol_c *constant) {
       return (symbol_c *)constant->accept(*this);
--- a/stage4/generate_iec/generate_iec.cc	Tue May 19 16:31:35 2009 +0200
+++ b/stage4/generate_iec/generate_iec.cc	Mon May 25 12:17:10 2009 +0200
@@ -287,6 +287,14 @@
 /***********************************/
 void *visit(time_type_name_c *symbol) {s4o.print("TIME"); return NULL;}
 void *visit(bool_type_name_c *symbol) {s4o.print("BOOL"); return NULL;}
+/******************************************************/
+/* whether we are suporting safe extensions           */
+/* as defined in PLCopen - Technical Committee 5      */
+/* Safety Software Technical Specification,           */
+/* Part 1: Concepts and Function Blocks,              */
+/* Version 1.0 – Official Release                     */
+/******************************************************/
+void *visit(safebool_type_name_c *symbol) {s4o.print("SAFEBOOL"); return NULL;}
 void *visit(sint_type_name_c *symbol) {s4o.print("SINT"); return NULL;}
 void *visit(int_type_name_c *symbol) {s4o.print("INT"); return NULL;}
 void *visit(dint_type_name_c *symbol) {s4o.print("DINT"); return NULL;}