Skip to content

Commit

Permalink
Add container type for memory objects
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Buesch <[email protected]>
  • Loading branch information
mbuesch committed Sep 3, 2018
1 parent ac73741 commit ed481e3
Show file tree
Hide file tree
Showing 55 changed files with 1,059 additions and 543 deletions.
26 changes: 21 additions & 5 deletions awlsim/awlcompiler/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ def __rawFieldToDataType(self, rawField):
# dataType: The AwlDataType for this field.
# initBytes: The initialization bytes for this field, or None.
def rawFieldTranslate(self, rawField):
#@cy cdef AwlMemoryObject memObj

# Get the field name
name = rawField.getIdentString()
# Get the field data type
Expand Down Expand Up @@ -230,7 +232,6 @@ def rawFieldTranslate(self, rawField):
# Translate the initialization values and
# put them into a bytearray.
initMem = AwlMemory(intDivRoundUp(dataType.width, 8))
initBytes = initMem.dataBytes
AwlDataType = _getAwlDataTypeClass()
if dataType.type == AwlDataType.TYPE_ARRAY:
for rawDataInit in rawField.defaultInits:
Expand All @@ -240,29 +241,44 @@ def rawFieldTranslate(self, rawField):
offset = make_AwlOffset_fromLongBitOffset(
linArrayIndex *
dataType.arrayElementType.width)
memObj = make_AwlMemoryObject_fromGeneric(
value,
dataType.arrayElementType.width)
try:
initMem.store(offset, dataType.arrayElementType.width,
value)
initMem.store(offset, memObj)
except AwlSimError as e:
raise AwlSimError("Data field '%s' initialization "
"is out of range." % str(rawField))
else:
assert(len(rawField.defaultInits) == 1)
value = dataType.parseMatchingImmediate(rawField.defaultInits[0].valueTokens)
memObj = make_AwlMemoryObject_fromGeneric(
value, dataType.width)
try:
initMem.store(make_AwlOffset(0, 0), dataType.width, value)
initMem.store(make_AwlOffset(0, 0), memObj)
except AwlSimError as e:
raise AwlSimError("Data field '%s' initialization "
"is out of range." % str(rawField))
initBytes = initMem.getDataBytes()
else:
initBytes = None
return name, dataType, initBytes

# Initialize a DB (global or instance) data field from a raw data-init.
def __initDBField(self, db, dataType, rawDataInit):
#@cy cdef AwlStructInstance structInstance
#@cy cdef AwlMemoryObject memObj

fieldName = rawDataInit.getIdentString()
value = dataType.parseMatchingImmediate(rawDataInit.valueTokens)
db.structInstance.setFieldDataByName(fieldName, value)
if dataType.type == dataType.TYPE_ARRAY:
# This is a single-array-element initializer.
width = dataType.arrayElementType.width
else:
width = dataType.width
memObj = make_AwlMemoryObject_fromGeneric(value, width)
structInstance = db.structInstance
structInstance.setFieldDataByName(fieldName, memObj)

