| 1 |
# Copyright 2018 Google LLC |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
from copy import copy |
| 5 |
|
| 6 |
from .common.codegen import CodeGen |
| 7 |
from .common.vulkantypes import \ |
| 8 |
VulkanAPI, makeVulkanTypeSimple, iterateVulkanType, VulkanTypeIterator |
| 9 |
|
| 10 |
from .wrapperdefs import VulkanWrapperGenerator |
| 11 |
from .wrapperdefs import EQUALITY_VAR_NAMES |
| 12 |
from .wrapperdefs import EQUALITY_ON_FAIL_VAR |
| 13 |
from .wrapperdefs import EQUALITY_ON_FAIL_VAR_TYPE |
| 14 |
from .wrapperdefs import EQUALITY_RET_TYPE |
| 15 |
from .wrapperdefs import API_PREFIX_EQUALITY |
| 16 |
from .wrapperdefs import STRUCT_EXTENSION_PARAM, STRUCT_EXTENSION_PARAM2 |
| 17 |
|
| 18 |
class VulkanEqualityCodegen(VulkanTypeIterator): |
| 19 |
|
| 20 |
def __init__(self, cgen, inputVars, onFailCompareVar, prefix): |
| 21 |
self.cgen = cgen |
| 22 |
self.inputVars = inputVars |
| 23 |
self.onFailCompareVar = onFailCompareVar |
| 24 |
self.prefix = prefix |
| 25 |
|
| 26 |
def makeAccess(varName, asPtr = True): |
| 27 |
return lambda t: self.cgen.generalAccess(t, parentVarName = varName, asPtr = asPtr) |
| 28 |
|
| 29 |
def makeLengthAccess(varName): |
| 30 |
return lambda t: self.cgen.generalLengthAccess(t, parentVarName = varName) |
| 31 |
|
| 32 |
def makeLengthAccessGuard(varName): |
| 33 |
return lambda t: self.cgen.generalLengthAccessGuard(t, parentVarName=varName) |
| 34 |
|
| 35 |
self.exprAccessorLhs = makeAccess(self.inputVars[0]) |
| 36 |
self.exprAccessorRhs = makeAccess(self.inputVars[1]) |
| 37 |
|
| 38 |
self.exprAccessorValueLhs = makeAccess(self.inputVars[0], asPtr = False) |
| 39 |
self.exprAccessorValueRhs = makeAccess(self.inputVars[1], asPtr = False) |
| 40 |
|
| 41 |
self.lenAccessorLhs = makeLengthAccess(self.inputVars[0]) |
| 42 |
self.lenAccessorRhs = makeLengthAccess(self.inputVars[1]) |
| 43 |
|
| 44 |
self.lenAccessGuardLhs = makeLengthAccessGuard(self.inputVars[0]) |
| 45 |
self.lenAccessGuardRhs = makeLengthAccessGuard(self.inputVars[1]) |
| 46 |
|
| 47 |
self.checked = False |
| 48 |
|
| 49 |
def getTypeForCompare(self, vulkanType): |
| 50 |
res = copy(vulkanType) |
| 51 |
|
| 52 |
if not vulkanType.accessibleAsPointer(): |
| 53 |
res = res.getForAddressAccess() |
| 54 |
|
| 55 |
if vulkanType.staticArrExpr: |
| 56 |
res = res.getForAddressAccess() |
| 57 |
|
| 58 |
return res |
| 59 |
|
| 60 |
def makeCastExpr(self, vulkanType): |
| 61 |
return "(%s)" % ( |
| 62 |
self.cgen.makeCTypeDecl(vulkanType, useParamName=False)) |
| 63 |
|
| 64 |
def makeEqualExpr(self, lhs, rhs): |
| 65 |
return "(%s) == (%s)" % (lhs, rhs) |
| 66 |
|
| 67 |
def makeEqualBufExpr(self, lhs, rhs, size): |
| 68 |
return "(memcmp(%s, %s, %s) == 0)" % (lhs, rhs, size) |
| 69 |
|
| 70 |
def makeEqualStringExpr(self, lhs, rhs): |
| 71 |
return "(strcmp(%s, %s) == 0)" % (lhs, rhs) |
| 72 |
|
| 73 |
def makeBothNotNullExpr(self, lhs, rhs): |
| 74 |
return "(%s) && (%s)" % (lhs, rhs) |
| 75 |
|
| 76 |
def makeBothNullExpr(self, lhs, rhs): |
| 77 |
return "!(%s) && !(%s)" % (lhs, rhs) |
| 78 |
|
| 79 |
def compareWithConsequence(self, compareExpr, vulkanType, errMsg=""): |
| 80 |
self.cgen.stmt("if (!(%s)) { %s(\"%s (Error: %s)\"); }" % |
| 81 |
(compareExpr, self.onFailCompareVar, |
| 82 |
self.exprAccessorValueLhs(vulkanType), errMsg)) |
| 83 |
|
| 84 |
def onCheck(self, vulkanType): |
| 85 |
|
| 86 |
self.checked = True |
| 87 |
|
| 88 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 89 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 90 |
|
| 91 |
bothNull = self.makeBothNullExpr(accessLhs, accessRhs) |
| 92 |
bothNotNull = self.makeBothNotNullExpr(accessLhs, accessRhs) |
| 93 |
nullMatchExpr = "(%s) || (%s)" % (bothNull, bothNotNull) |
| 94 |
|
| 95 |
self.compareWithConsequence( \ |
| 96 |
nullMatchExpr, |
| 97 |
vulkanType, |
| 98 |
"Mismatch in optional field") |
| 99 |
|
| 100 |
skipStreamInternal = vulkanType.typeName == "void" |
| 101 |
|
| 102 |
if skipStreamInternal: |
| 103 |
return |
| 104 |
|
| 105 |
self.cgen.beginIf("%s && %s" % (accessLhs, accessRhs)) |
| 106 |
|
| 107 |
def endCheck(self, vulkanType): |
| 108 |
|
| 109 |
skipStreamInternal = vulkanType.typeName == "void" |
| 110 |
if skipStreamInternal: |
| 111 |
return |
| 112 |
|
| 113 |
if self.checked: |
| 114 |
self.cgen.endIf() |
| 115 |
self.checked = False |
| 116 |
|
| 117 |
def onCompoundType(self, vulkanType): |
| 118 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 119 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 120 |
|
| 121 |
lenAccessLhs = self.lenAccessorLhs(vulkanType) |
| 122 |
lenAccessRhs = self.lenAccessorRhs(vulkanType) |
| 123 |
|
| 124 |
lenAccessGuardLhs = self.lenAccessGuardLhs(vulkanType) |
| 125 |
lenAccessGuardRhs = self.lenAccessGuardRhs(vulkanType) |
| 126 |
|
| 127 |
needNullCheck = vulkanType.pointerIndirectionLevels > 0 |
| 128 |
|
| 129 |
if needNullCheck: |
| 130 |
bothNotNullExpr = self.makeBothNotNullExpr(accessLhs, accessRhs) |
| 131 |
self.cgen.beginIf(bothNotNullExpr) |
| 132 |
|
| 133 |
if lenAccessLhs is not None: |
| 134 |
equalLenExpr = self.makeEqualExpr(lenAccessLhs, lenAccessRhs) |
| 135 |
|
| 136 |
self.compareWithConsequence( \ |
| 137 |
equalLenExpr, |
| 138 |
vulkanType, "Lengths not equal") |
| 139 |
|
| 140 |
loopVar = "i" |
| 141 |
accessLhs = "%s + %s" % (accessLhs, loopVar) |
| 142 |
accessRhs = "%s + %s" % (accessRhs, loopVar) |
| 143 |
forInit = "uint32_t %s = 0" % loopVar |
| 144 |
forCond = "%s < (uint32_t)%s" % (loopVar, lenAccessLhs) |
| 145 |
forIncr = "++%s" % loopVar |
| 146 |
|
| 147 |
if needNullCheck: |
| 148 |
self.cgen.beginIf(equalLenExpr) |
| 149 |
|
| 150 |
if lenAccessGuardLhs is not None: |
| 151 |
self.cgen.beginIf(lenAccessGuardLhs) |
| 152 |
|
| 153 |
self.cgen.beginFor(forInit, forCond, forIncr) |
| 154 |
|
| 155 |
self.cgen.funcCall(None, self.prefix + vulkanType.typeName, |
| 156 |
[accessLhs, accessRhs, self.onFailCompareVar]) |
| 157 |
|
| 158 |
if lenAccessLhs is not None: |
| 159 |
self.cgen.endFor() |
| 160 |
if lenAccessGuardLhs is not None: |
| 161 |
self.cgen.endIf() |
| 162 |
if needNullCheck: |
| 163 |
self.cgen.endIf() |
| 164 |
|
| 165 |
if needNullCheck: |
| 166 |
self.cgen.endIf() |
| 167 |
|
| 168 |
def onString(self, vulkanType): |
| 169 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 170 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 171 |
|
| 172 |
bothNullExpr = self.makeBothNullExpr(accessLhs, accessRhs) |
| 173 |
bothNotNullExpr = self.makeBothNotNullExpr(accessLhs, accessRhs) |
| 174 |
nullMatchExpr = "(%s) || (%s)" % (bothNullExpr, bothNotNullExpr) |
| 175 |
|
| 176 |
self.compareWithConsequence( \ |
| 177 |
nullMatchExpr, |
| 178 |
vulkanType, |
| 179 |
"Mismatch in string pointer nullness") |
| 180 |
|
| 181 |
self.cgen.beginIf(bothNotNullExpr) |
| 182 |
|
| 183 |
self.compareWithConsequence( |
| 184 |
self.makeEqualStringExpr(accessLhs, accessRhs), |
| 185 |
vulkanType, "Unequal strings") |
| 186 |
|
| 187 |
self.cgen.endIf() |
| 188 |
|
| 189 |
def onStringArray(self, vulkanType): |
| 190 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 191 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 192 |
|
| 193 |
lenAccessLhs = self.lenAccessorLhs(vulkanType) |
| 194 |
lenAccessRhs = self.lenAccessorRhs(vulkanType) |
| 195 |
|
| 196 |
lenAccessGuardLhs = self.lenAccessGuardLhs(vulkanType) |
| 197 |
lenAccessGuardRhs = self.lenAccessGuardRhs(vulkanType) |
| 198 |
|
| 199 |
bothNullExpr = self.makeBothNullExpr(accessLhs, accessRhs) |
| 200 |
bothNotNullExpr = self.makeBothNotNullExpr(accessLhs, accessRhs) |
| 201 |
nullMatchExpr = "(%s) || (%s)" % (bothNullExpr, bothNotNullExpr) |
| 202 |
|
| 203 |
self.compareWithConsequence( \ |
| 204 |
nullMatchExpr, |
| 205 |
vulkanType, |
| 206 |
"Mismatch in string array pointer nullness") |
| 207 |
|
| 208 |
if lenAccessLhs is not None and lenAccessRhs is not None: |
| 209 |
equalLenExpr = self.makeEqualExpr(lenAccessLhs, lenAccessRhs) |
| 210 |
|
| 211 |
self.compareWithConsequence( \ |
| 212 |
equalLenExpr, |
| 213 |
vulkanType, "Lengths not equal in string array") |
| 214 |
|
| 215 |
self.compareWithConsequence( \ |
| 216 |
equalLenExpr, |
| 217 |
vulkanType, "Lengths not equal in string array") |
| 218 |
|
| 219 |
self.cgen.beginIf("%s && %s" % (equalLenExpr, bothNotNullExpr)) |
| 220 |
|
| 221 |
loopVar = "i" |
| 222 |
accessLhs = "*(%s + %s)" % (accessLhs, loopVar) |
| 223 |
accessRhs = "*(%s + %s)" % (accessRhs, loopVar) |
| 224 |
forInit = "uint32_t %s = 0" % loopVar |
| 225 |
forCond = "%s < (uint32_t)%s" % (loopVar, lenAccessLhs) |
| 226 |
forIncr = "++%s" % loopVar |
| 227 |
|
| 228 |
if lenAccessGuardLhs is not None: |
| 229 |
self.cgen.beginIf(lenAccessGuardLhs) |
| 230 |
|
| 231 |
self.cgen.beginFor(forInit, forCond, forIncr) |
| 232 |
|
| 233 |
self.compareWithConsequence( |
| 234 |
self.makeEqualStringExpr(accessLhs, accessRhs), |
| 235 |
vulkanType, "Unequal string in string array") |
| 236 |
|
| 237 |
self.cgen.endFor() |
| 238 |
|
| 239 |
if lenAccessGuardLhs is not None: |
| 240 |
self.cgen.endIf() |
| 241 |
|
| 242 |
self.cgen.endIf() |
| 243 |
|
| 244 |
def onStaticArr(self, vulkanType): |
| 245 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 246 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 247 |
|
| 248 |
lenAccessLhs = self.lenAccessorLhs(vulkanType) |
| 249 |
|
| 250 |
if lenAccessLhs is not None: |
| 251 |
finalLenExpr = "%s * %s" % (lenAccessLhs, |
| 252 |
self.cgen.sizeofExpr(vulkanType)) |
| 253 |
|
| 254 |
self.compareWithConsequence( |
| 255 |
self.makeEqualBufExpr(accessLhs, accessRhs, finalLenExpr), |
| 256 |
vulkanType, "Unequal static array") |
| 257 |
|
| 258 |
def onStructExtension(self, vulkanType): |
| 259 |
lhs = self.exprAccessorLhs(vulkanType) |
| 260 |
rhs = self.exprAccessorRhs(vulkanType) |
| 261 |
|
| 262 |
self.cgen.beginIf(lhs) |
| 263 |
self.cgen.funcCall(None, self.prefix + "extension_struct", |
| 264 |
[lhs, rhs, self.onFailCompareVar]) |
| 265 |
self.cgen.endIf() |
| 266 |
|
| 267 |
def onPointer(self, vulkanType): |
| 268 |
accessLhs = self.exprAccessorLhs(vulkanType) |
| 269 |
accessRhs = self.exprAccessorRhs(vulkanType) |
| 270 |
|
| 271 |
skipStreamInternal = vulkanType.typeName == "void" |
| 272 |
if skipStreamInternal: |
| 273 |
return |
| 274 |
|
| 275 |
lenAccessLhs = self.lenAccessorLhs(vulkanType) |
| 276 |
lenAccessRhs = self.lenAccessorRhs(vulkanType) |
| 277 |
|
| 278 |
if lenAccessLhs is not None: |
| 279 |
self.compareWithConsequence( \ |
| 280 |
self.makeEqualExpr(lenAccessLhs, lenAccessRhs), |
| 281 |
vulkanType, "Lengths not equal") |
| 282 |
|
| 283 |
finalLenExpr = "%s * %s" % (lenAccessLhs, |
| 284 |
self.cgen.sizeofExpr( |
| 285 |
vulkanType.getForValueAccess())) |
| 286 |
else: |
| 287 |
finalLenExpr = self.cgen.sizeofExpr(vulkanType.getForValueAccess()) |
| 288 |
|
| 289 |
self.compareWithConsequence( |
| 290 |
self.makeEqualBufExpr(accessLhs, accessRhs, finalLenExpr), |
| 291 |
vulkanType, "Unequal dyn array") |
| 292 |
|
| 293 |
def onValue(self, vulkanType): |
| 294 |
accessLhs = self.exprAccessorValueLhs(vulkanType) |
| 295 |
accessRhs = self.exprAccessorValueRhs(vulkanType) |
| 296 |
self.compareWithConsequence( |
| 297 |
self.makeEqualExpr(accessLhs, accessRhs), vulkanType, |
| 298 |
"Value not equal") |
| 299 |
|
| 300 |
|
| 301 |
class VulkanTesting(VulkanWrapperGenerator): |
| 302 |
|
| 303 |
def __init__(self, module, typeInfo): |
| 304 |
VulkanWrapperGenerator.__init__(self, module, typeInfo) |
| 305 |
|
| 306 |
self.codegen = CodeGen() |
| 307 |
|
| 308 |
self.equalityCodegen = \ |
| 309 |
VulkanEqualityCodegen( |
| 310 |
None, |
| 311 |
EQUALITY_VAR_NAMES, |
| 312 |
EQUALITY_ON_FAIL_VAR, |
| 313 |
API_PREFIX_EQUALITY) |
| 314 |
|
| 315 |
self.knownDefs = {} |
| 316 |
|
| 317 |
self.extensionTestingPrototype = \ |
| 318 |
VulkanAPI(API_PREFIX_EQUALITY + "extension_struct", |
| 319 |
EQUALITY_RET_TYPE, |
| 320 |
[STRUCT_EXTENSION_PARAM, |
| 321 |
STRUCT_EXTENSION_PARAM2, |
| 322 |
EQUALITY_ON_FAIL_VAR_TYPE]) |
| 323 |
|
| 324 |
def onBegin(self,): |
| 325 |
VulkanWrapperGenerator.onBegin(self) |
| 326 |
self.module.appendImpl(self.codegen.makeFuncDecl( |
| 327 |
self.extensionTestingPrototype)) |
| 328 |
|
| 329 |
def onGenType(self, typeXml, name, alias): |
| 330 |
VulkanWrapperGenerator.onGenType(self, typeXml, name, alias) |
| 331 |
|
| 332 |
if name in self.knownDefs: |
| 333 |
return |
| 334 |
|
| 335 |
category = self.typeInfo.categoryOf(name) |
| 336 |
|
| 337 |
if category in ["struct", "union"] and alias: |
| 338 |
self.module.appendHeader( |
| 339 |
self.codegen.makeFuncAlias(API_PREFIX_EQUALITY + name, |
| 340 |
API_PREFIX_EQUALITY + alias)) |
| 341 |
|
| 342 |
if category in ["struct", "union"] and not alias: |
| 343 |
|
| 344 |
structInfo = self.typeInfo.structs[name] |
| 345 |
|
| 346 |
typeFromName = \ |
| 347 |
lambda varname: makeVulkanTypeSimple(True, name, 1, varname) |
| 348 |
|
| 349 |
compareParams = \ |
| 350 |
list(map(typeFromName, EQUALITY_VAR_NAMES)) + \ |
| 351 |
[EQUALITY_ON_FAIL_VAR_TYPE] |
| 352 |
|
| 353 |
comparePrototype = \ |
| 354 |
VulkanAPI(API_PREFIX_EQUALITY + name, |
| 355 |
EQUALITY_RET_TYPE, |
| 356 |
compareParams) |
| 357 |
|
| 358 |
def structCompareDef(cgen): |
| 359 |
self.equalityCodegen.cgen = cgen |
| 360 |
for member in structInfo.members: |
| 361 |
iterateVulkanType(self.typeInfo, member, |
| 362 |
self.equalityCodegen) |
| 363 |
|
| 364 |
self.module.appendHeader( |
| 365 |
self.codegen.makeFuncDecl(comparePrototype)) |
| 366 |
self.module.appendImpl( |
| 367 |
self.codegen.makeFuncImpl(comparePrototype, structCompareDef)) |
| 368 |
|
| 369 |
def onGenCmd(self, cmdinfo, name, alias): |
| 370 |
VulkanWrapperGenerator.onGenCmd(self, cmdinfo, name, alias) |
| 371 |
|
| 372 |
def onEnd(self,): |
| 373 |
VulkanWrapperGenerator.onEnd(self) |
| 374 |
|
| 375 |
def forEachExtensionCompare(ext, castedAccess, cgen): |
| 376 |
cgen.funcCall(None, API_PREFIX_EQUALITY + ext.name, |
| 377 |
[castedAccess, |
| 378 |
cgen.makeReinterpretCast( |
| 379 |
STRUCT_EXTENSION_PARAM2.paramName, ext.name), |
| 380 |
EQUALITY_ON_FAIL_VAR]) |
| 381 |
|
| 382 |
self.module.appendImpl( |
| 383 |
self.codegen.makeFuncImpl( |
| 384 |
self.extensionTestingPrototype, |
| 385 |
lambda cgen: self.emitForEachStructExtension( |
| 386 |
cgen, |
| 387 |
EQUALITY_RET_TYPE, |
| 388 |
STRUCT_EXTENSION_PARAM, |
| 389 |
forEachExtensionCompare))) |