Skip to content

Commit 877b9fa

Browse files
committed
[svsim] Escape '$' in defines for VCS only
Add an abstract function to `svsim.Backend` which can be used to control how define key and values will be escaped. Add escaping for '$' for VCS only. This is done to work around what I think is a difference in how VCS and Verilator handle the `+define+key=value` command line argument. In svsim, we call both like the following (this is a key only, no value): <tool> '+define+Foo$Bar' While Verilator will accept this, VCS will not and instead wants: <tool> '+define+Foo\$Bar' As far as I can tell, the problem seems to be the single quotes not being respected by VCS. This is _likely_ due to the fact that the VCS entrypoint is shell and may have some oddities in how it is handling command line options. (This is speculation.) I expect that this could have manifested for anything that was using a Verilog system function, e.g.., `+define+RANDOM+$random`. For that specific case, we were already escaping it internally. The immediate need for this is to get inline layers and their `$`-ridden ABI (that I wrote...) working with VCS. They are already working with Verilator. Signed-off-by: Schuyler Eldridge <[email protected]>
1 parent 33b6b2d commit 877b9fa

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

svsim/src/main/scala/Backend.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ object CommonCompilationSettings {
2222
def apply(name: String) = new VerilogPreprocessorDefine(name, None)
2323
}
2424
case class VerilogPreprocessorDefine private (name: String, value: Option[String]) {
25-
private[svsim] def toCommandlineArgument: String = {
25+
private[svsim] def toCommandlineArgument(backend: Backend): String = {
2626
value match {
27-
case Some(v) => s"+define+${name}=${v}"
28-
case None => s"+define+${name}"
27+
case Some(v) => s"+define+${backend.escapeDefine(name)}=${backend.escapeDefine(v)}"
28+
case None => s"+define+${backend.escapeDefine(name)}"
2929
}
3030
}
3131
}
@@ -75,6 +75,12 @@ trait Backend {
7575
commonSettings: CommonCompilationSettings,
7676
backendSpecificSettings: CompilationSettings
7777
): Backend.Parameters
78+
79+
/** This function will be applied to all defines (both the keys and the values).
80+
* This can be used to workaround subtleties in how different simulators
81+
* parse defines and require different escaping.
82+
*/
83+
def escapeDefine(string: String): String
7884
}
7985

8086
final object Backend {

svsim/src/main/scala/vcs/Backend.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ final class Backend(
193193
VerilogPreprocessorDefine(svsim.Backend.HarnessCompilationFlags.supportsDelayInPublicFunctions)
194194
),
195195
backendSpecificSettings.traceSettings.verilogPreprocessorDefines
196-
).flatten.map(_.toCommandlineArgument),
196+
).flatten.map(_.toCommandlineArgument(this)),
197197
).flatten,
198198
environment = environment ++ Seq(
199199
"VCS_HOME" -> vcsHome,
@@ -212,4 +212,9 @@ final class Backend(
212212
)
213213
//format: on
214214
}
215+
216+
/** VCS seems to require that dollar signs in arguments are escaped. This is
217+
* different from Verilator.
218+
*/
219+
override def escapeDefine(string: String): String = string.replace("$", "\\$")
215220
}

svsim/src/main/scala/verilator/Backend.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,14 @@ final class Backend(
160160
VerilogPreprocessorDefine(svsim.Backend.HarnessCompilationFlags.enableVcdTracingSupport)
161161
)
162162
},
163-
).flatten.map(_.toCommandlineArgument),
163+
).flatten.map(_.toCommandlineArgument(this)),
164164
).flatten,
165165
environment = Seq()
166166
),
167167
simulationInvocation = svsim.Backend.Parameters.Invocation(Seq(), Seq())
168168
)
169169
//format: on
170170
}
171+
172+
override def escapeDefine(string: String): String = string
171173
}

svsim/src/test/scala/BackendSpec.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ case class CustomVerilatorBackend(actualBackend: verilator.Backend) extends Back
4343
backendSpecificSettings
4444
)
4545
}
46+
47+
override def escapeDefine(string: String): String = string
4648
}
4749

4850
class VerilatorSpec extends BackendSpec {

0 commit comments

Comments
 (0)