Skip to content

Commit

Permalink
Implemented convert_lines.pl inside Print.mo
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@17574 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Oct 7, 2013
1 parent 0b22bf8 commit f868564
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 84 deletions.
20 changes: 6 additions & 14 deletions Compiler/Template/CodegenC.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ template translateModel(SimCode simCode)

let()= textFile(simulationFunctionsHeaderFile(fileNamePrefix, modelInfo.functions, recordDecls), '<%fileNamePrefix%>_functions.h')

let()= textFile(simulationFunctionsFile(fileNamePrefix, modelInfo.functions, literals), '<%fileNamePrefix%>_functions.c')
let()= textFileConvertLines(simulationFunctionsFile(fileNamePrefix, modelInfo.functions, literals), '<%fileNamePrefix%>_functions.c')

let()= textFile(recordsFile(fileNamePrefix, recordDecls), '<%fileNamePrefix%>_records.c')

Expand All @@ -74,7 +74,7 @@ template translateModel(SimCode simCode)
// adpro: write the main .c file last! Make on windows doesn't seem to realize that
// the .c file is newer than the .o file if we have succesive simulate commands
// for the same model (i.e. see testsuite/linearize/simextfunction.mos).
let()= textFile(simulationFile(simCode,guid), '<%fileNamePrefix%>.c')
let()= textFileConvertLines(simulationFile(simCode,guid), '<%fileNamePrefix%>.c')

// If ParModelica generate the kernels file too.
if acceptParModelicaGrammar() then
Expand All @@ -95,7 +95,7 @@ template translateFunctions(FunctionCode functionCode)
let filePrefix = name
let _= (if mainFunction then textFile(functionsMakefile(functionCode), '<%filePrefix%>.makefile'))
let()= textFile(functionsHeaderFile(filePrefix, mainFunction, functions, extraRecordDecls, externalFunctionIncludes), '<%filePrefix%>.h')
let()= textFile(functionsFile(filePrefix, mainFunction, functions, literals), '<%filePrefix%>.c')
let()= textFileConvertLines(functionsFile(filePrefix, mainFunction, functions, literals), '<%filePrefix%>.c')
let()= textFile(recordsFile(filePrefix, extraRecordDecls), '<%filePrefix%>_records.c')
// If ParModelica generate the kernels file too.
if acceptParModelicaGrammar() then
Expand Down Expand Up @@ -3426,8 +3426,8 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula
%>
PERL=perl
FILEPREFIX=<%fileNamePrefix%>
MAINFILE=$(FILEPREFIX)<% if boolOr(acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS)) then ".conv"%>.c
MAINOBJ=$(FILEPREFIX)<% if boolOr(acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS)) then ".conv"%>.o
MAINFILE=$(FILEPREFIX).c
MAINOBJ=$(FILEPREFIX).o
GENERATEDFILES=$(MAINFILE) $(FILEPREFIX)_functions.c $(FILEPREFIX)_functions.h $(FILEPREFIX)_records.c $(FILEPREFIX).makefile

.PHONY: omc_main_target clean bundle
Expand All @@ -3437,11 +3437,6 @@ case SIMCODE(modelInfo=MODELINFO(__), makefileParams=MAKEFILE_PARAMS(__), simula

omc_main_target: $(MAINOBJ) $(FILEPREFIX)_records.o $(FILEPREFIX)_functions.c $(FILEPREFIX)_functions.h
<%\t%>$(CXX) -I. -o $(FILEPREFIX)$(EXEEXT) $(MAINOBJ) $(FILEPREFIX)_records.o $(CPPFLAGS) <%dirExtra%> <%libsPos1%> <%libsPos2%> $(CFLAGS) $(LDFLAGS)
<%fileNamePrefix%>.conv.c: $(FILEPREFIX).c
<%\t%>$(PERL) <%makefileParams.omhome%>/share/omc/scripts/convert_lines.pl $< $@.tmp
<%\t%>@mv $@.tmp $@
<%\t%>$(PERL) <%makefileParams.omhome%>/share/omc/scripts/convert_lines.pl $(FILEPREFIX)_functions.c $@.tmp
<%\t%>@mv $@.tmp $(FILEPREFIX)_functions.c

clean:
<%\t%>@rm -f $(FILEPREFIX)_records.o $(MAINOBJ)
Expand Down Expand Up @@ -3658,16 +3653,13 @@ case FUNCTIONCODE(makefileParams=MAKEFILE_PARAMS(__)) then
CFLAGS= -I"<%makefileParams.omhome%>/include/omc" <%makefileParams.includes ; separator=" "%> <%makefileParams.cflags%>
LDFLAGS= -L"<%makefileParams.omhome%>/lib/omc" -Wl,-rpath,'<%makefileParams.omhome%>/lib/omc' -lOpenModelicaRuntimeC <%ParModelicaLibs%> <%makefileParams.ldflags%> <%makefileParams.runtimelibs%>
PERL=perl
MAINFILE=<%name%><% if boolOr(acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS)) then ".conv"%>.c
MAINFILE=<%name%>.c