# Create a DB data field.
def __createDBField(self, db, rawDataField):
Expand Down
18 changes: 11 additions & 7 deletions awlsim/core/callstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def _FC_trans_dbpointerInVL(self, param, rvalueOp): #@nocy
else:
dbNumber = rvalueOp.offset.dbNumber
cpu.store(storeOper,
max(0, dbNumber),
make_AwlMemoryObject_fromScalar(max(0, dbNumber), 16),
widthMaskAll)
storeOper.offset = loffset.addInt(2, 0)
storeOper.width = 32
Expand All @@ -189,7 +189,7 @@ def _FC_trans_dbpointerInVL(self, param, rvalueOp): #@nocy
raise AwlSimBug("FC_trans_dbpointerInVL: Invalid rValueOp area. "
"(area=%d, operType=%d)" % (area, rvalueOp.operType))
cpu.store(storeOper,
area | rvalueOp.offset.toPointerValue(),
make_AwlMemoryObject_fromScalar(area | rvalueOp.offset.toPointerValue(), 32),
widthMaskAll)
# Return the operator for the DB pointer.
return make_AwlOperator(AwlOperatorTypes.MEM_VL,
Expand Down Expand Up @@ -485,6 +485,7 @@ def make_CallStackElem(cpu, #@nocy
#@cy cdef AwlStructField structField
#@cy cdef AwlStructInstance structInstance
#@cy cdef uint32_t widthMaskAll
#@cy cdef AwlMemoryObject memObj

cse = CallStackElem()

Expand Down Expand Up @@ -528,8 +529,9 @@ def make_CallStackElem(cpu, #@nocy
structField.compound:
# Compound data type with IN_OUT decl.
# Make a DB-ptr to the actual data.
data = cse._FB_trans_dbpointer(
param, param.rvalueOp)
memObj = make_AwlMemoryObject_fromBytes(
cse._FB_trans_dbpointer(param, param.rvalueOp),
48)
# Get the DB-ptr struct field.
structField = structField.finalOverride
else:
Expand All @@ -539,12 +541,14 @@ def make_CallStackElem(cpu, #@nocy
if structField.callByRef:
# Do not fetch. Type is passed 'by reference'.
# This is for TIMER, COUNTER, etc...
data = param.rvalueOp.resolve(True).offset.byteOffset
memObj = make_AwlMemoryObject_fromScalar(
param.rvalueOp.resolve(True).offset.byteOffset,
16)
else:
data = cpu.fetch(param.rvalueOp, widthMaskAll)
memObj = cpu.fetch(param.rvalueOp, widthMaskAll)
# Transfer data into DBI.
structInstance.setFieldData(structField,
data,
memObj,
instanceBaseOffset)
cse._interfRefs = None
else:
Expand Down
102 changes: 51 additions & 51 deletions awlsim/core/cpu.pxd.in
Original file line number Diff line number Diff line change
Expand Up @@ -101,56 +101,56 @@ cdef class S7CPU(object):
cdef mcrStackAppend(self, S7StatusWord statusWord)
cdef mcrStackPop(self)

cpdef object fetch(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchIMM(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchIMM_DT(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchIMM_PTR(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchIMM_STR(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchDBLG(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchDBNO(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchDILG(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchDINO(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchAR2(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchSTW(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchSTW_Z(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchSTW_NZ(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchSTW_POS(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchSTW_NEG(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchSTW_POSZ(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchSTW_NEGZ(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchSTW_UO(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchE(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchA(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchM(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchL(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchVL(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchDB(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchDI(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchPE(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchT(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchZ(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchNAMED_LOCAL(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchNAMED_LOCAL_PTR(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchNAMED_DBVAR(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchINDIRECT(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchVirtACCU(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchVirtAR(self, AwlOperator operator, uint32_t allowedWidths)
cdef object __fetchVirtDBR(self, AwlOperator operator, uint32_t allowedWidths)

cpdef store(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeE(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeA(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeM(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeL(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeVL(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeDB(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeDI(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storePA(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeAR2(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeSTW(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeNAMED_LOCAL(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeNAMED_DBVAR(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef __storeINDIRECT(self, AwlOperator operator, object value, uint32_t allowedWidths)
cdef AwlMemoryObject fetch(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchIMM(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchIMM_DT(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchIMM_PTR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchIMM_STR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchDBLG(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchDBNO(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchDILG(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchDINO(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchAR2(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchSTW(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchSTW_Z(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchSTW_NZ(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchSTW_POS(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchSTW_NEG(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchSTW_POSZ(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchSTW_NEGZ(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchSTW_UO(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchE(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchA(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchM(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchL(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchVL(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchDB(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchDI(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchPE(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchT(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchZ(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchNAMED_LOCAL(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchNAMED_LOCAL_PTR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchNAMED_DBVAR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchINDIRECT(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchVirtACCU(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchVirtAR(self, AwlOperator operator, uint32_t allowedWidths) except NULL
cdef AwlMemoryObject __fetchVirtDBR(self, AwlOperator operator, uint32_t allowedWidths) except NULL

cdef store(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeE(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeA(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeM(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeL(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeVL(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeDB(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeDI(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storePA(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeAR2(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeSTW(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeNAMED_LOCAL(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeNAMED_DBVAR(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)
cdef __storeINDIRECT(self, AwlOperator operator, AwlMemoryObject memObj, uint32_t allowedWidths)

cpdef bytearray fetchOutputRange(self, uint32_t byteOffset, uint32_t byteCount)
cpdef bytearray fetchInputRange(self, uint32_t byteOffset, uint32_t byteCount)
Expand Down Expand Up @@ -191,6 +191,6 @@ cdef class S7CPU(object):

cdef AwlOperator __translateFCNamedLocalOper(self, AwlOperator operator, _Bool store)

cdef makeCurrentDateAndTime(self, bytearray byteArray, uint32_t offset)
cdef makeCurrentDateAndTime(self, uint8_t *byteArray, uint32_t offset)

cdef __dumpLStackFrame(self, prefix, LStackFrame *frame)
Loading

0 comments on commit ed481e3

Please sign in to comment.