diff --git a/compiler/2014/C_CodeGen.jrag b/compiler/2014/C_CodeGen.jrag index a68fa0ed46e50f9932a7ca3ab1176644919e2762..19dd58ac842f271229ea6e1d17d6160832105d77 100644 --- a/compiler/2014/C_CodeGen.jrag +++ b/compiler/2014/C_CodeGen.jrag @@ -221,6 +221,7 @@ aspect C_CodeGen { getDecl(i).C_emitDecoderDeclaration(env); getDecl(i).C_emitEncoderDeclaration(env); getDecl(i).C_emitSizeofDeclaration(env); + getDecl(i).C_emitCopyStaticDeclaration(env); getDecl(i).C_emitCopyDeclaration(env); getDecl(i).C_emitCopyDeallocationDeclaration(env); env.println(""); @@ -239,6 +240,7 @@ aspect C_CodeGen { getDecl(i).C_emitEncoderRegisterHandler(env); getDecl(i).C_emitEncoderIoctl(env); getDecl(i).C_emitSizeof(env); + getDecl(i).C_emitCopyStatic(env); getDecl(i).C_emitCopy(env); getDecl(i).C_emitCopyDeallocation(env); } @@ -957,6 +959,139 @@ aspect C_copy { } } +aspect C_copy_static { + + private void SampleDecl.C_emitCopyStaticFunctionParam(C_env env, String src, + String dst) + { + env.println("void labcomm2014_copy_static_" + + env.prefix + getName() + "("); + env.indent(); + env.println(env.prefix + getName() + " *" + dst + ","); + env.println(env.prefix + getName() + " *" + src); + env.unindent(); + env.print(")"); + } + + public void Decl.C_emitCopyStaticDeclaration(C_env env) { + } + + public void SampleDecl.C_emitCopyStaticDeclaration(C_env env) { + C_emitCopyStaticFunctionParam(env, "src", "dst"); + env.println(";"); + } + + public void Decl.C_emitCopyStatic(C_env env) { + throw new Error(this.getClass().getName() + + ".C_emitCopyStatic(C_env env)" + + " not declared"); + } + + public void TypeDecl.C_emitCopyStatic(C_env env) { + } + + public void SampleDecl.C_emitCopyStatic(C_env env) { + final String dst = "dst"; + final String src = "src"; + C_env env_src = env.nestStruct(src).setPointer(); + C_env env_dst = env.nestStruct(dst).setPointer(); + + C_emitCopyStaticFunctionParam(env_src, src, dst); + env_src.println(""); + env_src.println("{"); + env_src.indent(); + getDataType().C_emitCopyStatic(env_src, env_dst); + env_src.unindent(); + env_src.println("}"); + } + + public void DataType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + throw new Error(this.getClass().getName() + + ".C_emitCopyStatic(C_env env)" + + " not declared"); + } + + public void VoidType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + } + + public void PrimType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + if (C_isDynamic()) { + env_src.println(String.format( + "memcpy(%s%s, %s%s, strlen(%s%s)+1);", + env_dst.accessor(), env_dst.qualid, + env_src.accessor(), env_src.qualid, + env_src.accessor(), env_src.qualid)); + } else { + env_src.println(env_dst.accessor() + env_dst.qualid + " = " + + env_src.accessor() + env_src.qualid + ";"); + } + } + + public void UserType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + lookupType(getName()).getDataType().C_emitCopyStatic(env_src, env_dst); + } + + public void StructType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + for (int i = 0 ; i < getNumField() ; i++) { + getField(i).C_emitCopyStatic(env_src, env_dst); + } + } + + public void ArrayType.C_emitCopyStatic(C_env env_src, C_env env_dst) { + C_emitCopyStaticDecodeLimit(env_src, env_dst); + C_emitCopyStaticArrayAllocate(env_src, env_dst); + env_src.println("{"); + env_src.indent(); + C_emitLoopVariables(env_src); + for (int i = 0 ; i < getNumExp() ; i++) { + String iterator = "i_" + env_src.depth + "_" + i; + env_src.println("for (" + iterator + " = 0" + + " ; " + + iterator + " < " + getExp(i).C_getLimit(env_src, i) + + " ; " + + iterator + "++) {"); + env_src.indent(); + } + C_emitCalcIndex(env_src); + getDataType().C_emitCopyStatic(C_Nest(env_src), C_Nest(env_dst)); + for (int i = getNumExp() - 1 ; i >= 0 ; i--) { + env_src.unindent(); + env_src.println("}"); + } + env_src.unindent(); + env_src.println("}"); + } + + public void Field.C_emitCopyStatic(C_env env_src, C_env env_dst) { + String fnam = env_src.memberAccessor() + getName(); + getDataType().C_emitCopyStatic(env_src.nestStruct(fnam), env_dst.nestStruct(fnam)); + } + + public void Exp.C_emitCopyStaticDecodeLimit(C_env env_src, C_env env_dst, int i) { + // Ordinary array has no length-member. + } + + public void VariableSize.C_emitCopyStaticDecodeLimit(C_env env_src, C_env env_dst, int i) { + String src = env_src.qualid + env_src.memberAccessor() + "n_" + i; + String dst = env_dst.qualid + env_dst.memberAccessor() + "n_" + i; + env_src.println(dst + " = " + src + ";"); + } + + public void ArrayType.C_emitCopyStaticDecodeLimit(C_env env_src, C_env env_dst) { + for (int i = 0 ; i < getNumExp() ; i++) { + getExp(i).C_emitCopyStaticDecodeLimit(env_src, env_dst, i); + } + } + + public void ArrayType.C_emitCopyStaticArrayAllocate(C_env env_src, C_env env_dst) { + } + + public void VariableArrayType.C_emitCopyStaticArrayAllocate(C_env env_src, + C_env env_dst) + { + } +} + aspect C_DecoderIoctl { public void Decl.C_emitDecoderIoctl(C_env env) {