.PHONY: <%name%>
<%name%>: $(MAINFILE) <%name%>.h <%name%>_records.c
<%\t%> $(CC) $(CFLAGS) -c -o <%name%>.o $(MAINFILE)
<%\t%> $(CC) $(CFLAGS) -c -o <%name%>_records.o <%name%>_records.c
<%\t%> $(LINK) -o <%name%>$(DLLEXT) <%name%>.o <%name%>_records.o <%libsStr%> $(CFLAGS) $(LDFLAGS) -lm
<%name%>.conv.c: <%name%>.c
<%\t%> $(PERL) <%makefileParams.omhome%>/share/omc/scripts/convert_lines.pl $< $@.tmp
<%\t%> @mv $@.tmp $@
>>
end functionsMakefile;

Expand Down
5 changes: 5 additions & 0 deletions Compiler/Template/SimCodeTV.mo
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,11 @@ package Tpl
input String inFileName;
end textFile;

function textFileConvertLines
input Text inText;
input String inFileName;
end textFileConvertLines;

//we do not import Error.addSourceMessage() directly
//because of list creation in Susan is not possible (yet by design)
function addSourceTemplateError
Expand Down
38 changes: 38 additions & 0 deletions Compiler/Template/Tpl.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1814,6 +1814,44 @@ algorithm
end matchcontinue;
end textFile;

public function textFileConvertLines "This function renders a (memory-)text to a file. If we generate modelicaLine directives, translate them to C preprocessor."
input Text inText;
input String inFileName;

algorithm
_ := matchcontinue (inText, inFileName)
local
Text txt;
String file;
Real rtTickTxt, rtTickW;
case (txt, file)
equation
rtTickTxt = System.realtimeTock(GlobalScript.RT_CLOCK_BUILD_MODEL);
Print.clearBuf();
textStringBuf(txt);
rtTickW = System.realtimeTock(GlobalScript.RT_CLOCK_BUILD_MODEL);
System.writeFile(file, "") /* To make realpath work */;
Debug.bcall1(boolOr(Config.acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS)), Print.writeBufConvertLines, System.realpath(file));
Debug.bcall1(not boolOr(Config.acceptMetaModelicaGrammar(), Flags.isSet(Flags.GEN_DEBUG_SYMBOLS)), Print.writeBuf, file);
Debug.bcall2(Config.getRunningTestsuite(), System.appendFile, Config.getRunningTestsuiteFile(), file +& "\n");
Print.clearBuf();
Debug.fprintln(Flags.TPL_PERF_TIMES,
"textFile " +& file
+& "\n text:" +& realString(realSub(rtTickW,rtTickTxt))
+& "\n write:" +& realString(realSub(System.realtimeTock(GlobalScript.RT_CLOCK_BUILD_MODEL), rtTickW))
);
then
();

//TODO: let this function fail and the error message can be reported via # ( textFile(txt,"file.cpp") ; failMsg="error" )
case (_,_)
equation
Debug.fprint(Flags.FAILTRACE, "-!!!Tpl.textFile failed - a system error ?\n");
then
();

end matchcontinue;
end textFileConvertLines;

public function sourceInfo
"Magic sourceInfo() function implementation"
Expand Down
6 changes: 6 additions & 0 deletions Compiler/Util/Print.mo
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public function writeBuf
external "C" Print_writeBuf(inString) annotation(Library = "omcruntime");
end writeBuf;

public function writeBufConvertLines
"Writes the print buffer to the filename, with /*#modelicaLine...*/ directives converted to #line C preprocessor macros"
input String filename;
external "C" Print_writeBufConvertLines(filename) annotation(Library = "omcruntime");
end writeBufConvertLines;

public function getBufLength
"Gets the actual length of the filled space in the print buffer."
output Integer outBufFilledLength;
Expand Down
7 changes: 6 additions & 1 deletion Compiler/runtime/Print_omc.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,12 @@ extern void Print_printBufNewLine(void)

extern void Print_writeBuf(const char* filename)
{
//fprintf(stderr, "Print_writeBuf %s: %s\n", filename, buf);
if (PrintImpl__writeBuf(filename))
MMC_THROW();
}

extern void Print_writeBufConvertLines(const char* filename)
{
if (PrintImpl__writeBufConvertLines(filename))
MMC_THROW();
}
8 changes: 8 additions & 0 deletions Compiler/runtime/Print_rml.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ RML_BEGIN_LABEL(Print__writeBuf)
}
RML_END_LABEL

RML_BEGIN_LABEL(Print__writeBufConvertLines)
{
if (PrintImpl__writeBufConvertLines(RML_STRINGDATA(rmlA0)))
RML_TAILCALLK(rmlFC);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(Print__getBufLength)
{
rmlA0 = mk_icon(PrintImpl__getBufLength());
Expand Down
97 changes: 97 additions & 0 deletions Compiler/runtime/printimpl.c
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,103 @@ static int PrintImpl__writeBuf(const char* filename)
return 0;
}

#include <regex.h>

/* returns 0 on success */
static int PrintImpl__writeBufConvertLines(const char *filename)
{
print_members* members = getMembers();
#if defined(__MINGW32__) || defined(_MSC_VER)
const char *fileOpenMode = "wt"; /* on Windows do translation so that \n becomes \r\n */
#else
const char *fileOpenMode = "wb"; /* on Unixes don't bother, do it binary mode */
#endif
char *str = buf, *next;
FILE * file = NULL;
regex_t re_begin,re_end;
regmatch_t matches[3];
int i;
long nlines=3 /* We start at 3 because we write 2 lines before the first line */, modelicaLine;
/* What we try to match: */
/*#modelicaLine [/path/to/a.mo:4:3-4:12]*/
/*#endModelicaLine*/
const char *re_str[2] = {"^ */[*]#modelicaLine .([^:]*):([0-9]*):[0-9]*-[0-9]*:[0-9]*.[*]/$", "^ */[*]#endModelicaLine[*]/$"};
const char *modelicaFileName = NULL;
str[nfilled] = '\0';

/* First, compile the regular expressions */
if (regcomp(&re_begin, re_str[0], REG_EXTENDED) || regcomp(&re_end, re_str[1], 0)) {
const char *c_tokens[1]={filename};
c_add_message(21, /* WRITING_FILE_ERROR */
ErrorType_scripting,
ErrorLevel_error,
gettext("Error compiling regular expression: %s or %s."),
re_str,
2);
return 1;
}

/* check if we have something to write */
/* open the file */
/* adrpo: 2010-09-22 open the file in BINARY mode as otherwise \r\n becomes \r\r\n! */
file = fopen(filename,fileOpenMode);
if (file == NULL) {
const char *c_tokens[1]={filename};
c_add_message(21, /* WRITING_FILE_ERROR */
ErrorType_scripting,
ErrorLevel_error,
gettext("Error writing to file %s."),
c_tokens,
1);
regfree(&re_begin);
regfree(&re_end);
return 1;
}
if (str == NULL || str[0]=='\0') {
/* nothing to write to file, just close it and return ! */
regfree(&re_begin);
regfree(&re_end);
fclose(file);
return 1;
}
#define ABC __FILE__
fprintf(file,"#define OMC_FILE __FILE__\n#line %ld OMC_FILE\n", nlines++);
do {
next = strchr(str,'\n');
if (!next) {
fprintf(file,"%s",str);
break;
}
*next++ = '\0';
if (0==regexec(&re_begin, str, 3, matches, 0)) {
str[matches[1].rm_eo] = '\0';
str[matches[2].rm_eo] = '\0';
modelicaFileName = str + matches[1].rm_so;
modelicaLine = strtol(str + matches[2].rm_so, NULL, 10);
} else if (0==regexec(&re_end, str, 3, matches, 0)) {
if (modelicaFileName) { /* There is sometimes #endModlicaLine without a matching #modelicaLine */
modelicaFileName = NULL;
fprintf(file,"#line %ld OMC_FILE\n", nlines++);
}
} else if (modelicaFileName) {
fprintf(file,"#line %ld \"%s\"\n", modelicaLine, modelicaFileName);
fprintf(file,"%s\n", str);
nlines+=2;
} else {
fprintf(file,"%s\n", str);
nlines++;
}
str = next;
} while (1);
/* We do destructive updates on the print buffer; hide our tracks */
*buf = 0;
nfilled = 0;
regfree(&re_begin);
regfree(&re_end);
fclose(file);
return 0;
}

static long PrintImpl__getBufLength(void)
{
print_members* members = getMembers();
Expand Down
69 changes: 0 additions & 69 deletions Compiler/scripts/convert_lines.pl

This file was deleted.

0 comments on commit f868564

Please sign in to comment.